Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active December 14, 2023 06:45
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save magnetikonline/10450786 to your computer and use it in GitHub Desktop.
Save magnetikonline/10450786 to your computer and use it in GitHub Desktop.
Nginx FastCGI cache configuration example.

Nginx FastCGI cache

Example /etc/nginx/nginx.conf using FastCGI (e.g. to PHP-FPM) with FastCGI cache enabled. This will capture returned data and persist it to a disk based cache store for a configurable amount of time, great for robust full page caching.

Will need to create a directory to hold cache files, for the example given here that would be:

$ sudo mkdir -p /var/cache/nginxfastcgi
$ chown www-data: /var/cache/nginxfastcgi

Ownership of the cache storage directory should be the same as the running Nginx user.

user www-data www-data;
worker_processes 1;
lock_file /run/lock/nginx.lock;
events {
worker_connections 1024;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 5;
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_static on;
gzip_comp_level 2;
gzip_disable "msie6";
gzip_proxied any;
gzip_types application/javascript application/json application/vnd.ms-fontobject application/x-font-ttf image/svg+xml text/css text/plain text/xml;
gzip_vary on;
fastcgi_cache_path /var/cache/nginxfastcgi levels=1:2 keys_zone=fastcgicache:10m inactive=10m max_size=64m;
fastcgi_cache_key $scheme$request_method$host$request_uri;
# note: can also use HTTP headers to form the cache key, e.g.
#fastcgi_cache_key $scheme$request_method$host$request_uri$http_x_custom_header;
fastcgi_cache_lock on;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_cache_valid 5m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
index index.php;
server {
listen 127.0.0.1:80;
server_name sitename.com;
root /var/www/sitename.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# example FastCGI cache exception rules
set $fastcgi_skipcache 0;
if ($query_string) {
set $fastcgi_skipcache 1;
}
if ($http_x_custom_header) {
set $fastcgi_skipcache 0;
}
if ($uri ~ "/path/matches/") {
set $fastcgi_skipcache 1;
}
if ($http_cookie ~ "users_login_cookie") {
set $fastcgi_skipcache 1;
}
include /etc/nginx/conf/phpfastcgicache;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ "\.php$" {
fastcgi_index index.php;
if (!-f $realpath_root$fastcgi_script_name) {
return 404;
}
# note: adds a HTTP response header "X-Cache" returning HIT/MISS/BYPASS/EXPIRED for cache use status
add_header X-Cache $upstream_cache_status;
fastcgi_cache fastcgicache;
fastcgi_cache_bypass $fastcgi_skipcache;
fastcgi_no_cache $fastcgi_skipcache;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php5/php-fpm.sock;
}
}
}
@zirikatzaile
Copy link

OK stupid question I did :)

I just needed serving a static directory so

    location ^~ /promocion {
            root   /var/www/myweb.com/web;
            expires     off;
            add_header Cache-Control "no-cache";       
    }  

was enough. No need of proxy neither fastcgi directives at all

@magnetikonline
Copy link
Author

@gothicx - sounds like you are running an older version of Nginx. With Nginx 1.6+ this should be all solid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment