abject_error
abject_error

Reputation: 2938

nginx not caching static assets

I have a nodejs server and SSL enabled nginx on 2 separate machines. Request/response all work properly, however I have some problems getting nginx to cache stuff. My server configuration is below. Initially, I had the proxy cache statement in the 'location /' block, and at the time it was caching only my index page. I read that nginx won't cache requests with set-cookie headers, so I ignored them as well (although it didn't stop my index page from getting cached earlier). I tried fiddling with this for a whole day, but couldn't get nginx to cache my js and css files. All such requests are getting routed back to my node server. Access logs and error logs don't have any unusual entries. What am I doing wrong?

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /webserver/nginx/credentials/cert;
    ssl_certificate_key /webserver/nginx/credentials/key;
    ssl_session_cache shared:SSL:10m;

    location ~ .*\.(ico|css|js|gif|jpe?g|png)$ {
        proxy_pass http://somewhere:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect http:// https://;

        proxy_ignore_headers "Set-Cookie";
        proxy_cache one;
        proxy_cache_valid 200 1d;
        proxy_cache_valid any 1m;
        expires 7d;
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
    }

    location / {
        proxy_pass http://somewhere:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect http:// https://;
    }
}

Upvotes: 0

Views: 9005

Answers (1)

Bill
Bill

Reputation: 25565

This is what I'm using (I don't have SSL enabled but I don't think that is the problem). You're missing the try_files line that tells nginx to look for the files in the root before passing off to the proxy. Also, it's not really a caching problem - none of the static file requests should ever be hitting your node.js backend with this configuration.

server {
  root        /public;
  listen      80;
  server_name _;
  index       index.html index.htm;
      charset     utf-8;

  # proxy request to node
  location @proxy {
    proxy_set_header   Host             $http_host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-NginX-Proxy    true;

    proxy_pass         http://127.0.0.1:3010;
    proxy_redirect     off;
    break;
  }  

  location / {
    try_files $uri.html $uri $uri/ @proxy;
  }

  #  static content
  location ~ \.(?:ico|jpg|css|png|js|swf|woff|eot|svg|ttf|html|gif)$ {
    access_log  off;
    log_not_found off;
    add_header  Pragma "public";
    add_header  Cache-Control "public";
    expires     30d;  
  }

  location ~ /\. {
    access_log    off;
    log_not_found off;
    deny all;
  }

  error_page 500 502 503 504 /50x.html;
      location = /50x.html {
      }

      error_page 404 /404.html;
      location = /404.html {
      }

}

Upvotes: 2

Related Questions