我正在尝试从应用程序中获取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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果文件不存在,则可以使用/path/to/__static
; 它将请求发送到应用程序服务器.但是,如果文件也存在/path/to/__static
,nginx会为文件本身提供服务.
在这两种情况下,反转try_files
line(try_files @proxy_to_app $uri
)都会失败.如果客户端请求/static/css/test.css
,则应用程序会收到请求/css/test.css
,/path/to/__static
即使应用程序返回404 ,它也似乎永远不会尝试.
已更新,包括完整配置.
location /static/ {
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;
proxy_intercept_errors on;
error_page 404 =200 /local$uri;
}
location /local/static/ {
internal;
alias /path/to/__static/;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9239 次 |
最近记录: |