Every new hosting plan ordered during MAR2019 can request free DNSSEC setup ($39 value). Use coupon MAR2019 for free DNSSEC ($39 value).
Dominate local SEO with a regional SlickStack cloud server for just $39/month! Order Now
250,000+ sites use our plugins! Become A Member

How to Setup FastCGI Caching on Nginx Servers

Jesse Nickles   |  21 Jan, 2017

A few years back, it had already become apparent that Nginx was the brave new king of web servers, due to its impressive simplicity and ability to scale with ease. One of its most attractive features, however, was its ability to act as a “reverse proxy” either in front of an Apache server, or even just completely on its own.

The only issue that really remained was whether FastCGI caching or true “proxy” caching (via proxy_pass) was the way forward, so naturally, fanboys on both sides duked it out for a few years there (okay, not really). TL;DR: FastCGI is meant to be an internal protocol, while proxy_pass is just the HTTP protocol being, well, sort of extended on the backend.

Now, it is safe to say beyond a doubt that FastCGI has won out, and for good reason…

First, if you already installed Nginx, you should remove it:

sudo apt-get update
sudo apt-get remove nginx*

Note: A fresh install of nginx-extras is always the better approach whenever possible.

Next, we will install the nginx-extras version of Nginx, and make sure its on the mainline (development) branch for newest features and compatibility (i.e. HTTP/2, etc).

sudo apt-get update
sudo add-apt-repository ppa:nginx/development
sudo apt-get update
sudo apt-get install nginx-extras
sudo apt-get update
sudo apt-get upgrade

Note: Make sure that your nginx.conf file has the below line before proceeding:

include /etc/nginx/modules-enabled/*.conf;

Now it’s time to edit your server block:

sudo nano /etc/nginx/sites-available/default

Include the following configuration in your Nginx server block:

#### below lines must be outside server blocks to enable FastCGI cache for Nginx
#### path can be anywhere, app name must be consistent, total size small enough to avoid RAM depletion
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=WORDPRESS:128m inactive=30m;
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;
add_header X-FastCGI-Cache $upstream_cache_status;

## this block redirects all HTTP non-www to HTTP www version
server {
    server_name            www.cyber-breeze.com;
    listen                 *:80;
    listen                 [::]:80 ipv6only=on;
    return 301 http://cyber-breeze.com$request_uri;
}

## this block is the default configuration for the live website
server {
    server_name            cyber-breeze.com;
    listen                 *:80 default_server;
    listen                 [::]:80;
    root                   /home/cyber/www;
    index index.php;
    autoindex off;

    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/|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;
    }

   ## make sure PHP loads via FCGI for better performance
    location ~ \.php$ {
        try_files $uri =404;
        #### or use 'include fastcgi_params' for older Nginx versions
        include fastcgi.conf;
        #### substitute the socket, or address and port, of your WordPress server i.e. fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid 200 301 302 404 60m;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
    }

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

    #### add more: http://mirrors.littlebizzy.com/nginx/nginx-server-block-ssl.txt

}

https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps
https://www.nginx.com/blog/9-tips-for-improving-wordpress-performance-with-nginx/#cache-dynamic
https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/
https://www.howtoforge.com/why-you-should-always-use-nginx-with-microcaching
https://www.linuxbabe.com/nginx/setup-nginx-fastcgi-cache
https://www.scalescale.com/tips/nginx/configure-nginx-fastcgi-cache/

Shout out to Jesin A. for his helpful discussions with me that improved this article.

Tags: , , ,

Last modified: 11 Mar, 2017https://www.littlebizzy.com/?p=15453

Results For Local Business, High Traffic, And E-Commerce.

Local Business

"After being hosted on GoDaddy for years, I didn't realize how negatively it was impacting my search traffic. Soon after moving to LittleBizzy, my homepage went from page 3 on Google to #1 world-wide for my target market, and I also reached the top 3 on Google Maps, with no additional SEO work."

Juliette S.

High Traffic

"Before moving to LittleBizzy, whenever our news website was featured on the Drudge Report, it often slowed to a crawl or even froze up during big traffic spikes. Now, that never happens anymore, and we've been able to focus on publishing more articles instead of worrying about our web hosting."

Tony H.

E-Commerce

"The research by Amazon is definitely true, because our slow WooCommerce store was bleeding sales. After LittleBizzy stabilized our performance and moved us closer to our target customers, we saw a measurable improvement in shopping cart checkouts, esp. during holidays... much better!"

Mohammed H.

No contracts, free migration, and free SSL forever. What are you waiting for? Order Hosting Now.

WordPress Gossip, Technical SEO News, And Other Goodies.

Free. Unsubscribe anytime.

Brands That Trust Our Software: