Skip to content

Instantly share code, notes, and snippets.

@loe
Created October 6, 2008 17:04
Show Gist options
  • Save loe/15078 to your computer and use it in GitHub Desktop.
Save loe/15078 to your computer and use it in GitHub Desktop.
user user group;
worker_processes 5;
pid /var/run/nginx.pid;
events {
worker_connections 8192;
use epoll;
}
http {
include /etc/nginx/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"';
sendfile on;
tcp_nopush on;
# Gzip lets you fit more through those narrow tubes.
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#include /etc/nginx/upstream.conf;
# Included inline for this example.
upstream mongrels {
server localhost:5000;
server localhost:5001;
server localhost:5002;
server localhost:5003;
server localhost:5004;
}
# /end upstream.conf
server {
listen 80;
server_name _;
/path/to/your/app/current;
client_max_body_size 2000M; # Allow 2GB uploads
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log notice;
# Allow a few file types to be used by the maintenance page.
if ($request_filename ~* \.(css|jpg|gif|png)$) {
break;
}
# Returns 503 if you have a maintenance page in place.
if (-f $document_root/system/maintenance.html) {
return 503;
}
# Simple rewrites, including capturing the 503 you just created and serving the maintenance page.
error_page 404 /404.html;
error_page 500 502 504 /500.html;
error_page 503 @503;
location @503 {
rewrite ^(.*)$ /system/maintenance.html break;
}
#include /etc/nginx/locations.conf;
# Include any custom location directives in here, things like an S3 proxy URL etc.
location /nginx_status {
stub_status on;
access_log off;
allow 1.2.3.4;
deny all;
}
# /end locations.conf
location / {
index index.html;
proxy_redirect false;
proxy_max_temp_file_size 0;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # List of IPs handling the request
proxy_pass_header Host; # We set host on our load-balancer (also nginx)
proxy_pass_header X-Real-IP; # The remote IP of the user, set by our border nginx.
proxy_set_header X-Webserver-Ip $server_addr; # We do some special talking between our webserver nginxs to serve files out of a temp directory while they're in the process of being copied to S3
# set Expire header on assets: see http://developer.yahoo.com/performance/rules.html#expires
location ~ ^/(images|javascripts|stylesheets|themes)/ {
expires max;
# Pass files that don't exist to the mongrels, these are generally cached stylesheets/javascripts that are mashed together by rails.
if (!-f $request_filename) {
proxy_pass http://mongrels;
break;
}
}
# 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;
}
# Send the request to the backends.
if (!-f $request_filename) {
proxy_pass http://mongrels;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment