Skip to content

Instantly share code, notes, and snippets.

@khoatran
Last active December 28, 2021 13:20
Show Gist options
  • Save khoatran/ae14e024053c14c574e46b6c9798de30 to your computer and use it in GitHub Desktop.
Save khoatran/ae14e024053c14c574e46b6c9798de30 to your computer and use it in GitHub Desktop.
Configuring Nginx to be a Cache for PHP-FPM
# This is the sample from Scaling PHP applications book
#Set the path where the cache is stored; Set the zone name(my_app), totalsize (100m),and max life time(60m)
fastcgi_cache_path /tmp/cachelevels=1:2keys_zone=my_app:100minactive=60m;
#Set the cache key used,in this case: httpsGETtest.com/somepage.html
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server{
listen 80;
root /u/apps/my_app;
location ~ \.php$ {
//fastcgi_pass config
# Use the zone name we defined on line 1
fastcgi_cache my_app;
# Only cache HTTP 200 responses (no error pages)
fastcgi_cache_valid 200 60m;
# Only enable for GET and HEAD requests (no POSTs)
fastcgi_cache_methods GET HEAD;
# Bypass the cache if the request contains HTTP Authorization
fastcgi_cache_bypass $http_authorization;
# Bypass the cache if the response contains HTTP Authorization
fastcgi_no_cache $http_authorization;
# Add a debugging header to show if the page came from cache
add_header X-Fastcgi-Cache $upstream_cache_status;
}
}
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{
listen 80;
charset utf-8;
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 ~ \.php$ {
# Use the zone name we defined on line 1
fastcgi_cache WORDPRESS;
# Only cache HTTP 200 responses (no error pages)
fastcgi_cache_valid 200 60m;
# Only enable for GET and HEAD requests (no POSTs)
fastcgi_cache_methods GET HEAD;
# Bypass the cache if the request contains HTTP Authorization
fastcgi_cache_bypass $http_authorization;
# Bypass the cache if the response contains HTTP Authorization
fastcgi_no_cache $http_authorization;
# Add a debugging header to show if the page came from cache
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment