Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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;
  }

}
share|improve this question
    
nginx respects certain cache related headers sent by the upstream; what headers are being sent back to nginx? –  mgorven Mar 12 '13 at 17:41
    
Cache-Control:public, max-age=3600 –  George Mar 12 '13 at 17:58
    
Any Set-Cookie, Expires or X-Accel-Expires headers? –  mgorven Mar 12 '13 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 '13 at 18:17

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.