[solved] New Directory for fastCGI Cache

Discussion in 'General' started by yasine, Jun 15, 2024.

  1. yasine

    yasine Member

    Yes, and my custom config is written successfully in the newly generated vhost file ( there is no demo.kodingschools.com.vhost.err ) upload_2024-6-20_16-17-44.png

    and the content is like so ( /etc/nginx/sites-available# cat demo.kodingschools.com.vhost ):

    Code:
    fastcgi_cache_path /var/www/clients/client0/web4/web/cache levels=1:2 keys_zone=demo.kodingschools.com:200m max_size=10g inactive=2h use_temp_path=off;
    
    server {
            listen *:80;
            listen [::]:80;
            listen *:443 ssl http2;
    
        ssl_protocols TLSv1.3 TLSv1.2;
            listen [::]:443 ssl http2;
            ssl_certificate /var/www/clients/client0/web4/ssl/demo.kodingschools.com-le.crt;
            ssl_certificate_key /var/www/clients/client0/web4/ssl/demo.kodingschools.com-le.key;
    
            server_name demo.kodingschools.com www.demo.kodingschools.com;
    
            root   /var/www/demo.kodingschools.com/web/;
            disable_symlinks if_not_owner from=$document_root;
    
            if ($scheme != "https") {
                rewrite ^(?!/\.well-known/acme-challenge)/ https://$http_host$request_uri? permanent;
            }
    
            index index.html index.htm index.php index.cgi index.pl index.xhtml standard_index.html;
    
            location ~ \.shtml$ {
                ssi on;
            }
    
            error_page 400 /error/400.html;
            error_page 401 /error/401.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 405 /error/405.html;
            error_page 500 /error/500.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            recursive_error_pages on;
            location = /error/400.html {
    
                internal;
                auth_basic off;
            }
            location = /error/401.html {
    
                internal;
                auth_basic off;
            }
            location = /error/403.html {
    
                internal;
                auth_basic off;
            }
            location = /error/404.html {
    
                internal;
                auth_basic off;
            }
            location = /error/405.html {
    
                internal;
                auth_basic off;
            }
            location = /error/500.html {
    
                internal;
                auth_basic off;
            }
            location = /error/502.html {
    
                internal;
                auth_basic off;
            }
            location = /error/503.html {
    
                internal;
                auth_basic off;
            }
    
            error_log /var/log/ispconfig/httpd/demo.kodingschools.com/error.log;
            access_log /var/log/ispconfig/httpd/demo.kodingschools.com/access.log combined;
    
            location ~ /\. {
                deny all;
            }
    
            location ^~ /.well-known/acme-challenge/ {
                access_log off;
                log_not_found off;
                auth_basic off;
                root /usr/local/ispconfig/interface/acme/;
                autoindex off;
                index index.html;
                try_files $uri $uri/ =404;
            }
    
            location = /favicon.ico {
                log_not_found off;
                access_log off;
                expires max;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            }
    
            location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
            }
    
            location /stats/ {
    
                index index.html index.php;
                auth_basic "Members Only";
                auth_basic_user_file /var/www/clients/client0/web4/web//stats/.htpasswd_stats;
                add_header Content-Security-Policy "default-src * 'self' 'unsafe-inline' 'unsafe-eval' data:;";
            }
    
            location ^~ /awstats-icon {
                alias /usr/share/awstats/icon;
            }
    
            location ~ \.php$ {
                try_files /6f38aa6730bb8152122d53359ec2bfc2.htm @php;
                fastcgi_cache demo.kodingschools.com;
                fastcgi_cache_valid 120m;
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
            }
    
            location @php {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
                fastcgi_index index.php;
                fastcgi_param DOCUMENT_ROOT /web;
                fastcgi_param HOME /web;
                fastcgi_param SCRIPT_FILENAME /web$fastcgi_script_name;
                fastcgi_intercept_errors on;
            }
    
    
    
    
            client_max_body_size 64M;
    
            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/*|/tag/.*/feed/*|index.php|/.*sitemap.*\.(xml|xsl)") {
                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;
            }
    
            location / {
                index index.php index.html index.htm;
                try_files $uri $uri/ =404;
            }
    
    
    
    }
    the response headers for https://demo.kodingschools.com :

    upload_2024-6-20_16-35-14.png
     
    Last edited: Jun 20, 2024
  2. yasine

    yasine Member

    I found the problem :
    fastcgi_cache_path is only available under the http context of an Nginx configuration file
    so it won't work if we just put it outside the server bloc, so I'm wondering if it's possible to put it into the global template here /usr/local/ispconfig/server/conf-custom/nginx_vhost.conf.master, otherwise , the only solution would be putting this directive under http block here : /etc/nginx/nginx.conf, the only problem is we can not work with placeholders here , like in the global template here :
    Code:
    fastcgi_cache_path <tmpl_var name='web_document_root'>cache levels=1:2 keys_zone=<tmpl_var name='domain'>:200m max_size=10g inactive=2h use_temp_path=off;
     
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    Are you sure that this is the cause of the problem? No tutorial I found on that topic puts this into http block and even the example from nginx website does not put this into the http {} block. They all place it just in front of the server {} block and on the same level as the server block.

    Code:
    fastcgi_cache_path /data/nginx/cache keys_zone=cache_zone:10m;
    
    map $request_method $purge_method {
        PURGE   1;
        default 0;
    }
    
    server {
        ...
        location / {
            fastcgi_pass        backend;
            fastcgi_cache       cache_zone;
            fastcgi_cache_key   $uri;
            fastcgi_cache_purge $purge_method;
        }
    }
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    Maybe your problem is just that /var/www/clients/client0/web4/web/cache is not writable by nginx as nginx itself runs as www-data user and group. Have you tested that www-data user can write in that folder? Also, I don't think that its good to put the fastcgi cache into a folder in web root, better put it in website temp folder or so.
     
  5. yasine

    yasine Member

    indeed, here from nginx website : https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_path , they confirm that the context is http : upload_2024-6-21_16-39-18.png

    also from this tutorial : Set Up Nginx FastCGI Cache to Reduce WordPress Server Response Time - LinuxBabe ,
    the question has been asked here as well : nginx - Is it possible to have a fastcgi_cache_path for each website / virtual host? - Server Fault

    just tested , the folder is writable by nginx ,I switched to to www-data User and created testfile inside that directory and it's created

    upload_2024-6-21_16-57-1.png

    I agree that is not best practice
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    On the same page, they show an example where it is not in that context, plus most tutorials do not put it there and I assume at least some of them have tested the setup. So have you tested it to ensure that it works after you put it there instead of putting it just in front of the server block? Because your issue can be caused by many things and the only way to conclude that it does not work in front of the server block is by moving the code lines into the http {} block and restart nginx. if it works then, then we know it must be put there. if it still does not work, then we know that the http {} block is not the cause of the issue.
     
  7. yasine

    yasine Member

    you're right, I've moved fastcgi_cache_path into the http {} block
    upload_2024-6-21_18-2-33.png
    , update the cache_key inside the location server block like so : upload_2024-6-21_18-0-18.png
    and restarted nginx and did not work upload_2024-6-21_17-58-31.png
     
  8. yasine

    yasine Member

    it works only after I merge the config with location @php s=block ,

    Code:
    location @php { ##merge##
        fastcgi_cache {DOMAIN};
        fastcgi_cache_valid 200 301 302 2h;
        fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        add_header X-FastCGI-Cache $upstream_cache_status;
    }
    but I don't know if this is the correct way to do it , here is the entire generated vhost config :

    Code:
    fastcgi_cache_path /var/www/clients/client0/web4/web/cache levels=1:2 keys_zone=demo.kodingschools.com:200m max_size=10g inactive=2h use_temp_path=off;
    
    server {
            listen *:80;
            listen [::]:80;
            listen *:443 ssl http2;
    
        ssl_protocols TLSv1.3 TLSv1.2;
            listen [::]:443 ssl http2;
            ssl_certificate /var/www/clients/client0/web4/ssl/demo.kodingschools.com-le.crt;
            ssl_certificate_key /var/www/clients/client0/web4/ssl/demo.kodingschools.com-le.key;
    
            server_name demo.kodingschools.com www.demo.kodingschools.com;
    
            root   /var/www/demo.kodingschools.com/web/;
            disable_symlinks if_not_owner from=$document_root;
    
            if ($scheme != "https") {
                rewrite ^(?!/\.well-known/acme-challenge)/ https://$http_host$request_uri? permanent;
            }
    
            index index.html index.htm index.php index.cgi index.pl index.xhtml standard_index.html;
    
            location ~ \.shtml$ {
                ssi on;
            }
    
            error_page 400 /error/400.html;
            error_page 401 /error/401.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 405 /error/405.html;
            error_page 500 /error/500.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            recursive_error_pages on;
            location = /error/400.html {
    
                internal;
                auth_basic off;
            }
            location = /error/401.html {
    
                internal;
                auth_basic off;
            }
            location = /error/403.html {
    
                internal;
                auth_basic off;
            }
            location = /error/404.html {
    
                internal;
                auth_basic off;
            }
            location = /error/405.html {
    
                internal;
                auth_basic off;
            }
            location = /error/500.html {
    
                internal;
                auth_basic off;
            }
            location = /error/502.html {
    
                internal;
                auth_basic off;
            }
            location = /error/503.html {
    
                internal;
                auth_basic off;
            }
    
            error_log /var/log/ispconfig/httpd/demo.kodingschools.com/error.log;
            access_log /var/log/ispconfig/httpd/demo.kodingschools.com/access.log combined;
    
            location ~ /\. {
                deny all;
            }
    
            location ^~ /.well-known/acme-challenge/ {
                access_log off;
                log_not_found off;
                auth_basic off;
                root /usr/local/ispconfig/interface/acme/;
                autoindex off;
                index index.html;
                try_files $uri $uri/ =404;
            }
    
            location = /favicon.ico {
                log_not_found off;
                access_log off;
                expires max;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            }
    
            location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
            }
    
            location /stats/ {
    
                index index.html index.php;
                auth_basic "Members Only";
                auth_basic_user_file /var/www/clients/client0/web4/web//stats/.htpasswd_stats;
                add_header Content-Security-Policy "default-src * 'self' 'unsafe-inline' 'unsafe-eval' data:;";
            }
    
            location ^~ /awstats-icon {
                alias /usr/share/awstats/icon;
            }
    
            location ~ \.php$ {
                try_files /b83d2ba881a25df8d8c3eb0e8fdcb707.htm @php;
            }
    
            location @php {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
                fastcgi_index index.php;
                fastcgi_param DOCUMENT_ROOT /web;
                fastcgi_param HOME /web;
                fastcgi_param SCRIPT_FILENAME /web$fastcgi_script_name;
                fastcgi_intercept_errors on;
                fastcgi_cache demo.kodingschools.com;
                fastcgi_cache_valid 200 301 302 2h;
                fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
                fastcgi_cache_min_uses 1;
                fastcgi_cache_lock on;
                fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-FastCGI-Cache $upstream_cache_status;
            }
    
    
    
    
            client_max_body_size 64M;
    
            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/*|/tag/.*/feed/*|index.php|/.*sitemap.*\.(xml|xsl)") {
                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;
            }
    
            location / {
                index index.php index.html index.htm;
                try_files $uri $uri/ =404;
            }
    
    
    
    }
    upload_2024-6-21_20-41-15.png
     
    Last edited: Jun 21, 2024
    till likes this.
  9. till

    till Super Moderator Staff Member ISPConfig Developer

    If it works, then it should be fine.
     
    yasine likes this.

Share This Page