I need to nginx proxy use cache if backend server down:
this is my configuration. but seems be nginx use cache without check backend server.
http {
# ...
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=tmpzone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
server {
server_name _;
location / {
proxy_connect_timeout 5s;
proxy_read_timeout 5s;
proxy_cache tmpzone;
proxy_cache_valid 200 304 1d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host 'www.example.com';
proxy_pass http://www.example.com;
}
}
}
Question is how can i bypass the proxy cache if backend server is up ? And when backend server is up my proxy server dont use cache at all.
Seems a duplicate of this:
https://stackoverflow.com/questions/16756271/how-to-configure-nginx-to-serve-cached-content-only-when-backend-is-down-5xx-re
In short, use proxy_cache_use_stale
As an update, i tested this and it works fine. I did the test in my workstation where i have (for completeness):
Fedora 23 nginx 1.8.1 configured as ssl terminator + cache + reverse proxy Apache 2.4.18 configured to listen at port 80
With apache acting as upstream, serving just a static file i did this test:
The nginx config i used is (only the interesting parts):
nginx.conf :
/etc/nginx/conf.d/local.conf :
Use proxy_intercept_errors and proxy 500s to a server that has caching enabled.