Skip to content

Instantly share code, notes, and snippets.

@rchampourlier
Created October 13, 2011 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rchampourlier/1283618 to your computer and use it in GitHub Desktop.
Save rchampourlier/1283618 to your computer and use it in GitHub Desktop.
Template for nginx host file for unicorn-served Rails application deployed through our Capistrano recipe
# Some changes made on https://github.com/ricodigo/ricodigo-capistrano-recipes/blob/master/generators/nginx.conf.erb
upstream <%= application %>_app_server {
server unix:<%= unicorn_socket %> fail_timeout=0;
}
# <%= application %> Server
server {
listen <%= app_port %>;
client_max_body_size 500M;
server_name <%= nginx_domains %>;
# ~2 seconds is often enough for most folks to parse HTML/CSS and
# retrieve needed images/icons/frames, connections are cheap in
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root <%= deploy_to %>/current/public;
access_log <%= logs_path %>/nginx.access.log;
error_log <%= logs_path %>/nginx.error.log;
# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
# If the file exists as a static file serve it directly without
# running all the other rewite tests on it
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if its there on the filesystem then rewite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# this is the meat of the rails page caching config
# it adds .html to the end of the url and then checks
# the filesystem for that file. If it exists, then we
# rewite the url to have explicit .html on the end
# and then send it on its way to the next config rule.
# if there is no file on the fs then it sets all the
# necessary headers and proxies to our upstream mongrels
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://<%= application %>_app_server;
break;
}
}
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
# BUT there's a chance it could break the ajax calls.
location ~* \.(ico|css|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
location ~ ^/javascripts/.*\.js(\?[0-9]+)?$ {
expires max;
break;
}
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
# BUT there's a chance it could break the ajax calls.
location ~* \.(ico|css|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
location ~ ^/javascripts/.*\.js(\?[0-9]+)?$ {
expires max;
break;
}
# if the request is for a static resource, nginx should serve it directly
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
location ~ ^/(images|javascripts|stylesheets|system)/ {
root <%= deploy_to %>/current/public;
expires max;
break;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root <%= deploy_to %>/current/public;
}
}
<% if app_uses_ssl %>
upstream <%= application %>_app_ssl {
server unix:<%= unicorn_socket %> fail_timeout=0;
}
# This server is setup for ssl. Uncomment if
# you are using ssl as well as port 80.
server {
listen <%= app_port_ssl %>;
server_name <%= nginx_domains %>;
ssl on;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
ssl_session_timeout 5m;
client_max_body_size 50M;
root <%= deploy_to %>/current/public;
access_log <%= deploy_to %>/current/log/nginx.access.log;
error_log <%= deploy_to %>/current/log/nginx.error.log;
# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
location / {
# needed to forward user's IP address to rails
proxy_set_header X-Real-IP $remote_addr;
# needed for HTTPS
proxy_set_header X_FORWARDED_PROTO https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_max_temp_file_size 0;
# If the file exists as a static file serve it directly without
# running all the other rewite tests on it
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if its there on the filesystem then rewite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# this is the meat of the rails page caching config
# it adds .html to the end of the url and then checks
# the filesystem for that file. If it exists, then we
# rewite the url to have explicit .html on the end
# and then send it on its way to the next config rule.
# if there is no file on the fs then it sets all the
# necessary headers and proxies to our upstream mongrels
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://<%= application %>_app_ssl;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root <%= deploy_to %>/current/public;
}
}
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment