Nginx CSS Caching

mattrick asked:

I’ve been trying to fix this for a while, but I haven’t been able to find a solution.

Some notes about my setup:

  • I am not using VirtualBox
  • I am running Nginx 1.4.6 Ubuntu

The CSS’s cache will not clear even when using the “expires: off;” flag in my site configuration. Here’s my site config:

server {
  # Replace this port with the right one for your requirements
  listen 80;  #could also be 1.2.3.4:80

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name www.example.com; # Alternately: _

  root /var/www/;

  error_page 404 /404.html;

  location = /404.html {  
    root  /etc/nginx/errors;  
  }  
  #access_log logs/star.example.com.access.log;

  index index.php index.html index.htm;

  location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires off;
  }

  location / {
    root /var/www;
    try_files $uri $uri/ @extensionless-php;
    index index.php index.html;
  }

  location ~ .php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }

  location ~ /.ht {
    deny  all;
  }

  location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
  }

}

My nginx.conf is as follows:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    client_max_body_size 150m;

    sendfile off;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 2;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

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

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Thank you 🙂

My answer:


I don’t think expires off is what you really want here. That just disables sending the Cache-Control and Expires headers entirely, and you get browser-default behavior (which is usually RFC compliant, resulting in the resource being cached).

If you mean to have the resource not cached at all by user agents, use expires epoch instead.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.