Sample Nginx Configurations
Simple Configuration with PHP enabled
Here we have a very simple starting configuration with PHP support. I will provide most of the comments here for the basic lines. The other sample configurations won't be as heavily commented.
server {
# This will listen on all interfaces, you can instead choose a specific IP
# such as listen x.x.x.x:80; Setting listen 80 default_server; will make
# this server block the default one if no other blocks match the request
listen 80;
# Here you can set a server name, you can use wildcards such as *.example.com
# however remember if you use server_name *.example.com; You'll only match subdomains
# to match both subdomains and the main domain use both example.com and *.example.com
server_name example.com www.example.com;
# It is best to place the root of the server block at the server level, and not the location level
# any location block path will be relative to this root.
root /usr/local/www/example.com;
# It's always good to set logs, note however you cannot turn off the error log
# setting error_log off; will simply create a file called 'off'.
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
location / {
# Rewrite rules and other criterias can go here
# Remember to avoid using if() where possible (http://wiki.nginx.org/IfIsEvil)
}
# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# We can include our basic configs here, as you can see its much easier
# than pasting out the entire sets of location block each time we add a vhost
include drop.conf;
include php.conf;
}
Wordpress Simple (Not using file-based caching or special rewrites)
This can also be used with W3 Total Cache if you're using Memcache or Opcode Caching.
server {
listen 80;
server_name example.com www.example.com;
root /usr/local/www/example.com;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
location / {
try_files $uri $uri/ /index.php;
}
location /search { limit_req zone=kbeezieone burst=3 nodelay; rewrite ^ /index.php; }
fastcgi_intercept_errors off;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
include php.conf;
# You may want to remove the robots line from drop to use a virtual robots.txt
# or create a drop_wp.conf tailored to the needs of the wordpress configuration
include drop.conf;
}
Wordpress w/ W3 Total Cache using Disk (Enhanced)
The following rules have been updated to work with W3TC 0.9.2.8, in some cases $host may need to be replaced with your domain name.
server {
listen 80;
server_name example.com www.example.com;
root /usr/local/www/example.com;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
# the next two location blocks are to ensure gzip encoding is turned off
# for the serving of already gzipped w3tc page cache
# normally gzip static would handle this but W3TC names them with _gzip
location ~ /wp-content/cache/page_enhanced.*html$ {
expires max;
charset utf-8;
add_header Vary "Accept-Encoding, Cookie";
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
expires max;
gzip off;
types {}
charset utf-8;
default_type text/html;
add_header Vary "Accept-Encoding, Cookie";
add_header Content-Encoding gzip;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / {
if (-f $request_filename) {
break;
}
set $w3tc_rewrite 1;
if ($request_method = POST) { set $w3tc_rewrite 0; }
if ($query_string != "") { set $w3tc_rewrite 0; }
set $w3tc_rewrite2 1;
if ($request_uri !~ \/$) { set $w3tc_rewrite2 0; }
if ($request_uri ~* "(sitemap(_index)?\.xml(\.gz)?|[a-z0-9_\-]+-sitemap([0-9]+)?\.xml(\.gz)?)") { set $w3tc_rewrite2 1; }
if ($w3tc_rewrite2 != 1) { set $w3tc_rewrite 0; }
if ($http_cookie ~* "(comment_author|wp\-postpass|wordpress_\[a\-f0\-9\]\+|wordpress_logged_in)") { set $w3tc_rewrite 0; }
if ($http_user_agent ~* "(W3\ Total\ Cache/0\.9\.2\.4)") { set $w3tc_rewrite 0; }
set $w3tc_ua "";
set $w3tc_ref "";
set $w3tc_ssl "";
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) { set $w3tc_enc _gzip; }
set $w3tc_ext "";
if (-f "$document_root/wp-content/cache/page_enhanced/$host/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl.html$w3tc_enc") {
set $w3tc_ext .html;
}
if ($w3tc_ext = "") { set $w3tc_rewrite 0; }
if ($w3tc_rewrite = 1) {
rewrite ^ "/wp-content/cache/page_enhanced/$host/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl$w3tc_ext$w3tc_enc" last;
}
if (!-e $request_filename) {
rewrite ^ /index.php last;
}
}
location /search { limit_req zone=kbeezieone burst=3 nodelay; rewrite ^ /index.php; }
fastcgi_intercept_errors off;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
include php.conf;
# You may want to remove the robots line from drop to use a virtual robots.txt
# or create a drop_wp.conf tailored to the needs of the wordpress configuration
include drop.conf;
}