0

I'm trying to get a param(h=1500 for example) via regex in a server block in an nginx server but it's not working. My last try was this:

location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)?h=(?<height>\d+)$" { ...... }

Here you can check and it works: https://regex101.com/r/kP9eY9/1

But in my server block file it does't.

If I try something like this, it works:

location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)/(?<height>\d+)$" { ...... }

Instead a param like "h=300", I just use a "/300" and I can get the value in my server block file.

I'm not a expert using regex so I can't see if there is something wrong. I need your help guys! Thank you!

2
  • Have you tried to escape eqiality sign? I mean h\=. Also ? after your (?<image>.+). First request has it, but second doesn't. According to example string in provided link, I guess you wanted to set it as a character, not as special symbol, so use (?<image>.+)\? Commented Sep 6, 2016 at 22:14
  • I did try (?<image>.+)\? but this also doesn't work. And yes, I want it as a character like a get parameter ?foo=bar. I don't know if I did undertand, do I also need to escape the eqiality sign? Like \?h\=(?<height>\d+) Commented Sep 7, 2016 at 0:36

2 Answers 2

2

From the documentation:

locations of all types test only a URI part of request line without arguments

which means the ? and anything that follows it.

Sign up to request clarification or add additional context in comments.

Comments

0

As @Richard mentioned, you can't use request arguments in locations regexps.

If you need to work with request arguments in your nginx config you might use $arg_ and/or $args syntax:

http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_ http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args

I.e

location / {
  if ($arg_param = 'someval') {
     # some code here
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.