【发布时间】:2013-02-22 03:26:49
【问题描述】:
如果应用程序正在为它们提供服务,我正在尝试从应用程序获取 nginx 反向代理静态文件,否则自己提供它们。目前,我有这个配置:
upstream app_server {
server unix:/tmp/gunicorn.sock fail_timeout=0;
}
server {
listen 8080;
server_name example.com;
access_log /var/log/nginx.access.log;
error_log /var/log/nginx.error.log;
keepalive_timeout 5;
location /static {
try_files $uri @proxy_to_app;
alias /path/to/__static;
sendfile off;
}
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
如果文件在/path/to/__static 中不存在,则此方法有效;它将请求发送到应用程序服务器。但是,如果文件也存在于 /path/to/__static 中,则 nginx 会自行提供文件。
在这两种情况下,反转 try_files 行 (try_files @proxy_to_app $uri) 都会失败。如果客户端请求/static/css/test.css,则应用程序会收到对/css/test.css 的请求,即使应用程序返回404,它似乎也不会尝试/path/to/__static。
更新为包含完整配置。
【问题讨论】:
-
请显示您的完整配置。可能最有趣的部分是“@proxy_to_app”。
-
已更新完整配置
-
你读过the documentation吗?
try_files指令根本不适合您的任务。您应该使用error_page和proxy_intercept_errors的组合。
标签: nginx