Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx as cache proxy not caching anything

Tags:

I'm trying to cache static content which are basically inside the paths below in virtual server configuration. For some reason files are not being cached. I see several folders and files inside the cache dir but its always something like 20mb no higher no lower. If it were caching images for example would take at least 500mb of space.

Here is the nginx.conf cache part:

** nginx.conf ** proxy_cache_path /usr/share/nginx/www/cache levels=1:2 keys_zone=static$ proxy_temp_path /usr/share/nginx/www/tmp; proxy_read_timeout 300s; 

Heres the default virtual server.

**sites-available/default** server {     listen   80;       root /usr/share/nginx/www;     server_name myserver;     access_log /var/log/nginx/myserver.log main;     error_log /var/log/nginx/error.log;      proxy_set_header Host $host;     proxy_set_header X-Real-IP $remote_addr;     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      location ~* ^/(thumbs|images|css|js|pubimg)/(.*)$ {             proxy_pass http://backend;             proxy_cache static;             proxy_cache_min_uses 1;             proxy_cache_valid 200 301 302 120m;             proxy_cache_valid 404 1m;             expires max;     }      location / {             proxy_pass http://backend;     } } 
like image 356
Bruno Faria Avatar asked Feb 10 '12 16:02

Bruno Faria


People also ask

Does nginx proxy cache?

Enabling Proxy Cache in NGINXNGINX also supports the caching of responses from other proxied servers (defined by the proxy_pass directive). For this test case, we are using NGINX as a reverse proxy for a Node. js web application, so we will enable NGINX as a cache for the Node.

How do I enable caching in nginx?

Go to the “Web Server” tab. In the “nginx settings” section, select the “Enable nginx caching” checkbox. (Optional) You can customize nginx caching settings. If you are not familiar with nginx caching, we recommend that you keep the default settings.

Does nginx support caching?

Specifying Which Requests to CacheBy default, NGINX Plus caches all responses to requests made with the HTTP GET and HEAD methods the first time such responses are received from a proxied server. As the key (identifier) for a request, NGINX Plus uses the request string.

How do I know if nginx caching is working?

1) Adding cache status header You could also check your header by using Developer Tools in your browser (Firefox and Chrome can open dev tools with F12 ). The cache status could be one of the following: “ MISS ”, “ BYPASS ”, “ EXPIRED ”, “ STALE ”, “ UPDATING ”, “ REVALIDATED ”, or “ HIT ”.


1 Answers

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.

like image 165
Alexander Azarov Avatar answered Sep 20 '22 14:09

Alexander Azarov