Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active December 14, 2023 06:45
Show Gist options
  • Save magnetikonline/10450786 to your computer and use it in GitHub Desktop.
Save magnetikonline/10450786 to your computer and use it in GitHub Desktop.

Revisions

  1. magnetikonline revised this gist Apr 4, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Nginx FastCGI cache

    Example `/etc/nginx/nginx.conf` using FastCGI (e.g. to PHP-FPM) with [FastCGI cache](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#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:
  2. magnetikonline revised this gist Nov 10, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@ http {
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;
    keepalive_timeout 5;

    include /etc/nginx/mime.types;
  3. magnetikonline revised this gist Sep 18, 2014. 1 changed file with 19 additions and 22 deletions.
    41 changes: 19 additions & 22 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    user www-data www-data;
    worker_processes 1;

    pid /run/nginx.pid;
    lock_file /run/lock/nginx.lock;


    @@ -12,23 +10,22 @@ events {

    http {
    server_tokens off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;

    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.
    @@ -37,56 +34,56 @@ http {
    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;
    }
  4. magnetikonline revised this gist Sep 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,4 @@ $ 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` defined in `nginx.conf`.
    Ownership of the cache storage directory should be the same as the running Nginx `user`.
  5. magnetikonline revised this gist Aug 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,7 @@ http {

    location ~ "\.php$" {
    fastcgi_index index.php;
    if (!-f $document_root$fastcgi_script_name) {
    if (!-f $realpath_root$fastcgi_script_name) {
    return 404;
    }

  6. Peter Mescalchin revised this gist Apr 25, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,6 @@ http {

    # 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;
  7. Peter Mescalchin revised this gist Apr 25, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,10 @@ http {
    gzip_comp_level 2;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_types application/json application/vnd.ms-fontobject application/x-font-ttf application/x-javascript image/svg+xml text/css text/plain text/xml;
    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;
    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;
  8. Peter Mescalchin revised this gist Apr 23, 2014. 2 changed files with 4 additions and 2 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,5 @@ Will need to create a directory to hold cache files, for the example given here
    $ 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` defined in `nginx.conf`.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -33,8 +33,9 @@ http {
    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_use_stale error timeout invalid_header updating http_500;
    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;
    @@ -84,7 +85,6 @@ http {
    add_header X-Cache $upstream_cache_status;

    fastcgi_cache fastcgicache;
    fastcgi_cache_valid 5m;
    fastcgi_cache_bypass $fastcgi_skipcache;
    fastcgi_no_cache $fastcgi_skipcache;

  9. Peter Mescalchin revised this gist Apr 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ http {
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # example cache exceptions/skipping
    # example FastCGI cache exception rules
    set $fastcgi_skipcache 0;

    if ($query_string) {
  10. Peter Mescalchin revised this gist Apr 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -60,7 +60,7 @@ http {
    set $fastcgi_skipcache 0;
    }

    if ($request_uri ~ "/path/matches/") {
    if ($uri ~ "/path/matches/") {
    set $fastcgi_skipcache 1;
    }

  11. Peter Mescalchin created this gist Apr 11, 2014.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Nginx FastCGI cache
    Example `/etc/nginx/nginx.conf` using FastCGI (e.g. to PHP-FPM) with [FastCGI cache](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#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:

    ```sh
    $ sudo mkdir -p /var/cache/nginxfastcgi
    $ chown www-data: /var/cache/nginxfastcgi
    ```
    95 changes: 95 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    user www-data www-data;
    worker_processes 1;

    pid /run/nginx.pid;
    lock_file /run/lock/nginx.lock;


    events {
    worker_connections 1024;
    }


    http {
    server_tokens off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;

    keepalive_timeout 5;

    gzip on;
    gzip_static on;
    gzip_comp_level 2;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_types application/json application/vnd.ms-fontobject application/x-font-ttf application/x-javascript 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;
    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_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_lock on;
    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 cache exceptions/skipping
    set $fastcgi_skipcache 0;

    if ($query_string) {
    set $fastcgi_skipcache 1;
    }

    if ($http_x_custom_header) {
    set $fastcgi_skipcache 0;
    }

    if ($request_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 $document_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_valid 5m;
    fastcgi_cache_bypass $fastcgi_skipcache;
    fastcgi_no_cache $fastcgi_skipcache;

    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/run/php5/php-fpm.sock;
    }
    }
    }