E
E
Ernest Faizullin2017-11-30 18:25:42
Nginx
Ernest Faizullin, 2017-11-30 18:25:42

How to set Last-Modified headers via free ssl on Clouflare?

Hi all. I'm trying to set up the Last-Modified, Expires headers so that only modified files are downloaded from the server. Let's say that I corrected some js file, refreshed the page and the modified file was loaded from the server, while other unmodified files returned the 304 Not Modified status.
Without https, you just need to specify expires modified 0;and everything works as it should. But when the request goes through https, then the headers are apparently lost, since the https connection goes through proxy DNS. (https is free)
In Cloudflare settings under Caching set Browser Cache Expiration to Respect Existing Headers (it's like using headers from the original server)
Then tried specifying headers ignoring framework headers (Laravel)

location ~* \.(css|js|gif|jpe?g|png)(\?[0-9]+)?$ {

        sub_filter_last_modified on;
        fastcgi_ignore_headers "Cache-Control" "Expires";
        proxy_ignore_headers "Cache-Control" "Expires";

        add_header Cache-Control "public, max-age=60, s-maxage=60, must-revalidate, proxy-revalidate";
        add_header Last-Modified $sent_http_Expires;

        proxy_set_header Last-Modified $sent_http_Expires;
        fastcgi_pass_header Last-Modified;

        if_modified_since exact;
    }

I tried a lot of things, but the headers are not transmitted. Perhaps someone has come across this, tell me advice or where to google.
Thank you.
UPDATE: I
managed to find a solution
before using it, you need to set Browser Cache Expiration to 2 hours in the Caching section (or even less, on a free plan it's 2 hours)
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        log_not_found off;
        
        sub_filter_last_modified on;
        fastcgi_ignore_headers "Cache-Control";

        add_header Cache-Control "public, max-age=30, s-maxage=30, must-revalidate, proxy-revalidate";
        proxy_set_header Last-Modified $sent_http_Expires;
        proxy_set_header Expires $sent_http_Expires;

        fastcgi_pass_header Last-Modified;
        fastcgi_pass_header Expires;
        if_modified_since exact;
    }

works well, but there are two nuances
1. The changes will take effect only 30 seconds after the file is changed
2. In chrome, you will need to check the Disable Cache checkbox in the console (for developers)
until I could find the reason why chrome does not send the If-Modified- header Since, after all, he sees the Last-Modified header and therefore must add the If-Modified-Since header, now I am looking for a solution to fix this (for example, everything is perfect in Firefox about this)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-11-30
@BorisKorobkov

Headers leave your server regardless of http/https.
If the request goes through Cloudflare, then it can sometimes give data from its cache. If you change a static file (which was previously allowed to be cached), you must either "Purge cache" in Cloudflare, or save the edited file under a different name (or at least access it with a different GET parameter)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question