Jump to content

Nginx | FastCgi Cache caching issues| Is a Unique cache for each auth customer possible?


Recommended Posts

I apologize in advance if somethings aren't clear, I'm learning how to edit server settings as I go so I'm not versed on how to put a lot of things. If you have any questions or need clarity anywhere just ask and I'll do my best to provide the details needed to get this issue resolved for myself or anyone else in the community that may have/will come across this problem.
 
From testing I have confirmed that cache is the root cause of this problem as I have deactivated it on one site on the server and the problem goes away and the other still has it.
 
My sites are loading in at incredible speeds compared to how they were before adding the cache settings, but the cache is caching user information, keeping the cart from saving, and disabling my instant search results. 
 
For example I can log in with my test account and try to logout it just refreshes the page and shows my user information still at the top.
 
If I add something to my cart and then go to the cart it will be empty or if I add from the home page and then go to the cart and back again my cart will show as 0 again.
 
I've seen that when you're running just Apache you can add:  "ExpiresByType text/html "access plus 0 minutes"" to get rid of the caching issues that are similar to this.
 
I have added the below code to stop the admin/order/account/login pages from being cached:
 
set $no_cache 0;
 
if ($request_uri ~* "/(admin8909/|order|my-account|login|cart|order)")
{
  set $no_cache 1;
}

 

With this in place I am able go to the login page > login and be directed to my-account > from my-account page can I can actually logout and be directed back to login screen...every other page is just a refresh of the same page, but if I hit logout from those pages and try to go back to my-account I am in fact logged out. I am also able to travel to every page except the ones that I visited before I logged in and be logged in.

 

Is there a way to have ajax bypassed by the caching and have user sessions, cart, and search results all passed via ajax to verify the current user?

 

PM Me and I can give you a site link. I don't mind experimenting and breaking things to get this figured out as this is a fresh install, I just need it fixed so that my other live site can benefit from the speed and I can then get this site online as well.

 

Link to comment
Share on other sites

Hey Daniel,

 

Thanks for the advice, I read up on that feature and the docs say that it is for when you're running on an apache server and I'm on nginx.

 

I found an article about a similar problem on a wordpress site and the suggested solution was to add the cookie to the fastcgi_cache_key but I haven't been able to get that to work.

 

Here's the article: http://community.rtcamp.com/t/prevent-fastcgi-cache-from-caching-specific-functions-or-wp-shortcodes/2506

Link to comment
Share on other sites

# here you select your cache path and the name (MYAPP) to id your cache
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
# here you select the key to id. your cache elements.
fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_state";
server {
    # virtualhost settings...
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name localhost;

    # directory and logs settings...
    root /var/www/yourhost.com/public_html;
    access_log /var/www/yourhost.com/logs/access.log;
    error_log /var/www/yourhost.com/logs/error.log;
    index index.php;
    
    # maybe you want to add http authentication
    #auth_basic "Restricted";
    #auth_basic_user_file /etc/nginx/.htpasswd;

    ##
    # Prestashop Rewrites
    ##
    rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
    rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$1$2.jpg last;
    rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
    rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
    rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
    rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
    try_files $uri $uri/ /index.php?$args;
    
    # cache with nginx the static files
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 30d;
        access_log off;
        add_header Cache-Control "public";
    }
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
        expires -1;
        # access_log logs/static.log; # I don't usually include a static log
    }

    # cache with nginx more static files
    location ~* \.(?:css|js)$ {
        expires max;
        access_log off;
        add_header Cache-Control "public";
    }
    # and here fastcgi config
      location ~ \.php {
        fastcgi_index index.php;

        fastcgi_cache MYAPP;
        # http code that u will cache, and the time it will be cached
        fastcgi_cache_valid 200 60m;
        
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      }


    # wordpress (maybe you have nested apps in your app)
    location /blog {
        index index.php index.html;
        try_files $uri $uri/ /blog/index.php?$args;   
    }

    # gzip compression
    gzip on;
    gzip_proxied any;
    gzip_types
        text/css
        text/plain
        text/javascript
        application/javascript
        application/json
        application/x-javascript
        application/xml
        application/xml+rss
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon
        application/rss+xml
        application/atom_xml;
    gzip_comp_level 9;
    gzip_http_version 1.0;
    gzip_buffers 16 8k;
    gzip_min_length 50;
}

I think i didn't forget anything. What I might do is try to cache everything, and then go for the cookie exceptions.

  • Like 1
Link to comment
Share on other sites

Have you been able to verify that your server is caching everything?

 

Can you create and upload a file called test-time.php with this inside:

 

<?php
echo time();
?>
 
If you refresh this a few times the time should stay the same. Let me know if it works for you.
Link to comment
Share on other sites

I changed my code to match yours and I'm now able to go from page to page as the logged in user BUT the user session is being cached so I can log in incognito mode on Chrome and be logged in a the user who was on the normal browser.

 

The below code should be what handles that, correct? 

 

location ~* \.(?:manifest|appcache|html?|xml|json)$ {

expires -1;
# access_log logs/static.log; # I don't usually include a static log
}

Link to comment
Share on other sites

Yeah, I mean 143 milliseconds. That's like light years. That's why i'm starting to learn how to deal with NGINX. fr the tpl file, i'm not sure though, you seem pretty advanced into the caching subject by now. When i get free soon, I will research it with you.

  • Like 1
Link to comment
Share on other sites

Yeah, I mean 143 milliseconds. That's like light years. That's why i'm starting to learn how to deal with NGINX. fr the tpl file, i'm not sure though, you seem pretty advanced into the caching subject by now. When i get free soon, I will research it with you.

Thank you! We'll talk soon :)

Link to comment
Share on other sites

This is the problem with fastcgi-cache and varnish, they cache the response, so if you need to work this response with cookies (user logged, cart, wishlist...) you need extra-work, for example making the proccess via ajax (post request) and its not cached and you dont lose the speed of the cache.

Link to comment
Share on other sites

No sorry, I've been looking for a good solution to implement http caches to eCommerces like Prestashop but I have not a solution jet. You need something to save the actual state of the app.

 

If you move from fastcgi to Varnish cache you could read about ESI Includes (you can cache part of the page), but prestashop and almost any framework or eCommerce are ready to implement then....

 

So im really sorry D:, even if you use ajax for user data, you need to store the Cart.. (maybe in a html5 local storage in the client side....)

 

If you find a solution please, share it!!!

Link to comment
Share on other sites

  • 1 month later...
# here you select your cache path and the name (MYAPP) to id your cache
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
# here you select the key to id. your cache elements.
fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_state";
server {

Using your FastCGI Cache configuration my site doesn't work properly. For example, you add some product to the cart and the cart doesn't change because te whole page is in the cache.

 

How do you manage to solve that issue?

 

With exceptions like this one?:

#Don't cache the following URLs
if ($request_uri ~* "/(administrator/|login.php)")
{
    set $no_cache 1;
}
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...