fastcgi_pass and caching with request rewrite

0815 at lenhardt.in 0815 at lenhardt.in
Thu Oct 15 16:25:48 UTC 2020


Hi!

This is the first time I am doing rewrites with a fastcgi backend (php-fpm).

This is my fpm location which is working fine on a ubuntu 18.04 VM:

        # fpm-config
         location ~ \.php$ {


                 include snippets/fastcgi-php.conf;
                 fastcgi_pass unix:/run/php/php-fpm-typo3.sock;
                 fastcgi_param   HTTPS 'on';

                 fastcgi_read_timeout 240;
                 # cache settings
		fastcgi_cache			html_cache;
		fastcgi_cache_valid		200 404 60m;
		fastcgi_cache_bypass		$no_cache_allowed;

		fastcgi_cache_bypass		$cookie_be_typo_user;
		fastcgi_cache_bypass		$eID_search_no_cache;


         }


in the http section:

http {
	...
	...

         fastcgi_cache_path /var/cache/nginx/proxy_cache levels=1:2
keys_zone=html_cache:10m max_size=5120m inactive=60m use_temp_path=off;

         fastcgi_cache_key "$scheme$request_method$host$request_uri";

         fastcgi_buffer_size          128k;
         fastcgi_buffers              4 256k;
         fastcgi_busy_buffers_size    256k;


	...
}


After adding this location in the fpm vhost the caching is not working
as expected and delivers the same page for each request:


        location ~ ^/hotel/([0-9]+)/.* {
                 rewrite ^/hotel/([0-9]+)/.*
/index.php?id=3238&user_kuoniibefe_pi3[iff]=$1 break;


                 include snippets/fastcgi-php.conf;
                 fastcgi_param  REQUEST_URI
$fastcgi_script_name?$query_string;
                 fastcgi_pass unix:/run/php/php-fpm-typo3.sock;
                 fastcgi_param   HTTPS 'on';

                 fastcgi_read_timeout 240;
                 # cache settings
                 include /etc/nginx/conf.d/fcgi_cache_settings.inc;

         }

The location block is used to rewrite URLs like
/hotel/1234/bla?x=1&...

to
/index.php?h=1234&.....

but in this case the cache key is missing all the query parameters:

2020/10/15 17:27:51 [debug] 825#825: *1 http2 request line: "GET 
/hotel/3/bl-zzz-xxxccccresort-spa-antigua/?ddate=2020-11-01&rdate=2021-04-30&adult=2&aid=3&dur=13,15&ibe=package 
HTTP/2.0"
2020/10/15 17:27:51 [debug] 825#825: *1 http cache key: 
"httpsGETdev1.restplatzboerse.at"


with other URLs the cache key is correct:
2020/10/15 17:27:30 [debug] 32644#32644: *761 http2 request line: "GET 
/index.php?id=3238&user_kuoniibefe_pi3[iff]=3&ddate=2020-11-01&rdate=2021-04-30&adult=2&aid=3&dur=13,15&ibe=package 
HTTP/2.0"
2020/10/15 17:27:30 [debug] 32644#32644: *761 http cache key: 
"httpsGETdev1.restplatzboerse.at/index.php?id=3238&user_kuoniibefe_pi3[iff]=3&ddate=2020-11-01&rdate=2021-04-30&adult=2&aid=3&dur=13,15&ibe=package"


My question is why is the cache key incomplete with request that are 
rewritten? How could I get the correct cache key after rewriting the 
/hotel/1234/bla request.

The fastcgi_cache_key parameter in the http block is only once allowed, 
so I can not use another fastcgi_cache_key parameter in the rewrite 
location block.

br,
Marco



More information about the nginx mailing list