nginx as cache proxy not caching anything
Make sure your backend does not return Set-Cookie
header. If Nginx sees it, it disables caching.
If this is your case, the best option is to fix your backend. When fixing the backend is not an option, it's possible to instruct Nginx to ignore Set-Cookie
header
proxy_ignore_headers "Set-Cookie";
proxy_hide_header "Set-Cookie";
See the documentation
proxy_ignore_header
will ensure that the caching takes place. proxy_hide_header
will ensure the Cookie payload is not included in the cached payload. This is important to avoid leaking cookies via the NGINX cache.
NGINX proxy_pass not caching content
Turns out that thumbnail requests returned from Dropbox include the header
Cache-Control: no-cache
and Nginx will adhere to these headers unless they are explicitly ignored which can be done by simply using the following config line that will ignore any caching control.
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
We also had issues placing the "proxy_ignore_headers" option in different areas within the nginx.conf file. Finally after much playing around we got it to work by explicitly setting it in the "location" block. The full snippet of the config file can be found below
## Proxy Server Caching
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:50m inactive=2h max_size=2g;
## Proxy Server Setting
server {
listen *:8181;
location ~ ^/(.*) {
set $dropbox_api 'api-content.dropbox.com';
set $url '$1';
resolver 8.8.8.8;
proxy_set_header Host $dropbox_api;
proxy_hide_header x-dropbox-thumbcachehit;
proxy_hide_header x-dropbox-metadata;
proxy_hide_header x-server-response-time;
proxy_hide_header x-dropbox-request-id;
proxy_hide_header cache-control;
proxy_hide_header expires;
add_header cache-control "private";
add_header x-cache $upstream_cache_status; # HIT / MISS / BYPASS / EXPIRED
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_pass https://$dropbox_api/$url$is_args$args;
}
}
nginx is not caching proxied responses on disk, even though I ask it to
I tried running nginx alone with that nginx.conf, and found that it complained about some of the options being invalid. I think I was never successfully building a new nginx container at all.
In particular, it turns out you don't just put any old headers in the proxy_ignore_headers
option. It only takes particular headers as arguments, ones that the proxy system cares about.
Here is my revised nginx.conf, which worked:
upstream balancer53 {
server conceptnet-api-1:10053;
server conceptnet-api-2:10053;
}
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:100m max_size=100m;
server {
listen 80;
gzip on;
gzip_proxied any;
gzip_types application/json;
charset utf-8;
charset_types application/json;
location /web {
proxy_pass http://balancer53;
proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
}
location /data/5.3 {
proxy_pass http://balancer53;
proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
}
location / {
root /var/www;
index index.html;
autoindex on;
rewrite ^/static/(.*)$ /$1;
}
}
Related Topics
Recursively Find Files with a Specific Extension
What Is Preemption/What Is a Preemtible Kernel? What Is It Good For
Need Explanations for Linux Bash Builtin Exec Command Behavior
Execute a Shell Script Everyday at Specific Time
How Is It Possible That Kill -9 for a Process on Linux Has No Effect
How to Request a File But Not Save It with Wget
How to Purge Disk I/O Caches on Linux
Connect to Host Localhost Port 22: Connection Refused
Shell Script Current Directory
How to Break Up an Extremely Long String Literal in Bash
How to Force 'Cp' to Overwrite Directory Instead of Creating Another One Inside
Error: Can't Open Display: (Null) When Using Xclip to Copy Ssh Public Key
How to Display Modified Date Time with 'Find' Command
Grep Recursively for a Specific File Type on Linux
How to Ignore Line Breaks in Input Using Nasm Assembly
Delete Files with String Found in File - Linux Cli