我有以下Nginx配置文件..。
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common/ {
root /etc/nginx/html/common;
}
}文件夹结构就像..。
html\app1
html\公共
当我试图浏览..。
http://localhost/ > Works
http://localhsot/index.html > Works
http://localhost/common/somefile.txt >不工作
我遗漏了什么?
发布于 2015-10-12 15:36:14
您应该使用alias而不是root
server {
listen 80;
server_name 127.0.0.1 localhost;
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common {
alias /etc/nginx/html/common;
}
}如果在root中使用common,127.0.0.1/common/somefile.txt将尝试/etc/nginx/html/common/common/somefile.txt (注意两个common)。如果你检查nginx的日志,你就能看到它。
发布于 2015-10-12 12:58:48
我正在添加我自己的答案,因为我终于使它发挥作用。把它贴在这里,这样也许能帮到别人.
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html;
}
}基本上,Nginx尝试的方式是/etc/nginx/html/公用/通用。从根目录中移除公共部分有效。还发现http://localhost:8888/common/需要有一个跟踪/。
发布于 2015-10-12 11:24:52
因为它首先与location /匹配。你可以这样做:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html/common;
}
} 编辑:是的。好像有点复杂。你可以这样做:
首先,您需要创建一个新服务器:
server {
listen 80;
server_name common.com; # A virtual host
root /etc/nginx/html/common;
}然后,您需要修改上面的配置如下:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
rewrite ^/common(/.*)$ $1 break; # rewrite the /common/
proxy_set_header Host common.com; # it will requests common.com which the server of 127.0.0.1. then will match the above server.
proxy_pass http://127.0.0.1;
}
} https://stackoverflow.com/questions/33078633
复制相似问题