Skip to content

Instantly share code, notes, and snippets.

@yungke
Last active February 2, 2016 14:52
Show Gist options
  • Save yungke/5c1ef91fbc7b99453cde to your computer and use it in GitHub Desktop.
Save yungke/5c1ef91fbc7b99453cde to your computer and use it in GitHub Desktop.
Nginx fastcgi_cache 緩存加速設定
fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_path /dev/shm/life-cache levels=1:2 keys_zone=life.1798.in:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 302 2m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 2m;
fastcgi_cache_min_uses 1;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
fastcgi_cache life.1798.in;
fastcgi_cache_valid 200 302 2m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 2m;
fastcgi_cache_min_uses 1;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
fastcgi_cache_purge life.1798.in "$scheme$request_method$host$1";
}
[ ! -d '/dev/shm/nginx-cache' ] && { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; }
[ ! -d '/dev/shm/life-cache' ] && { mkdir /dev/shm/life-cache; chown -R ${user}.$user /dev/shm/life-cache; }
cd /root/oneinstack/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.9.10.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
tar xzf nginx-1.9.10.tar.gz
cd nginx-1.9.10
nginx -V #查看nginx編譯參數,最後加上--add-module=../ngx_cache_purge-2.3
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3
make
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #備份nginx
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 顯示ngx_cache_purge表示已經安裝成功
define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache');
# 另一個網站 (Life photo story) 也記的在 wp-config.php 添加
define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/life-cache');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment