我正在尝试,没有运气(获得404s),让nginx为某个子目录下的请求提供静态文件,而所有其他请求都是反向代理的。例如,我想要http://example.com/project/static/index.html和http://example.com/project/static/subdir/file2.html的请求分别映射到/var/www/example.com/htdocs/index.html和/var/www/example.com/htdocs/subdir/file2.html。
所有其他请求应反向代理。例如,http://example.com/project/thisWillBeProxied.html和http://example.com/project/subdir/soWillThis.html
这是我的活跃nginx个人资料
server {
listen 8080;
server_name example.com;
access_log /var/www/example.com/log/nginx.access.log;
error_log /var/www/example.com/log/nginx_error.log debug;
location / {
proxy_pass http://reverse-proxy-host/;
}
location ^~ /project/static/ {
alias /var/www/example.com/htdocs;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
}
error log,有助于解释发生了什么。
你的配置:
location ^~ /project/static/ {
alias /var/www/example.com/htdocs;
}
它完全符合你所写的。它将URI中的/project/static/
替换为文件路径/var/www/example.com/htdocs
。所以,如果请求看起来像http://example.com/project/static/index.html
那么nginx将尝试打开/var/www/example.com/htdocsindex.html
。我假设你不想服务/var/www/example.com/htdocsindex.html
,但你想服务/var/www/example.com/htdocs/index.html
然后你应该写:
location ^~ /project/static/ {
alias /var/www/example.com/htdocs/;
}
我打算尝试使用它:http://wiki.nginx.org/HttpCoreModule#error_page
阅读“如果在重定向期间无需更改URI,则可以将错误页面的处理重定向到命名位置”
我不必这样使用静态目录。 :)