Nginx提供的是默认内容,而不是我的内容
我有一个Nginx提供的是默认内容,而不是我的内容,nginx,configuration,server,configuration-files,nginx-location,Nginx,Configuration,Server,Configuration Files,Nginx Location,我有一个nginx版本:nginx/1.10.3(Ubuntu)运行在ubuntu16.04.2lts上 我使用nginx来提供静态文件,即webpack生成的包,但这是不相关的 我想要实现的是: 在example.com上,我想提供/home/bundles/main/index.html。我能做到 在projects.example.com/project_1上,我想提供/home/bundles/project_1/index.html 在projects.example.com/proj
nginx版本:nginx/1.10.3(Ubuntu)
运行在ubuntu16.04.2lts
上
我使用nginx
来提供静态文件,即webpack
生成的包,但这是不相关的
我想要实现的是:
在example.com
上,我想提供/home/bundles/main/index.html
。我能做到
在projects.example.com/project_1
上,我想提供/home/bundles/project_1/index.html
在projects.example.com/project_2
上,我想提供/home/bundles/project_2/index.html
最后两个,我做不到。当我进入projects.example.com/project_1
或projects.example.com/project_2
时,会收到默认的nginx页面
为了让事情变得更加混乱,/etc/nginx/sites enabled/default
完全被注释掉了
此外,如果在projects.example.com
的位置
块中,我用/
替换了例如project\u 1
,我将获得该特定项目的服务,但我将无法为其他项目提供服务
下面,我将向您展示我的nginx配置
谢谢你的帮助
编辑 我的回答 我找到的解决方案是使用
alias
更改root
server {
listen 80;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
server {
listen 80;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
解决方案基于这两个答案。第一个说明如何解决问题,第二个说明为什么alias
有效,而root
无效
引用@treecoder
对于root指令,将完整路径附加到包含位置部分的根,而对于alias指令,仅将不包含位置部分的路径部分附加到别名
在我的特殊情况下,这将像这样翻译
使用root
,nginx尝试访问的路径将是/home/bundles/project\u 1/project\u 1
使用alias
访问正确的路径,/home/bundles/project\u 1
回到一个层次,例如,说:
root/home/bundles/
也不是真正的选项。这是因为我的项目实际上没有被称为project\u 1
和project\u 2
。实际结构与此更为相似
在/bundles
中,我有项目a
和项目b
的目录。我想将project_1
路由到project_a
目录,并将project_2
路由到project_b
目录
这就是我使用别名的原因
server {
listen 80;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
server {
listen 80;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
我希望这能有所帮助。您有:
location /project_1 {
root /home/bundles/project_1;
try_files $uri /index.html;
}
因此,根
仅为以/project\u 1
开头的URI定义。对于任何其他URI,将使用默认的root
如果您提供URI/project\u 1/
(带有尾随的/
),假设有效,nginx
应返回您的/project\u 1/index.html
内容
但是,找不到URI/project_1
,因此返回/index.html
。URI/index.html
不是以/project\u 1
开头,因此使用默认根目录
如果希望URI
/project\u 1
按预期工作,并希望默认操作转到项目的index.html
文件,请更改try\u files
指令
location /project_1 {
root /home/bundles/project_1;
try_files $uri $uri/ /project_1/index.html;
}
更多信息,请参阅
由于两个项目共享一个公共根,您可以简化如下:
server {
listen 80;
server_name projects.example.com;
root /home/bundles
index index.html;
location /project_1 {
try_files $uri $uri/ /project_1/index.html;
}
location /project_2 {
try_files $uri $uri/ /project_2/index.html;
}
location / {
deny all;
}
}
我添加了
索引
指令以避免依赖默认值(相同),并添加了位置
块以拒绝访问项目以外的区域。我也遇到了同样的问题……结果,对我来说,默认站点“超车”我想要的辅助站点和服务默认文件…它允许IP6请求,而我的新站点不允许
下面是my access.log的一个示例:
::1 - - [05/Sep/2020:15:05:16 -0600] "GET / HTTP/1.1" 200 40 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
注意::1
位?这是一个IP6请求,我猜我的浏览器是默认的。因此,我只是通过更改以下内容,确保在我的站点配置文件中为相关站点启用本地IP6请求:
listen 80;
server_name example.local;
致:
就这样
listen 80;
listen [::]:80;
server_name example.local;