Kliver Max
Kliver Max

Reputation: 5299

How to check substring using regular expression in Nginx config?

I try to check some parameters in request. Here is my url:

http://localhost:8080/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&LAYERS=004C0000064F&
     STYLES=&WIDTH=256&HEIGHT=256&FORMAT=image%2fjpeg&CRS=EPSG%3a100000&DPI=96&
 MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi%3a96&
BBOX=1530569.52624839870259166%2c524135.21126760687911883%2c1531064.27656850102357566%2c524629.96158770937472582

I trying to get REQUEST parameter. Here is my nginx 1.12.1 config:

server {
  listen      8080;
  server_name 127.0.0.1 localhost;

  set $site_backend localhost:56297;

  proxy_set_header  Host            $host;
  proxy_set_header  X-Real-IP       $remote_addr;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

  location /favicon.ico {
      error_page 403 404  =  @tomcat_static_mapping;
  }

  location ~* /wms {

      internal;
      add_header URI $request_uri;
      add_header X-debug-message1 "$request_uri" always;

      if ($request_uri ~* REQUEST=([^&]*)) {
          add_header X-debug-message2 "hi" always;
          set $requesttype $1;
      } 
  }
}

And in browser i got header:

X-debug-message1: /wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&LAYERS=004C0000064F&STYLES=&WIDTH=256&HEIGHT=256&FORMAT=image%2fjpeg&CRS=EPSG%3a100000&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi%3a96&BBOX=1530569.52624839870259166%2c524135.21126760687911883%2c1531064.27656850102357566%2c524629.96158770937472582

But not get X-debug-message2 header. I check regular expression here https://rubular.com/ and it's found match and return GetMap as like i want.
What can be wrong here?

Upvotes: 0

Views: 2981

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9855

Something is not complete / matching in your post. I got X-debug-message2: hi only which does match to how nginx has to behave:

These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level

For more intuitive outcome, use Headers-More module.

 more_set_headers "URI: $request_uri";
 more_set_headers 'X-debug-message1: "$request_uri"';
 location ~* /wms {
     if ($request_uri ~* REQUEST=([^&]*)) {
         more_set_headers 'X-debug-message2: hi';
         set $requesttype $1;
     }
 }

Upvotes: 1

Related Questions