Skip to content

Instantly share code, notes, and snippets.

@AD7six
Created March 16, 2011 12:36
Show Gist options
  • Save AD7six/872413 to your computer and use it in GitHub Desktop.
Save AD7six/872413 to your computer and use it in GitHub Desktop.
Local development nginx config
# Set another default user than root for security reasons
# local development - use yourself
user andy wheel;
# As a thumb rule: One per CPU. If you are serving a large amount
# of static files, which requires blocking disk reads, you may want
# to increase this from the number of cpu_cores available on your
# system.
#
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes 1;
# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 8192;
events {
# When you need > 8000 * cpu_cores connections, you start optimizing
# your OS, and this is probably the point at where you hire people
# who are smarter than you, this is *a lot* of requests.
worker_connections 8000;
# This sets up some smart queueing for accept(2)'ing requests
# Set it to "on" if you have > worker_processes
accept_mutex off;
# These settings are OS specific, by defualt Nginx uses select(2),
# however, for a large number of requests epoll(2) and kqueue(2)
# are generally faster than the default (select(2))
# use epoll; # enable for Linux 2.6+
# use kqueue; # enable for *BSD (FreeBSD, OS X, ..)
}
# Change these paths to somewhere that suits you!
error_log logs/error.log;
pid logs/nginx.pid;
http {
# Set the mime-types via the mime.types external file
include mime.types;
# And the fallback mime-type
default_type application/octet-stream;
# Format for our log files
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Click tracking!
access_log logs/access.log main;
# ~2 seconds is often enough for HTML/CSS, but connections in
# Nginx are cheap, so generally it's safe to increase it
keepalive_timeout 5;
# You usually want to serve static files with Nginx
sendfile on;
tcp_nopush on; # off may be better for Comet/long-poll stuff
tcp_nodelay off; # on may be better for Comet/long-poll stuff
# Enable Gzip
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
server {
listen 80 default;
server_name _;
server_name_in_redirect off;
# Strip www. prefix
set $site $host;
if ($site ~ "^(w{3}\.)?(.*)$") {
set $site $2;
}
# Strip TLD prefix
if ($site ~ "^(.*)(\.[a-z]{3})$") {
set $site $1;
}
# Set webroot. Try multiples to support max variety of
# serveable applications
set $webroot /home/andy/www/apps/$site;
if (-d $webroot/webroot) {
set $webroot $webroot/webroot;
}
# Webroot for this domain could not be found
if (!-d $webroot) {
set $webroot /home/andy/www/apps/;
}
access_log /var/log/nginx/$site.access.log;
# Error log does not support variables. Look in default error log
rewrite_log on;
root $webroot;
index index.php index.html index.htm;
#auth_basic "Restricted";
#auth_basic_user_file /var/www/.htpasswd;
# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
# Pass the PHP scripts to FastCGI server
# listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on; # to support 404s for PHP files not found
include fastcgi_params;
}
expires 1M;
# Static files.
# Set expire headers, Turn off access log
location ~* \favicon.ico$ {
access_log off;
add_header Cache-Control public;
}
location ~ ^/(img|images|css|js|assets)/ {
expires max;
access_log off;
}
# Set expires max on static file types
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
expires max;
access_log off;
}
# Static assets
location ~* ^.+\.(manifest|appcache)$ {
expires -1;
}
# opt-in to the future
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment