Jump to content
Froxlor Forum
  • 0

Unable to Use Nginx Micro Cache


kingsley ezenwa

Question

Back in the days while doing Hostings without Control Panel, I was able to use NGINX with Micro Cache. Stopped laterafter moving on to ISPConfig 3 then FROXLOR .

I now want to resume that again BUT am unable to get it to work with FROXLOR.

Any idea how or why it isn't working would be greatly appreciated.

 

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

Finally Got to make NGINX Micro Cacheing Work   but am having a little issue making me ask this question

 

Normally, while creating vHosts for website without using FROXLOR or anyControl Panel, Nginx  php location block is 
 

location ~ \.php$ {}

 

BUT vHosts created by FROXLOR use this location block 

location @php$ {}

 

Wants to know if it's possible to change/swap these it even in the codes.

That is because on a Server without any Control Panel, Creating vHosts manually for NGINX , the FastCGI cache for NGINX get's added under the location ~ \.php$ {} block .

Currently, adding same code under the  location @php$ {} block causes an error - probably 404 error  OR Access Denied error

and While it currently works being added in the Server Block, the Cache refuses to refresh even after expiring .
When I look in cache directory, I can see new cache files created BUT still Old ones still gets served.

The only solution at the moment is to manually delete cache folder and restart NGINX after which the websites updates.

Link to comment
Share on other sites

Currently, NGINX FastCGI_cache (aka Micro Cache) works with FROXLOR but had to place the codes in Server block rather than within the  location ~ \.php$ {} block   OR   location @php$ {} block . 

And I want to assume you can't have more than 1 (one)    location php$ {} blocks or is that possible.

 

Also Is it even possible to place codes within the location ~ ^(.+?\.php)(/.*)?$ {    block

Also how is this file  207c8135c938fb25f460eac93b8d7fe3.htm  generated ?
Maybe I can look into it and do things in it directly.
Am almost there as it's working though not just updating it's self as it should. 


    location ~ ^(.+?\.php)(/.*)?$ {
        try_files /207c8135c938fb25f460eac93b8d7fe3.htm @php;
         <!--Can I place something here-->
    }

    
    location @php {
        try_files $1 = 404;
        
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)\$;
        fastcgi_param SCRIPT_FILENAME $document_root$1;
        fastcgi_param PATH_INFO $2;
        fastcgi_pass unix:/var/run/1-user-example.com-php-fpm.socket;
        fastcgi_index index.php;
    }

 


 

Link to comment
Share on other sites

Finally, on a Working Server in production with MicroCahe enabled and without any Control Panel like FROXLOR, below is how the PHP Location block looks like

 


          location ~ \.php$ {
                        #Cache everything by default
                        set $no_cache 0;
                        # If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
                        if ($request_method !~ ^(GET|HEAD)$) {
                          set $no_cache "1";
                        }
                        # Drop no cache cookie if need be
                        # (for some reason, add_header fails if included in prior if-block)
                        if ($no_cache = "1") {
                          #add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
                          #add_header X-Microcachable "0";
                        }
                        # Bypass cache if no-cache cookie is set
                        if ($http_cookie ~* "_mcnc") {
                          set $no_cache "1";
                        }
                        #Don't cache if the URL contains a query string
                        if ($query_string != ""){
                          set $no_cache 1;
                        }
                         # POST requests and urls with a query string should always go to PHP
                        if ($request_method = POST) {
                           set $no_cache 1;
                        }
                        # Don't use the cache for logged in users or recent commenters
                        if ($http_cookie ~* "comment_author|wordpress_no_cache|wordpress_logged_in") {
                            set $no_cache 1;
                        }
                        #Don't cache the following URLs
                        if ($request_uri ~* "/(wp-login.php|wp-admin|login.php|backend|admin)"){
                          set $no_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 $no_cache 1;
                        } 
                        #Don't cache if there is a cookie called PHPSESSID
                        if ($http_cookie ~* "PHPSESSID"){
                          set $no_cache 1;
                        }
                        #Don't cache if there is a cookie called wordpress_logged_in_[hash]
                        if ($http_cookie ~* "wordpress_logged_in_"){
                          set $no_cache 1;
                        }

                        # Bypass cache if flag is set
                        fastcgi_no_cache $no_cache;
                        fastcgi_cache_bypass $no_cache;
                        fastcgi_cache microcache;
                        fastcgi_cache_valid 404 30m;
                        fastcgi_cache_valid 200 301 302 304 20m;
                        fastcgi_max_temp_file_size 1500M;
                        fastcgi_cache_use_stale updating error timeout invalid_header http_500;
                        fastcgi_pass_header Set-Cookie;
                        fastcgi_pass_header Cookie;
                        
                        
                        fastcgi_index index.php;
                        fastcgi_pass localhost:9000;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_param  PATH_INFO          $fastcgi_path_info;
                        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;               
                        fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
                        #fastcgi_intercept_errors on;
                       include fastcgi_params; 
          }

.

Link to comment
Share on other sites

These are few suspects I think might be causing issues as they are totally different from normal vHosts without FROXLOR

1. fastcgi_pass localhost:9000; while FROXLOR generated vHost uses Sockets i.e fastcgi_pass unix:/var/run/1-user-example.com-php-fpm.socket;

2.  Normal vHost  .php location block  is   location ~ \.php$ { }  While FROXLOR generated .php block is   location ~ ^(.+?\.php)(/.*)?$ {   }   AND       location @php {  }

 

Link to comment
Share on other sites

1 minute ago, kingsley ezenwa said:

Hi

Please kindly respond as Am almost there.

Thanks

what do you want me to say? I repeat: This degree of customization cannot be handled by froxlor. Wordpress is not everything, there are other cms/boards/etc. who need special nginx configs...it's the way nginx handles these things that makes it really really hard to allow all possibilities through froxlor

Link to comment
Share on other sites

2 hours ago, d00p said:

"It's not working" is not a helpful message. What did you try? What changes are needed in the config fpr micro-cache to work? Are there any error-messages? etc.etc.etc.

I think it's better i paste here a short tutorial for you to understand as pasting what worked for me while I was not using Control Panel would be confusing for you.

 

The below should go into the concerned website's vHost file under the server block

    if (!-e $request_filename) {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }
    location ~ \.php$ {
        # Setup var defaults
        set $no_cache "";
        # If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1";
        }
        # Drop no cache cookie if need be
        # (for some reason, add_header fails if included in prior if-block)
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0";
        }
        # Bypass cache if no-cache cookie is set
        if ($http_cookie ~* "_mcnc") {
                    set $no_cache "1";
        }
        # Bypass cache if flag is set
        fastcgi_no_cache $no_cache;
        fastcgi_cache_bypass $no_cache;
        fastcgi_cache microcache;
        fastcgi_cache_key $server_name|$request_uri;
        fastcgi_cache_valid 404 30m;
        fastcgi_cache_valid 200 10s;
        fastcgi_max_temp_file_size 1M;
        fastcgi_cache_use_stale updating;
        fastcgi_pass localhost:9000;
        fastcgi_pass_header Set-Cookie;
        fastcgi_pass_header Cookie;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  PATH_INFO          $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
        #fastcgi_intercept_errors on;
        include fastcgi_params;
    }

 

The below is meant to go into nginx.conf file under the http { } block

fastcgi_cache_path /var/cache/nginx2 levels=1:2 keys_zone=microcache:5m max_size=1000m;
log_format cache '$remote_addr - $remote_user [$time_local] "$request" '
'$status $upstream_cache_status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

 

Above were copied and unmodified from a tutorial as it's leaner and less confusing for you.

Link to comment
Share on other sites

What I have tried

I added the below

 if (!-e $request_filename) {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }
    location ~ \.php$ {
        # Setup var defaults
        set $no_cache "";
        # If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1";
        }
        # Drop no cache cookie if need be
        # (for some reason, add_header fails if included in prior if-block)
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0";
        }
        # Bypass cache if no-cache cookie is set
        if ($http_cookie ~* "_mcnc") {
                    set $no_cache "1";
        }
        # Bypass cache if flag is set
        fastcgi_no_cache $no_cache;
        fastcgi_cache_bypass $no_cache;
        fastcgi_cache microcache;
        fastcgi_cache_key $server_name|$request_uri;
        fastcgi_cache_valid 404 30m;
        fastcgi_cache_valid 200 10s;
        fastcgi_max_temp_file_size 1M;
        fastcgi_cache_use_stale updating;
        fastcgi_pass localhost:9000;
        fastcgi_pass_header Set-Cookie;
        fastcgi_pass_header Cookie;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  PATH_INFO          $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
        #fastcgi_intercept_errors on;
        include fastcgi_params;
    }

into a file named microCache.conf and with the help of include 

e.g include /etc/nginx/microCache.conf;

i added it to FROXLOR's Default vHost-settings for NGINX so it gets included in all website's vHost files AND i can confirm the line   include /etc/nginx/microCache.conf;   is added in all vHosts files for websites.

But a look at this directory /var/cache/nginx2 shows it's empty even after visiting most of the websites so the files gets generated.

 

Another observation is that the file above adds another Location block  location ~ \.php$ {} while FROXLOR add a similar Location Block location @php {} .

Any how to append the content of the file above microCache.conf into the Location Block location @php {} added by   FROXLOR.

Have tried editing one of the website's vHost file to add it manually yet it refused to work.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...