Skip to content

Instantly share code, notes, and snippets.

@duduribeiro
Created March 12, 2014 15:13
Show Gist options
  • Save duduribeiro/9508966 to your computer and use it in GitHub Desktop.
Save duduribeiro/9508966 to your computer and use it in GitHub Desktop.
nginx.conf
# User and group that workers will use.
user nginx nginx;
# Number of NGINX workers. Usually it's set, at maximum, 1 worker per CPU core.
# If the server has 2+ CPU cores but it still does not receive many requests,
# it's a good idea to keep the value to 1 so it will avoid creating idle processes.
worker_processes 2;
# NGINX's master process id.
pid /var/run/nginx.pid;
events {
# How many client connections each worker can maintain at a time.
worker_connections 1024;
# Using epoll is less CPU intensive when you have to serve thousands of connections, because
# instead of scanning all connections to see which file descriptor is ready to read/write,
# it will only scan those which are active.
# p.s.: It's only available in Linux kernels later than 2.6.
use epoll;
}
# HTTP config module
http {
##
# MIME-TYPE
##
# You can include other config files using the "include" directive.
# Here we are including the mime-types, for example.
include /etc/nginx/mime.types;
# The "application/octet-stream" means that the response is a binary file.
# If this directive is not defined then response will be set to "text/plain" by default.
default_type application/octet-stream;
##
# LOG
##
# Log format. Other NGINX variables ($variable) can be used as you wish.
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Access log: path and type.
access_log /var/log/nginx_access.log main;
# Error log: path and type.
error_log /var/log/nginx_error.log debug;
##
# TCP
##
# Optimizes data transfer copying data between one file descriptor and another
# instead of reading and copying data to/from user space.
sendfile on;
# Causes NGINX to attempt to send its HTTP response head in one packet,
# instead of using partial frames. This is useful for prepending headers before calling sendfile,
# or for throughput optimization.
tcp_nopush on;
# Disables the Nagle algorithm.
# It's useful for sending frequent small bursts of data in real time.
tcp_nodelay off;
# Timeout during which a keep-alive client connection will stay open to serve
# all the requested files from the server side.
keepalive_timeout 30s;
# If a client does not receive anything within the time set in this directive, a connection is closed.
send_timeout 60s;
##
# GZIP
##
# In production you MUST set gzip to "on" in order to save bandwidth. Web browsers
# which handle compressed files (all recent ones do) will get a very smaller version
# of the server response.
gzip on;
# Enables compression for a given HTTP request version.
gzip_http_version 1.0;
# Compression level 1 (fastest) to 9 (slowest).
gzip_comp_level 6;
# Enables compression for all proxied requests.
gzip_proxied any;
# Minimum length of the response (bytes). Responses shorter than this length will not be compressed.
gzip_min_length 10000;
# Enables compression for additional MIME-types.
gzip_types text/plain text/css application/x-javascript text/xml
application/xml application/xml+rss text/javascript;
# Disables gzip compression for User-Agents matching the given regular expression.
# Is this case we've disabled gzip for old versions of the IE that don't support compressed responses.
gzip_disable "MSIE [1-6] \.";
# Load modular configuration files from the /etc/nginx/conf.d directory.
include /etc/nginx/conf.d/*.conf;
##
# CLUSTERS
##
# Below the "upstream" is defining some Thin clusters that are serving a Rails app.
# They will handle the requets proxied by NGINX.
upstream thin {
server 0.0.0.0:3000;
server 0.0.0.0:3001;
# Here's a quick example of how you can load balance the requests among the clusters
# applying the "weight" option to be used by the Round-robin algorithm.
server 0.0.0.0:3002 weight=2;
}
# Example using Unix sockets.
upstream puma {
server unix:///var/run/puma.sock;
}
##
# VHOSTS
##
# NGINX's virtual host.
server {
# Port to listen on.
listen 80;
# Sets the max size for file uploads to 20Mb.
client_max_body_size 20M;
# You can point the domain(s) served by this virtual host.
server_name www.myawesomesite.com myawesomesite.com ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com;
# VHost specific access log.
access_log /var/log/nginx.vhost.access.log main;
location / {
# Root directory from your application.
root /var/www/myawesomesite/current/public;
# Checks for the existence of files in order, and returns the first file that is found.
try_files $uri $uri/ /index.html;
# Forward user's IP address to Rails app.
proxy_set_header X-Real-IP $remote_addr;
# If the file exists as a static file, it won't check the other conditionals
if (-f $request_filename) {
break;
}
# Checks for index.html then rewrite
# the URL to add "/index.html" to the end of it.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# Append the ".html" to the end of the URL and then checks
# the file system for that file. If it exists, then it
# rewrites the URL to have explicit ".html" on its end.
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
# If there is no file on the file system then it sets all the
# necessary headers and proxies to the Thin upstream.
if (!-f $request_filename) {
# Here's where the magic happens!
proxy_pass http://thin;
# Uncomment or define another virtual horst (server directive)
# to serve and test the upstream w/ Unix sockets.
# proxy_pass http://puma;
break;
}
}
# Defines the static page for HTTP status 404
error_page 404 /var/www/myawesomesite/current/public/404.html;
# Defines the static page for HTTP status 40x
location = /40x.html {
root /usr/share/nginx/html;
}
# Defines the static page for HTTP status 500
error_page 500 /var/www/myawesomesite/current/public/500.html;
# Defines the static page for HTTP status 50x
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment