openresty / openresty/lua-nginx-module

Multiple header_filter_by_lua_block wont working with FastCGI cache

Open
#1,335 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

One lua script in server section changes GET parameters of fcgi cached response to current, because i wont include GET in facgi_cache_key (buisines logic).
Including empty second lua script in location causes first lua script does not working.
Here is example:

fastcgi_cache_path /var/www/test.com/cache/nginx/products levels=1:2 keys_zone=fastcgi_cache_foo:512m max_size=320G inactive=12h;

upstream backends  {
      server 127.0.0.1:9000 weight=1 max_fails=2 fail_timeout=5s;
}

server {
	listen  *:80;
	server_name test.com;

	header_filter_by_lua_block {
		 -- Update GET parameters with current get parameters in cached response
                if ngx.resp.get_headers()["Location"] ~= nil and ngx.var.upstream_cache_status ~= nil then

			local is_cache = string.find("HIT STALE UPDATING", ngx.var.upstream_cache_status)

			if is_cache ~= nil then
				local location = string.gsub(ngx.header.location, "(\?.*)", "")

				if ngx.header.cacheparam ~= nil then
			    		location = location .. "?" .. ngx.header.cacheparam
				elseif ngx.var.is_args ~= '' then
			    		location = location .. "?" .. ngx.var.args
				end
				ngx.header.location = location
			end
		end
	}

	root /var/www/test.com/cgi-bin;
        index index.php;

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

	location ~ \.php$ {

		try_files $uri =404;
		fastcgi_pass backends;
		fastcgi_index index.php;
		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME /var/www/test.com/cgi-bin/index.php;
		fastcgi_param SCRIPT_NAME /var/www/test.com/cgi-bin/index.php;

		fastcgi_cache fastcgi_cache_foo;
		fastcgi_cache_valid 200 304 404 25h;
		fastcgi_cache_valid 302 5m;
		fastcgi_cache_valid 500 502 20s;

		fastcgi_param BACKGROUND_UPDATE_CACHE_ZONE 'fastcgi_cache_products';
		fastcgi_param BACKGROUND_UPDATE_CACHE_STATUSES '302';

		fastcgi_cache_lock on;
		fastcgi_cache_lock_timeout 25s;

		add_header X-Cache-Status $upstream_cache_status;

		set $fastcgi_cache_key "$request_method|$scheme|$host|$uri|old";
                fastcgi_cache_key $fastcgi_cache_key;
                fastcgi_param BACKGROUND_UPDATE_CACHE_KEY $fastcgi_cache_key;

		#If uncomment this, everything is working
		#header_filter_by_lua_block {}

	}

}

PHP script is very simple:

<?php

header("Location: foo?" . http_build_query($_GET));
exit;

Request cached page with commented second block header_filter_by_lua_block:
curl -v ​http://test.com/?test1
About to connect() to test.com port 80 (#0)
Trying 127.0.0.1... connected
Connected to test.com (127.0.0.1) port 80 (#0)
GET /?test1 HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: test.com
Accept: /
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 04 Jun 2018 15:22:27 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=75
< X-Powered-By: PHP/7.1.16
< Location: foo?test1=
< X-Cache-Status: HIT
<
Connection #0 to host test.com left intact
curl -v ​http://test.com/?test2
About to connect() to test.com port 80 (#0)
Trying 127.0.0.1... connected
Connected to test.com (127.0.0.1) port 80 (#0)
GET /?test2 HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: test.com
Accept: /
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 04 Jun 2018 15:22:30 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=75
< X-Powered-By: PHP/7.1.16
< location: foo?test2
< X-Cache-Status: HIT
<
Connection #0 to host test.com left intact
Closing connection #0

Request cached page with uncommented block:
curl -v ​http://test.com/?test1
About to connect() to test.com port 80 (#0)
Trying 127.0.0.1... connected
Connected to test.com (127.0.0.1) port 80 (#0)
GET /?test1 HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: test.com
Accept: /
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 04 Jun 2018 15:24:24 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=75
< X-Powered-By: PHP/7.1.16
< Location: foo?test1=
< X-Cache-Status: HIT
<
Connection #0 to host test.com left intact
Closing connection #0
curl -v ​http://test.com/?test2
About to connect() to test.com port 80 (#0)
Trying 127.0.0.1... connected
Connected to test.com (127.0.0.1) port 80 (#0)
GET /?test2 HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: test.com
Accept: /
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 04 Jun 2018 15:24:26 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=75
< X-Powered-By: PHP/7.1.16
< Location: foo?test1=
< X-Cache-Status: HIT
<
Connection #0 to host test.com left intact
Closing connection #0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the two configurations with FastCGI caching and multiple header_filter_by_lua_block directives, then trace how the directives are handled; done means the empty second block no longer changes the first block's cached Location behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua, nginx
Domain
backend, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.