0

I have a variable say $foo and I need to do a case-insensitive match against a given pattern, but that pattern is also a variable received via a header. If that is true then I set a given variable to 1, which will be used later on. I tried with the following snippet:

set $variable 0; if ($foo ~* $http_header) { set $variable 1; } 

This didn't work. I also tried using ${http_header}, "$http_header", "${http_header}", but none of these worked. Can someone please tell me how to achieve this?

P.S. I know there will be a solution with LUA scripts, but we need to avoid using that for the purposes of the project I am working on.

2
  • 1
    You can't. (without Lua or njs) Commented Oct 28, 2024 at 21:35
  • All regular expressions from configuration file(s) are being compiled into libpcre/libpcre2 internal representation during nginx startup, so, as already said, you can't do it. Commented Oct 29, 2024 at 21:44

2 Answers 2

0

You can concatenate the two variables then verify that the first half matches the second half using a regex. Is there an easier way in 2024?

set $room "$carpet:$curtains"; if ($room !~* "^(.*):\1$"){ return 403 '403 - Carpet ${carpet} did not match curtains ${curtains}'; } 

Borrowed from 2011

https://forum.nginx.org/read.php?2,206345,206620

2
0

I use following approach to map http_header $host to my custom variable.

In your case it would be something like:

map $http_header $foo { "~*{{REGEX_PATTERN}}" "{{FOO_CONTENT}}"; } 

You parse the content of your desired http_header with a {{REGEX_PATTERN}} and set your variable {{FOO_CONTENT}} accordingly.

Keep in mind, you can also use map without regex.

Please have a look at following stackoverflow answer, which sums it up really great.

https://stackoverflow.com/a/66381679/10891124

Nginx documenatation: https://nginx.org/en/docs/http/ngx_http_map_module.html

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.