schwarz

archive of mindset

schwarz header image 5

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

→ 2 comments Tags:

How to use lighttpd to serve a static site for development

September 18, 2007 · 0 comments

Write the following into your /etc/lighttpd/lighttpd.conf


server.bind = "0.0.0.0"
server.port = 3000
server.document-root = CWD
server.dir-listing = "enable"

mimetype.assign = (  
  ".css"        =>  "text/css",
  ".gif"        =>  "image/gif",
  ".htm"        =>  "text/html",
  ".html"       =>  "text/html",
  ".jpeg"       =>  "image/jpeg",
  ".jpg"        =>  "image/jpeg",
  ".js"         =>  "text/javascript",
  ".png"        =>  "image/png",
  ".swf"        =>  "application/x-shockwave-flash",
  ".txt"        =>  "text/plain"
)

Not bad, now, a really nice way to execute this, would be from the directory of where your sites' html & assets are at.

Place the folowing in to your ~/.bash_login or ~/.profile file.

alias li="lighttpd -D -f /etc/lighttpd/lighttpd.conf"

Proof is in the pudding:


bens-pb:~/sites ben$ li
2007-09-18 23:44:55: (log.c.75) server started

Ctrl+c will quit the server


2007-09-18 23:45:30: (server.c.1216) [note] graceful shutdown started 
2007-09-18 23:45:30: (log.c.135) server stopped

Shazam! Easy.

→ 0 comments Tags: