Skip to content

Instantly share code, notes, and snippets.

@mintindeed
Created May 29, 2013 20:03
Show Gist options
  • Save mintindeed/5673393 to your computer and use it in GitHub Desktop.
Save mintindeed/5673393 to your computer and use it in GitHub Desktop.
Example nginx 0.9.x config for accessing memcached caches directly
index index.php index.html index.htm;
upstream phpfcgi {
server 127.0.0.1:9000;
}
upstream memcached {
server example.compute-1.amazonaws.com:11211 max_fails=1 fail_timeout=2s;
server example.compute-1.amazonaws.com:11211 max_fails=1 fail_timeout=2s;
}
upstream wordpressfcgi {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 128M;
client_body_buffer_size 128k;
root /var/www/sites/www.example.com/htdocs;
## Set the real IP.
proxy_set_header True-Client-IP $http_true_client_ip; # Akamai
proxy_set_header X-Real-IP $remote_addr;
## Set the hostname
proxy_set_header Host $host;
## Set the forwarded-for header.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
## Default location
location / {
## Default type is plain text, need to make it html so that any cache hits by nginx are served as html
default_type text/html;
set $can_cache 1;
# Bypass memcached for POST requests
if ( $request_method = "POST" ) {
set $can_cache 0;
}
if ( $request_uri ~ ^/wp-.*\.php ) {
set $can_cache 0;
}
# If logged in, don't cache.
if ($http_cookie ~* "loggedinuser|uncached|comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $can_cache 0;
}
# Tell memcached when to try another server
memcached_next_upstream not_found invalid_response error timeout;
# Pull from memcached pool if possible
if ( $can_cache = "1" ) {
# Set the default memcached key using the host + full request uri (including querystring)
set $memcached_key $http_host$request_uri;
# Pass the request to memcached
memcached_pass memcached;
# Pass any misses or fails to wordpress
error_page 404 502 504 = @fallback;
}
if ( $can_cache = "0" ) {
# Pass any misses to wordpress
proxy_pass http://wordpressfcgi;
}
}
## Define fallback location for memcache misses
location @fallback {
proxy_pass http://wordpressfcgi;
}
## Images and static content is treated different
location ~* ^.+.(ico|jpg|png|css|js|gif|jpeg|swf|htm|html)$ {
# access_log off;
expires 30d;
}
## Don't log requests for favicon
location = /favicon.ico {
log_not_found off;
access_log off;
expires 30d;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
server {
listen 8000;
server_name www.example.com;
client_max_body_size 128M;
client_body_buffer_size 128k;
root /var/www/sites/www.example.com/htdocs;
# Check if files/directory exist, if not rewrite to index.php
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Redirect robots.txt requests to dynamic robots.txt.php file
rewrite ^/robots\.txt$ /robots.txt.php last;
location ~ \.php$ {
fastcgi_pass phpfcgi;
include fastcgi_params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment