Enable FCGI cache in Nginx
To enable fcgi cache in Nginx, add the following code outside the server block.
1 2 3 4 |
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
1 2 3 4 5 6 7 |
# 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
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; } |