dwchiang/nginx-php-fpm

php caching

Closed this issue · 2 comments

I create a file sleep200.php

<?php $startMicrotime = microtime(true); $sleepInMilliseconds = 200; usleep( $sleepInMilliseconds * 1000 ); print ((microtime(true) - $startMicrotime) * 1000) . 'ms runtime<br>'; // millisekunden
If i run domain.tld/sleep200.php i get a result like "200.11210441589ms runtime" and the next requests get exact the same response. It looks that this url is full cached and not mor running with php.
How can i change this?

Best Regards Thomas

It seems that your web server is caching the output of your PHP script, causing the microtime calculation to be the same for each request. To prevent this behavior, you can disable caching for the specific PHP script by sending appropriate HTTP headers.

Add the following lines at the beginning of your sleep200.php file to send headers that instruct browsers and intermediary caching systems not to cache the response:

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Another option is to check the nginx configuration files to match your situation :)

For anyone who facing this problem, you can disable fastcgi_cache on /etc/nginx/conf.d/default.conf

    location ~ \.php$ {
        root /usr/share/nginx/html/;
        
        fastcgi_cache dwchiang;
        fastcgi_cache_valid 200 204 60m;
        fastcgi_ignore_headers Cache-Control;
        fastcgi_no_cache $skip_cache $http_authorization $cookie_laravel_session;
        fastcgi_cache_lock on;
        fastcgi_cache_lock_timeout 10s;
        fastcgi_buffer_size 6144;

        add_header X-Proxy-Cache $upstream_cache_status;

        fastcgi_pass            127.0.0.1:9000;
        fastcgi_index           index.php;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param           HTTPS $fastcgi_param_https_variable;
        fastcgi_read_timeout    900s;
        include                 fastcgi_params;
    }

removing

fastcgi_cache dwchiang;
fastcgi_cache_valid 200 204 60m;
fastcgi_ignore_headers Cache-Control;
fastcgi_no_cache $skip_cache $http_authorization $cookie_laravel_session;
fastcgi_cache_lock on;
fastcgi_cache_lock_timeout 10s;