将来自 location /projects 的请求直接发送到不同的根目录,Nginx?
Direct requests from location /projects to a different root, Nginx?
Nginx 设置为将我的节点服务器 9200 代理到端口 80,root 设置为服务 mypath/to/node_build
。我想上传一些静态文件到 /projects
.
所以我 location /projects { root /mypath/to/static_projects}
但我收到 404 错误。
server {
listen 80;
root /var/www/example.io/public_html/dist;
index index.html index.htm;
# Make site accessible from http://example.io
server_name example.io;
server_name app.example.io;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
}
location / {
proxy_pass http://127.0.0.1:9200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ^~ /blog {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:9200;
proxy_redirect off;
}
location /projects {
root /var/www/example.io/public_html/projects;
}
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable “msie6″;
#error_page 404 /404.html;
}
当你有一个位置块匹配 /... nginx 将继续匹配其他块——参见:nginx docs on "location":
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
[ configuration C ]
}
因此,如果您在匹配静态文件 (\.(js|css|png|jpg|jpeg|gif|ico)$
) 之上还有另一个块,这些最终将使用您的默认值 root
。
你可以做的是在你的 /projects
块中放置一个位置块来处理静态文件,或者创建一个新的位置块,其正则表达式匹配静态文件和以 /projects
开头的路径.
location /projects {
root /var/www/example.io/public_html/projects;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
try_files $uri =404;
}
}
# or:
location ~* ^/projects/.+\.(js|css|png|jpg|jpeg|gif|ico)$ {
root /var/www/example.io/public_html/projects;
expires 1y;
try_files $uri =404;
}
Nginx 设置为将我的节点服务器 9200 代理到端口 80,root 设置为服务 mypath/to/node_build
。我想上传一些静态文件到 /projects
.
所以我 location /projects { root /mypath/to/static_projects}
但我收到 404 错误。
server {
listen 80;
root /var/www/example.io/public_html/dist;
index index.html index.htm;
# Make site accessible from http://example.io
server_name example.io;
server_name app.example.io;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
}
location / {
proxy_pass http://127.0.0.1:9200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ^~ /blog {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:9200;
proxy_redirect off;
}
location /projects {
root /var/www/example.io/public_html/projects;
}
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable “msie6″;
#error_page 404 /404.html;
}
当你有一个位置块匹配 /... nginx 将继续匹配其他块——参见:nginx docs on "location":
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
[ configuration C ]
}
因此,如果您在匹配静态文件 (\.(js|css|png|jpg|jpeg|gif|ico)$
) 之上还有另一个块,这些最终将使用您的默认值 root
。
你可以做的是在你的 /projects
块中放置一个位置块来处理静态文件,或者创建一个新的位置块,其正则表达式匹配静态文件和以 /projects
开头的路径.
location /projects {
root /var/www/example.io/public_html/projects;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
try_files $uri =404;
}
}
# or:
location ~* ^/projects/.+\.(js|css|png|jpg|jpeg|gif|ico)$ {
root /var/www/example.io/public_html/projects;
expires 1y;
try_files $uri =404;
}