Nginx – Best way to serve static content

nginxstatic-content

For the moment, I serve my static content (jpg, png, css) from mydomain.com like this:

location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
root /home/www/mydomain/current/web;
add_header        Cache-Control public;
expires        365d;
access_log     off;
}

I'd like to serve it from static.mydomain.com to be "cookie free" and have better performances.

What is better? Add a new server section in my nginx vhost configuration with something like:

server {
    listen  192.168.2.25:80;
    server_name     static.mydomain.com;
    root /home/www/mydomain/current/web;

    location / {
   return 404;
    }

    location ~ \.(?:jpg|css|gif|png|swf|ico|mp3)$ {
add_header        Cache-Control public;
    }
}

Or proxy-pass the request for dynamic content like that (exemple take on nginx wiki):

server { 
listen       192.168.2.25:80;
server_name  www.mydomain.com;

# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/  {
  root    /var/www/virtual/big.server.com/htdocs;
  expires 30d;
}

# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
  proxy_pass      http://127.0.0.1:8080;
}

}

Thanks for your advices.

Best Answer

Your proxy_pass solution wouldn't result in a cookieless domain. Your dynamic content would set cookies on www.mydomain.com and you'd be back where you started (but with additional needless latency).

The first solution is the better of the two, but the best might be something like Amazon's CloudFront CDN with an origin of www.mydomain.com for static content.