nginx try_files from proxy'd app, then nginx
10,408
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/;
}
Author by
rouge8
Updated on September 11, 2022Comments
-
rouge8 over 1 year
I'm attempting to have nginx reverse proxy static files from an application if the application is serving them, else serve them itself. Currently, I have this configuration:
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; } }
This works if the files don't exist in
/path/to/__static
; it sends the request to the application server. However, if the files also exist in/path/to/__static
, nginx serves the files itself.Reversing the
try_files
line (try_files @proxy_to_app $uri
) fails in both cases. If the client requests/static/css/test.css
, the application receives a request for/css/test.css
, and it never seems to try/path/to/__static
even though the application returns a 404.Updated to include full configuration.
-
rouge8 about 11 yearsThe
error_page 404 = /local/$uri;
line doesn't work. It needs to be changed toerror_page 404 =200 /local$uri;
.