Skip to content

Instantly share code, notes, and snippets.

@c3mdigital
Created November 16, 2012 05:39
Show Gist options
  • Save c3mdigital/4084475 to your computer and use it in GitHub Desktop.
Save c3mdigital/4084475 to your computer and use it in GitHub Desktop.
WordPress Nginx Multisite Configuration with Nginx Microcahing
/**
* Micro Cache Purge PHP Class
* Sends a request header to nginx to purge the microcache when post are created or updated
* Include this file at plugins_loaded or in theme functions.php
*/
new Micro_Cache_Purge();
class Micro_Cache_Purge {
public function __construct() {
add_action( 'save_post', array ( $this, 'transition_post_status' ) );
}
public function transition_post_status( $new, $old, $post ) {
if ( 'publish' !== $old && 'publish' !== $new )
return;
$post = get_post( $post );
$url = get_permalink( $post );
// Purge this URL
$this->purge( $url );
// Purge the front page
$this->purge( home_url( '/' ) );
// Add any additional urls to purge eg: /blog/
if ( 'post' === $post->post_type ) {
$this->purge( home_url( '/blog/' ) );
}
}
private function purge( $url ) {
wp_remote_get( $url, array ( 'timeout' => 0.01, 'blocking' => false, 'headers' => array ( 'X-Nginx-Cache-Purge' => '1' ) ) );
}
}
# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
log_not_found off;
access_log off;
}
## Comment out this block and uncomment the robots.txt block at bottom of the file to enable WordPress dynamic robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
## Uncomment out this block to use WordPress dynamic robots.txt
# location = /robots.txt {
# rewrite ^ /index.php;
# }
error_page 404 = @wordpress;
log_not_found off;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
set $cachetest "$document_root/wp-content/cache/ms-filemap/${host}${uri}";
if ($uri ~ /$) {
set $cachetest "";
}
if (-f $cachetest) {
# Rewrites the URI and stops rewrite processing so it doesn't start over and attempt to pass it to the next rule.
rewrite ^ /wp-content/cache/ms-filemap/${host}${uri} break;
}
location ^~ /files/ {
rewrite ^.*/files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
## If using W3 Total Cache include the conf file set in settings-general nginx conf file location
## Make sure WordPress can write to this file!
# include w3-total.conf;
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
location @wordpress {
fastcgi_pass php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}
location ~ \.php$ {
fastcgi_cache microcache_fpm;
fastcgi_cache_key $scheme$host$request_method$request_uri;
fastcgi_cache_valid 200 304 10m;
fastcgi_cache_use_stale updating;
fastcgi_max_temp_file_size 1M;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
set $no_cache_set 0;
set $no_cache_get 0;
if ( $http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $no_cache_set 1;
set $no_cache_get 1;
}
# If a request comes in with a X-Nginx-Cache-Purge: 1 header, do not grab from cache
# But note that we will still store to cache
# We use this to proactively update items in the cache!
if ( $http_x_nginx_cache_purge ) {
set $no_cache_get 1;
}
# For cached requests, tell client to hang on to them for 5 minutes
if ( $no_cache_set = 0 ) {
expires 5m;
}
# 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;
fastcgi_pass php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
try_files $uri @wordpress;
}
user www-data;
worker_processes 8;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 3;
client_max_body_size 13m;
index index.php index.html index.htm;
# Upstream to abstract backend connection(s) for PHP.
upstream php {
server unix:/var/run/php5-fpm.sock;
}
include sites-enabled/*;
}
## Microcache key_zone configuration.
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache_fpm:10m inactive=5m max_size=500m;
server {
## Using default_server directive and catchall server_name to route all request
## on port 80 to nginx. This allows nginx to let WordPress multisite handle domain mapping
listen 80 default_server;
server_name _;
root /srv/www/public;
index index.php index.html index.htm;
include global.restrictions.conf;
include global-wp-multi.conf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment