Skip to content

Instantly share code, notes, and snippets.

@shapeshed
Created October 10, 2011 19:13
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save shapeshed/1276230 to your computer and use it in GitHub Desktop.
Save shapeshed/1276230 to your computer and use it in GitHub Desktop.
Nginx Config for Rails 3.1 with Unicorn and Asset Pipeline
upstream app {
server unix:/srv/app/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.app.com;
rewrite ^/(.*) http://app.com/$1 permanent;
}
server {
listen 80;
client_max_body_size 2G;
server_name app.com;
keepalive_timeout 5;
root /srv/app/current/public;
access_log off;
error_log off;
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
location ~ ^/(assets)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
try_files $uri/index.html $uri.html $uri @app;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location @app {
proxy_pass http://app;
}
location = /favicon.ico {
expires max;
add_header Cache-Control public;
}
location ~ \.php$ {
deny all;
}
}
@maletor
Copy link

maletor commented Sep 8, 2012

i see what you did there with the php

@DavidEGrayson
Copy link

This configuration is not great because if someone just requests /assets/application.css from your servo (without a hash) you are going to tell him to go ahead and cache that resource forever, when in fact it will change.

@daenney
Copy link

daenney commented Jan 23, 2013

Might I suggest an asset match block like this instead:

location ~ "^/assets/(.*/)*.*-[0-9a-f]{32}.*" {
    ...
}

This will match any file however many subdirectories deep that looks like name-32charslonghash.extension.

So this will match:

  • /assets/application-fecb6ab224716be9503690659be9bb3e7.js

And also:

  • /assets/some/other/dir/welcome-fecb6ab224716be9503690659be9bb3e7.css

But not:

  • /assets/application.js

@companygardener
Copy link

Extended your asset regex to include '.' before extension and ending anchor.

location ~ "^/assets/(.*/)*.*-[0-9a-f]{32}\..*$" {
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment