Skip to content

Commit

Permalink
Improve Nginx's FastCGI caching
Browse files Browse the repository at this point in the history
  • Loading branch information
joglomedia committed Jul 14, 2024
1 parent 3eb0909 commit dbf06fd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
4 changes: 1 addition & 3 deletions etc/nginx/fastcgi_cache
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
## Include this file if you want to use fastcgi_cache across many sites.
# Designed to be included in /etc/nginx/nginx.conf http {} block.

fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=FASTCGICACHE:100m max_size=1g inactive=60m;
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=LEMPERCACHE:200m max_size=10g inactive=2h;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;
fastcgi_cache_revalidate on;
fastcgi_cache_background_update on;


# FastCGI Log Format
log_format cache '$remote_addr - $upstream_cache_status [$time_local] '
'"$request" $status $body_bytes_sent '
Expand Down
6 changes: 3 additions & 3 deletions etc/nginx/includes/fastcgi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 90;
fastcgi_read_timeout 90;
fastcgi_connect_timeout 120s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_buffer_size 128k;
fastcgi_buffers 128 128k;
fastcgi_busy_buffers_size 128k;
Expand Down
10 changes: 5 additions & 5 deletions etc/nginx/includes/fastcgi_cache.conf
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
## FastCGI cache.
# Designed to be included in any http, server, location block.

fastcgi_cache FASTCGICACHE;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache LEMPERCACHE;
fastcgi_cache_valid 200 301 302 2h;
fastcgi_cache_valid 404 1m;
fastcgi_cache_valid any 60m;
fastcgi_cache_valid any 2h;
fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;

fastcgi_cache_bypass $skip_cache $is_mobile;
fastcgi_cache_bypass $http_pragma $http_authorization;

fastcgi_no_cache $skip_cache $is_mobile;
fastcgi_no_cache $http_pragma $http_authorization;

fastcgi_cache_purge $purge_method;

# Ignore header (Added Pragma, crosscheck first)
fastcgi_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

# Header status
add_header X-FastCGI-Cache $upstream_cache_status;
Expand Down
6 changes: 3 additions & 3 deletions etc/nginx/includes/rules_fastcgi_cache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ if ($cookie_woocommerce_items_in_cart = "1") {
## Purging FastCGI cache ##

# Purge uri.
location ~ /purge(/.*) {
location ~ ^/(.*)/purge {
allow 127.0.0.1;
#allow SERVER_IP;
deny all;
access_log off;
log_not_found off;
fastcgi_cache_purge FASTCGICACHE "$scheme$request_method$host$1";
#return 301 $scheme://$host$1;
fastcgi_cache_purge LEMPERCACHE "$scheme$request_method$host$1";
return 301 $scheme://$host/$1;
}

## You may add your own FastCGI cache rules here...
8 changes: 4 additions & 4 deletions etc/nginx/includes/rules_restriction.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ location ~ /\.ht {
}

# Deny all attempts to access error and access log file.
location ~ /(error|access)\_log$
{
location ~ /(error|access)\_log$ {
deny all;
log_not_found off;
return 404;
Expand Down Expand Up @@ -93,12 +92,13 @@ location ~* /(?:uploads|files)/.*\.php$ {
}

# Deny access to PHP file in site files directory.
location ~ ^/sites/.*/private/ {
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
log_not_found off;
return 404;
}
location ~ ^/sites/[^/]+/files/.*\.php$ {

location ~ ^/sites/.*/private/ {
deny all;
log_not_found off;
return 404;
Expand Down
4 changes: 2 additions & 2 deletions etc/nginx/includes/rules_staticfiles.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ location ~ \.(?:atom|feed|rss|json)$ {
}

# Header setting for known static files.
location ~ \.(?:ogg|ogv|3gp|gif|jpeg|jpg|png|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|rtf|rtx|txt|pdf|iso)$ {
location ~ \.(?:gif|jpeg|jpg|png|webp|wmv|avi|asf|asx|ogg|ogv|3gp|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|rtf|rtx|txt|pdf|iso)$ {
add_header Pragma "public";
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
Expand All @@ -52,7 +52,7 @@ location ~ \.(eot|svg|svgz|ttf|woff|woff2)$ {
}

# Dynamic thumbnail/resized image.
location ~ (timthumb|thumbs|image|images|img)\.php?src=.*\.(jpg|jpeg|png|gif).* {
location ~ (timthumb|thumbs|image|images|img)\.php?src=.*\.(jpg|jpeg|png|gif|webp).* {
add_header Pragma "public";
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
Expand Down

0 comments on commit dbf06fd

Please sign in to comment.