Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Created March 26, 2012 20:06
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save markjaquith/e567c81060178663b238 to your computer and use it in GitHub Desktop.
Save markjaquith/e567c81060178663b238 to your computer and use it in GitHub Desktop.
Nginx host config
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/access.log main;
root /srv/www/example.com;
index index.html index.htm index.php;
location ~ \.(jpe?g|gif|png|css|bmp|js|ico)$ {
expires 30d;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
location ~ /\.ht[a-z]+$ {
deny all;
}
location ~ \.php$ {
# Set some proxy cache stuff
fastcgi_cache microcache_fpm;
fastcgi_cache_key $scheme$host$request_method$request_uri;
fastcgi_cache_valid 200 5s;
fastcgi_cache_use_stale updating;
fastcgi_max_temp_file_size 1M;
fastcgi_cache_min_uses 3; # Hit a URL 3 times before caching it
set $no_cache_set 0;
set $no_cache_get 0;
set $temp_caching_exemption 0;
if ($request_method !~ ^(GET|HEAD)$) {
set $temp_caching_exemption 1;
}
if ( $temp_caching_exemption = 1 ) {
add_header Set-Cookie "_mcnc=1; Max-Age=10; Path=/";
}
# Bypass cache if no-cache cookie is set
if ( $http_cookie ~* "_mcnc" ) {
set $no_cache_set 1;
set $no_cache_get 1;
}
if ( $http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $no_cache_set 1;
set $no_cache_get 1;
}
# fastcgi_no_cache means "Do not store this proxy response in the cache"
fastcgi_no_cache $no_cache_set;
# fastcgi_cache_bypass means "Do not look in the cache for this request"
fastcgi_cache_bypass $no_cache_get;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
try_files $uri =404;
fastcgi_pass phpfpm;
}
}
# Obviously not the whole thing, but have this somewhere in the http{} block
upstream phpfpm {
server unix:/var/run/php5-fpm.sock;
}
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache_fpm:30m max_size=1000m;
@dangayle
Copy link

Thanks for sharing this. It's almost the same as my config that I set up for wordpress. With even a 5s/10s microcache, the amount of hits an nginx/php-fpm/apc/wordpress site can handle is phenomenal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment