3

I have a question related to Kubernetes Ingress-nginx, I want to use ngx_http_map_module to define a new attribute for log-format-upstream. The respective part in helm chart where I have defined my map looks like this:

containerPort:
   http: 80
   https: 443
 
 config:
   log-format-upstream: $time_iso8601, $proxy_protocol_addr, $proxy_add_x_forwarded_for, $req_id, $remote_user, $bytes_sent, $request_time, $status, $host, $server_protocol, $uri $uri_category

   http-snippet: |
       map $uri $uri_category {
        ~(*/)([0-9]{3,}+)(/*)$ $2;
        }

 configAnnotations: {}

However, it gives me following error:

nginx: [emerg] unexpected "{" in /tmp/nginx-cfg1517276787:255

The line 255 in the config looks like this:

 proxy_ssl_session_reuse on;
 map $uri $uri_category { #Line: 255
 ~(*/)([0-9]{3,}+)(/*)$ $2;
 }
 
 upstream upstream_balancer {

I doubt that i havent defined http-snippet and map at the right location in the chart, but i am not sure where exactly it should be either?

Maven
  • 14,587
  • 42
  • 113
  • 174
  • The regex is missing escape "\" characters. I can't tell what exactly what you want to replace, e.g. do you want any of these two `-~` characters to be converted to a `1`? When I test that map declaration, I get: `nginx: [emerg] invalid number of the map parameters` – Eric Fortis Oct 01 '22 at 02:30
  • @EricFortis fixed the regex, had been trying multiple patterns so. However, the issue and error is still the same. – Maven Oct 01 '22 at 11:02

1 Answers1

3

Related answer: Surround the regex in double-quotes; Nginx uses { and } for defining blocks.

For example:

  map $uri $uri_category {
    "~[0-9]{3,}" 'FOO';
  }

  server {
    location / {
      try_files $uri$uri_category =404;
    }
  }

That config appends 'FOO' to three+ consecutive digits.

/123  -> /123FOO
/4444 -> /4444FOO

In your case, I think the regex should be something like:

"~(.*/)([0-9]{3,})(/.*)$" $2;

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62