Nginx fastcgi_cache And proxy_cache

FastCGI Cache Example

FastCGI Cache VS WP Super Cache

FastCGI cache is faster than WP Super Cache because the latter uses .htaccess and PHP itself to route the visitor to the cache (files). So before the visitors get to a hit on a cached page, WP Super Cache has to perform some logic both in the form of .htaccess (rewrites) and PHP itself (all WP plugins must use PHP). Whereas FastCGI uses compiled bindings that are fast and routes traffic directly to the cache.

Now, an HTTP cache like Varnish will always be faster than the two. That’s because you’re traveling one fewer hop down the stack to get to the data, which in the case of Varnish sits at the HTTP level as an HTML object and can be served hot.

So to illustrate:

So to get to the:

FastCGI cache: Nginx -> FastCGI -> File

WP Super Cache cache: Nginx -> FastCGI -> PHP -> File

通常你需要做的修改在这个文件 /etc/nginx/sites-available/default

#move next 4 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites 
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS: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;
server {

	access_log   /var/log/nginx/;
	error_log    /var/log/nginx/;

	root /var/www/;
	index index.php;

	set $skip_cache 0;

	# POST requests and urls with a query string should always go to PHP
	if ($request_method = POST) {
		set $skip_cache 1;
	if ($query_string != "") {
		set $skip_cache 1;

	# Don't cache uris containing the following segments
	if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
		set $skip_cache 1;

	# Don't use the cache for logged in users or recent commenters
	if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
		set $skip_cache 1;

	location / {
		try_files $uri $uri/ /index.php?$args;

	location ~ \.php$ {
		try_files $uri =404; 
		include fastcgi_params;

		fastcgi_cache_bypass $skip_cache;
	        fastcgi_no_cache $skip_cache;

		fastcgi_cache WORDPRESS;
		fastcgi_cache_valid  60m;

	location ~ /purge(/.*) {
	    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";

	location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
		access_log off;	log_not_found off; expires max;

	location = /robots.txt { access_log off; log_not_found off; }
	location ~ /\. { deny  all; access_log off; log_not_found off; }
}Code language: PHP (php)

Proxy Cache Example

# 申明缓存地址,名字和相关变量
proxy_cache_path /var/cache/nginx/microcache levels=1:2 keys_zone=microcache:10M max_size=10g inactive=2h use_temp_path=off;

server {
    listen       80;
    server_name  localhost;

    # proxy the PHP scripts to Apache listening on
    location / {
        proxy_cache_valid 200       1d;
        proxy_cache microcache;
        proxy_cache_lock on;
        proxy_cache_lock_timeout    10s;
        proxy_cache_valid 301 302   10m;
        proxy_cache_valid 404       10s;
        proxy_cache_min_uses 1;
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        proxy_ignore_headers Cache-Control Expires Set-Cookie;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X_FORWARDED_PROTO https;
        proxy_ssl_verify off;
        proxy_connect_timeout 200;
        proxy_send_timeout 300;
        proxy_read_timeout 300;

        add_header X-Cache-Status $upstream_cache_status;
        proxy_set_header Host;
        send_timeout 300;
}Code language: PHP (php)