用于静态文件和反向代理的Nginx映射
我正在尝试让nginx为某个子目录下的请求提供静态文件,而所有其他请求都是反向代理的,但运气不好(得到404s)。例如,我希望请求分别映射到/var/www/example.com/htdocs/index.html和/var/www/example.com/htdocs/subdir/file2.html 所有其他请求都应通过代理进行撤销。例如 这是我的活动nginx配置文件用于静态文件和反向代理的Nginx映射,nginx,reverse-proxy,Nginx,Reverse Proxy,我正在尝试让nginx为某个子目录下的请求提供静态文件,而所有其他请求都是反向代理的,但运气不好(得到404s)。例如,我希望请求分别映射到/var/www/example.com/htdocs/index.html和/var/www/example.com/htdocs/subdir/file2.html 所有其他请求都应通过代理进行撤销。例如 这是我的活动nginx配置文件 server { listen 8080; server_name example.com;
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;
}
}
这有助于解释发生了什么
您的配置:
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/;
}
.我将尝试使用以下方法: 阅读“如果重定向期间不需要更改URI,则可以将错误页面的处理重定向到指定位置” 我不必这样使用静态目录。:)