Ad

NGINX Prevent Caching Certain Locations, While Having FCGI

- 1 answer

I have a Laravel application. Here is my NGINX config file:

server {                                                                                                                                                                          
     # Log files for Debugging
     access_log /var/log/nginx/access-test.log;
     error_log /var/log/nginx/access-test.log;                                                     

     # Webroot Directory for Laravel project                         
     root /var/www/html/deploy/website/current/public;             
     index index.php index.html index.htm;

     # Your Domain Name
     server_name test.example.com;

    location / {                                      
            try_files $uri $uri/ /index.php?$query_string;
    }                                    

     # PHP-FPM Configuration Nginx                    
     location ~ \.php$ {                  
             fastcgi_split_path_info ^(.+\.php)(/.+)$;
             fastcgi_pass unix:/run/php/php7.2-fpm.sock;
             fastcgi_index index.php;      
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
             include fastcgi_params;
     }          

listen 443 ssl; # managed by Certbot                      
ssl_certificate /etc/letsencrypt/live/test.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/test.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}   

I want to prevent any kind of caching in certain locations, say '/submit/*'.

I tried adding this above location /:

    location ~ /submit {
             expires -1;
             add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0' always;
            try_files $uri $uri/ /index.php?$query_string;
    }    

I also added a middleware to my Laravel app, to /submit sub-paths that appends Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0 and Expires: <one day ago> and Pragma: no-cache to response.

What I see in browser is that Expires and Pragma headers are set, but Cache-Control is not what I specified, and is being set to max-age=31536000.

I suspect something in NGINX is overwriting that header, but I can't seem to find it by reading docs or googling.

Any help or clue is appreciated.

BTW, I test the page in Firefox, while having inspector open, and having Disable cache ticked in Network tab. So I always see fresh headers from server.

Ad

Answer

I finally added following location block:

location ~ /submit {
         expires -1;
         add_trailer 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0' always;
             fastcgi_split_path_info ^(.+\.php)(/.+)$;
             fastcgi_pass unix:/run/php/php7.2-fpm.sock;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME $document_root/index.php;
             include fastcgi_params;
}

It now works as expected.

The only problem is now duplication, which can be solved by having fcgi commands in a separate file and includeing it in both locations.

I'm not so pleased with my current solution, so I'm still open to new or better ways.

Ad
source: stackoverflow.com
Ad