Nginx设置为将我的节点服务器9200代理到端口80,根设置为服务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;
}
答案 0 :(得分:1)
如果你有一个位置块匹配/ ... 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
开头。< / p>
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;
}