To enable fcgi cache in Nginx, add the following code outside the server block.
fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=SOKCACHE:60m 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;
Inside your location for .php files, above the proxy_pass, add
# cache start
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache SOKCACHE;
fastcgi_cache_valid 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
# cache end
Add below code above .php location block
# Cache start
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;
}
# Cache end
This is used to skip FCGI cache for logged-in users.
Example
fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=SOKCACHE:60m 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 {
listen 80;
listen 443 ssl http2;
ssl_certificate /etc/ssl/serverok.in.crt;
ssl_certificate_key /etc/ssl/serverok.in.key;
server_name serverok.in www.serverok.in;
root /home/serverok.in/html/;
index index.php index.html index.htm;
access_log /var/log/nginx/serverok.in.log;
error_log /var/log/nginx/serverok.in-error.log;
client_max_body_size 1000M;
proxy_read_timeout 600s;
fastcgi_read_timeout 600s;
fastcgi_send_timeout 600s;
# Rewrites for Yoast SEO XML Sitemap
rewrite ^/sitemap.xml$ /index.php?sitemap=1 last;
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
# Forbidden files or directories
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|\.env) {
return 404;
}
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 = /xmlrpc.php {
deny all;
access_log off;
log_not_found off;
}
# Cache start
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;
}
# Cache end
location /nginx_status {
stub_status;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:/run/php/php-fpm-serverok.sock;
# cache start
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache SOKCACHE;
fastcgi_cache_valid 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
# cache end
}
location ~* \.(txt|xml|js)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(css)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
expires max;
log_not_found off;
access_log off;
}
location /wp-content/themes/understrap-child/.git/ {
return 404;
}
gzip on;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/x-javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/svg+xml
image/x-icon
text/css
text/plain
text/x-component;
}
Leave a Reply