1

I am utilizing Nginx's proxy_cache module for caching but have noticed the page does not cache immediately. The content is very dynamic and each page load produces a different layout, I am trying to cache the layout for an hour.

Nginx only seems to cache the page after hitting it with multiple requests quickly. Here is my config.

proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=1000m; #caching
proxy_temp_path /var/tmp; #caching

proxy_cache_valid 404 500 1m;
proxy_cache_valid 200 60m;
proxy_cache_min_uses 1;

gzip_comp_level 6;
gzip_vary on;
gzip_min_length  1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;

upstream staging {
  server 127.0.0.1:1337;
  server 127.0.0.1:7331;
}

server {
  listen 0.0.0.0:80;
  server_name dev.example.com;
  access_log /var/log/nginx/dev.example.log;
  error_log  /var/log/nginx/dev.example.error.log debug;   log_subrequest on;

  location ~ ^/(images/|scripts/|styles/|robots.txt|humans.txt|favicon.ico) { #caching
    root /home/example/app/website/public;
    access_log off;
    expires max;
  }

  location /ssi { #Our serverside includes
    proxy_pass http://staging;
  }

  location / {
    ssi on;

    #auth_basic "Restricted";
    #auth_basic_user_file /etc/nginx/.htpasswd;

    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_cache one; #caching
    proxy_cache_key sfs$request_uri$scheme; #caching

    proxy_http_version 1.1;
    proxy_pass http://staging/; #points to the upstream staging
  }
}

server {
  listen 0.0.0.0:80;
  server_name static.example.com;

  location ~ ^/(images/|scripts/|styles/|robots.txt|humans.txt|favicon.ico) { #caching
    add_header Access-Control-Allow-Origin *.example.com;
    root /home/example/app/website/public;
    access_log off;
    expires max;
  }

  location / {
    return 404;
  }

}
4
  • nginx respects certain cache related headers sent by the upstream; what headers are being sent back to nginx?
    – mgorven
    Mar 12, 2013 at 17:41
  • Cache-Control:public, max-age=3600
    – George
    Mar 12, 2013 at 17:58
  • Any Set-Cookie, Expires or X-Accel-Expires headers?
    – mgorven
    Mar 12, 2013 at 18:02
  • Looks like I was using Set-Cookie after all. I'm using Express with Node.js and using express.session() sets a cookie for the session. Removing that has allowed caching on the first request. Thank you! Go ahead and post the answer and I'll accept it.
    – George
    Mar 12, 2013 at 18:17

1 Answer 1

3

nginx decides whether upstream responses can be cached based on certain headers. From the documentation:

The following response headers flag a response as uncacheable unless they are ignored:

  • Set-Cookie
  • Cache-Control containing "no-cache", "no-store", "private", or a "max-age" with a non-numeric or 0 value
  • Expires with a time in the past
  • X-Accel-Expires: 0

Check what headers your upstream is sending to nginx and make sure that they allow caching.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .