Nginx FastCGI Cache Configuration for WordPress

Here is the Nginx Site Config Template for WordPress with Nginx FastCGI Cache.

Our Present site Powered by Nginx FastCGI and Redis

Here is My Server Setup Recommendation for WordPress

  • Server – Ubuntu 20.04 LTS
  • PHP 7.4
  • MYSQL 8
  • Nginx
  • Fastcgi Cache
  • Redis Object Cache

if we use Fastcgi Cache + Redis we no need install Extra Cache Plugins for Browser cache we add Nginx static cache rule on our Site Config

Soon I will Publish the Full Setup for Worpdress with the above Setup

OK come to our Configuration Part

  • Open the Nginx Configuration Folder
cd /etc/nginx
  • Open nginx.conf file  – Hide Nginx version and FastCGI cache Path & cache Lookup Key
  • Find the Line #server_tokens off;  uncomment yes remove # hash
  • Next http Block Add this Below to Enable Cache Directory and Key
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=wordpress:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
  • CTRL + X to save your Settings
  • Now open your Site Configuration File and add the Below Nginx Configuration
cd etc/nginx/sites-available
  • open your example.conf file  – PS: For SSL use Certbot

example.conf

server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;

root /var/www/example;
index index.php index.html index.htm;

charset UTF-8;

if ($scheme = http) {
return 301 https://$server_name$request_uri;
}

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name example.com www.example.com;
root /var/www/example;
index index.php index.html index.htm;

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

charset UTF-8;

#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /etc/nginx/error;
}

if ($scheme = http) {
return 301 https://$server_name$request_uri;
}

if ($host = www.$server_name) {
rewrite ^(.*) https://$server_name$request_uri? permanent;
}

location / {
#try_files $uri $uri/ /index.php?q=$uri&$args;
try_files $uri $uri/ /index.php?$args;
}

# cache by default
set $skip_cache 0;
# set default empty skip reason
set $skip_reason "";

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
set $skip_reason "${skip_reason}-POST";
}
if ($query_string != "") {
set $skip_cache 1;
set $skip_reason "${skip_reason}-QueryString";
}
# 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;
set $skip_reason "${skip_reason}-URI";
}
# 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;
set $skip_reason "${skip_reason}-Cookie";
}

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_cache wordpress;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
#fastcgi_cache_valid 200 301 302 404 1m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_valid 60m;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_no_cache $skip_cache;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache_bypass $http_secret_header $skip_cache;
add_header SanWeb-Fastcgi-Cache $upstream_cache_status;
add_header Sanweb-Skip $skip_reason;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}

client_max_body_size 50m;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location ~* \.(png|jpg|jpeg|gif|svg|ico)$ {
expires 365d;
log_not_found off;
access_log off;
}

location ~* \.(css|js|swf)$ {
expires 100d;
log_not_found off;
access_log off;
}

location ~* \.(ttf|woff|woff2|pdf)$ {
expires 365d;
log_not_found off;
access_log off;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
try_files $uri /index.php?$args;
}

location ~ /\.|wp-config\.php {
deny all;
access_log off;
log_not_found off; 
}

location = /xmlrpc.php {
deny all;
}

}
  • Replace example.com with your URL
  • After Configuration reload and restart Nginx
  • Verify the FastCGI cache

Attempt 1

sanweb-fastcgi-cache: MISS – Open the website first time

curl -I https://example.com

HTTP/2 200 
server: nginx
date: Sat, 30 May 2020 10:18:34 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-ua-compatible: IE=edge
sanweb-fastcgi-cache: MISS
strict-transport-security: max-age=63072000; includeSubDomains; preload
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff

Attempt 2

sanweb-fastcgi-cache: HIT – The Page was cached via Nginx FastCGI cache (just reload the website)

curl -I https://example.com

HTTP/2 200 
server: nginx
date: Sat, 30 May 2020 10:20:39 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-ua-compatible: IE=edge
sanweb-fastcgi-cache: HIT
strict-transport-security: max-age=63072000; includeSubDomains; preload
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff

I hope this post will Help your Configure Nginx FastCGI cache for your WordPress Site.

 

by Santhosh Veer
Blogger - Web developer - Open Source Lover

Leave a Comment