Browser Caching with Nginx's issue

March 25, 2018 2.1k views
Nginx Debian

Hallo,

i have use this tutorial but i have the problem.
I put html files on epoch but these files are cached.

Clould anyone help me out please?

https://i.imgur.com/UlM31tr.png

atm i have add this code to resolve the problem, but im still confused why i need this

        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;

2 Answers

It appears to be working for me:

~ ❯❯❯ curl -i https://anzahcraft.de/test.html
HTTP/2 200 
server: nginx
date: Sun, 25 Mar 2018 19:26:02 GMT
content-type: text/html
content-length: 1024
vary: Accept-Encoding
last-modified: Sunday, 25-Mar-2018 19:26:02 GMT
cache-control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0

Do you get the same issue in a different browser? There's an option to disable caching while the developer tools window is open in the developer tools settings pane. What happens when you enable that and reload the page?

  • yes after i add this part, but only with the tutorial settings it does not run

            add_header Last-Modified $date_gmt;
            add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
            if_modified_since off;
            expires off;
            etag off;
    

    yes i have test it with 3 browsers
    if i reload the page without cache i get the correct cache control settings, but only if i use the "disable chache" option, after i disable it, i have still the same issue

    • Ok, it sounds like Chrome is caching the old Cache-Control headers. Try opening developer tools, right clicking on the reload button and clicking on Empty Cache and Hard Reload. That should clear the cached headers and allow you to receive the updated no-store, ... one without having to open the developer tools.

      • ok, but if i remove this part I have still the same problem

                add_header Last-Modified $date_gmt;
                add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                if_modified_since off;
                expires off;
                etag off;
        

        this is my current config

        # Expires mappings
        map $sent_http_content_type $expires {
            default                 off;
            text/html               epoch;
            text/css                max;
            application/javascript  max;
            ~image/                 max;
        }
        
        server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            expires $expires;
        ...
            location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
                add_header Last-Modified $date_gmt;
                add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                if_modified_since off;
                expires off;
                etag off;
            }
        ...
        
        • I'm getting the right headers for test.html now:

          expires: Thu, 01 Jan 1970 00:00:01 GMT
          cache-control: no-cache
          

          I'm fairly sure the issue is your browser caching the old headers, as I have only visited your website after you set the right settings, so it was not a problem for me. Try out Incognito Mode, see if you get the old headers there as well.

          • If i remove this part:

                    add_header Last-Modified $date_gmt;
                    add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                    if_modified_since off;
                    expires off;
                    etag off;
            

            it does not work in incognito mode too

          • now have a look here please:

            Request URL:https://anzahcraft.de/
            Request Method:GET
            Status Code:200 
            Remote Address:185.233.105.115:443
            Referrer Policy:no-referrer
            cache-control:max-age=604800
            content-encoding:gzip
            content-length:27594
            content-type:text/html; charset=utf-8
            date:Mon, 26 Mar 2018 19:39:24 GMT
            expires:Mon, 02 Apr 2018 19:39:24 GMT
            last-modified:Mon, 26 Mar 2018 19:39:24 GMT
            referrer-policy:no-referrer
            server:nginx
            status:200
            strict-transport-security:max-age=31536000; includeSubDomains
            vary:Accept-Encoding
            x-content-type-options:nosniff
            x-content-type-options:nosniff
            x-frame-options:SAMEORIGIN
            x-frame-options:SAMEORIGIN
            x-xss-protection:1; mode=block
            

            this is my config file:

            # Expires mappings
            map $sent_http_content_type $expires {
                default                 off;
                text/html               epoch;
                text/css                max;
                application/javascript  max;
                ~image/                 max;
            }
            
            
            server {
                expires $expires;
            
                location / {
                    try_files $uri $uri/ /index.php?$uri&$args;
                }
            
                location ~ \.php$ {
                    include fastcgi.conf;
                    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                }
            
            
Have another answer? Share your knowledge.