schwarz

archive of mindset

schwarz header image 4

Extreme expires headers for nginx and mongrel

September 19, 2007 · 2 comments

Like everyone else, I read ezra's nginx config and put it straight to use.

After recently becoming addicted to yslow, tweaking configs day and night (well, it happened once. yslow is great though) I decided to add expires headers to anything that isn't served by rails, your js, css, images etc.

Without further ado, my config:


worker_processes  2;
pid /var/run/nginx.pid;
events {
  worker_connections 1024;
}

http {
  include conf/mime.types;
  default_type  application/octet-stream;
  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status  $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx_access.log  main;
  error_log  /var/log/nginx_error.log debug;
  sendfile on;

  tcp_nopush on;
  tcp_nodelay off;
  gzip on;
  gzip_http_version 1.0;
# More is heavier on the CPU
  gzip_comp_level 5;
  gzip_proxied any;
  gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  upstream schwarz {
    server 127.0.0.1:4000;
    server 127.0.0.1:4001;
    server 127.0.0.1:4002;
  }
  
  server {
    listen 80;
    client_max_body_size 50M;
    server_name .germanforblack.com;
    root /var/www/public;

    access_log  /var/log/nginx.vhost.access.log  main;
    
    location / {
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      if (-f $request_filename) { 
	expires max;
        break;
      }

      if (-f $request_filename/index.html) {
       rewrite (.*) $1/index.html break;
      }
      # Tasty caching
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://schwarz;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/public;
    }
  }
}

Note the expires method. You just sped up your app dramatically

Tags:·······

  • 1 Nate Clark // Oct 05, 2007 at 04:20 AM

    This is awesome, thank you. So simple! One question though: would you also use the expires max header in the rewrite block that serves the page cached html pages? That would make sense to me because then your cached pages are cached on the client side as well. Like this:
    expires max;
    rewrite (.*) $1.html break;
  • 2 Ben Schwarz // Oct 06, 2007 at 02:50 AM

    No worries Nate. I suppose that you could indeed add the expires max header in that block also, albetit dangerous (You'll be serving old content) One technique (at least for js, css and images) that I've been considering is asset packager, which places a version number into the packaged script file name. Then, as your scripts / app are deployed, your clients are served up something fresh.