nginxmongrel
nginx + Mongrel cluster
(local copy only, the original – Ezra Zygmuntowicz’s config )
user www-data www-data;
worker_processes 5;
pid logs/nginx.pid;
events {
worker_connections 8192;
use epoll; # linux only!
# use kqueue; # *BSD, OS X only!
}
http {
include conf/mime.types;
default_type application/octet-stream;
# configure log format
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';
access_log logs/access.log main;
error_log logs/error.log debug;
include conf/optimize.conf;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
upstream mongrel {
server 127.0.0.1:5001;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
}
server {
listen 80;
# sets the domain[s] that this vhost server requests for
#server_name example.com www.example.com;
root /var/www/railsapp/public;
index index.html index.htm;
# vhost specific access log
access_log logs/host.access.log main;
# rewrites all the requests to the maintenance.html
# page if it exists in the doc root.
if (-f $document_root/maintenance.html){
rewrite ^(.*)$ /maintenance.html last;
break;
}
# rails page caching
location / {
# 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;
}
proxy_pass http://mongrel;
include conf/proxy.conf;
break;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
The optimize.conf file referenced above:
# conf/optimize.conf # no sendfile on OSX - comment the next line sendfile on; tcp_nodelay on; tcp_nopush on; keepalive_timeout 75 20; server_names_hash_bucket_size 128; # this seems to be required for vhosts # other optimizations # origin: http://www.typemiss.net/blog/kounoike/20060227-75 client_header_timeout 10m; client_body_timeout 10m; send_timeout 10m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 4 2k; request_pool_size 4k; output_buffers 1 32k; postpone_output 1460; ignore_invalid_headers on;
The proxy.conf file referenced above:
# conf/proxy.conf proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
ngnix+ssl+rails
server {
listen 443;
ssl on;
# path to your certificate
ssl_certificate /etc/nginx/certs/server.crt;
# path to your ssl key
ssl_certificate_key /etc/nginx/certs/server.key;
# put the rest of your server configuration here.
location / {
# set X-FORWARDED_PROTO so ssl_requirement plugin works
proxy_set_header X-FORWARDED_PROTO https;
# standard rails+mongrel configuration goes here.
}
}