Welcome! Log In Create A New Profile

Advanced

Rewrite with number after hyphen

Posted by Ivan Bianchi 
Ivan Bianchi
Rewrite with number after hyphen
September 03, 2018 08:20AM
Hi,

I detected an issue with my rewrite rule in the nginx.conf and I don't
understand why it happens and how to fix it.

I have tested in two environments with versions 1.10.3 and 1.14.0.

Having the following simple conf with a regex is intended to get everything:

> location /foo {
> rewrite /foo/(.*) /web/foo.do?a=$1 last;
> }


OK:

> https://www.test.com/foo/asdf

https://www.test.com/foo/asdf-asdf

https://www.test.com/foo/asdf12

https://www.test.com/foo/asdf12-asdf

https://www.test.com/foo/12

https://www.test.com/foo/-12


KO:

> https://www.test.com/foo/asdf-12

https://www.test.com/foo/asdf-12-asdf


As implementing pcre regex, this regex works in all cases in the common
regex online sites but not in nginx.

Why if I put a number after a hyphen the regex stops working?

Many thanks,

--
Ivan Bianchi
Wikiloc
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Francis Daly
Re: Rewrite with number after hyphen
September 03, 2018 02:40PM
On Mon, Sep 03, 2018 at 08:13:11AM +0200, Ivan Bianchi wrote:

Hi there,

> > location /foo {
> > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > }

This seems to work as expected for me, using nginx/1.14.0.

> KO:
>
> > https://www.test.com/foo/asdf-12

Why do you think it does not work? What is the input/output/expected
output?

For example, if you add the new location

location = /web/foo.do {
return 200 "$uri$is_args$args\n";
}

and repeat the tests, do you see any difference in output?

> Why if I put a number after a hyphen the regex stops working?

My guesses are:

* you have another location{} that you have configured to match those
requests, so your shown location{} is not involved

or

* your /web/foo.do location-handler handles those requests differently.

Good luck with it,

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Ivan Bianchi
Re: Rewrite with number after hyphen
September 05, 2018 04:30PM
Hi Francis,

many thanks for your response and guidelines. Indeed you were right that
there was another location capturing the request.

Best regards,

On Mon, Sep 3, 2018 at 2:36 PM Francis Daly <[email protected]> wrote:

> On Mon, Sep 03, 2018 at 08:13:11AM +0200, Ivan Bianchi wrote:
>
> Hi there,
>
> > > location /foo {
> > > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > > }
>
> This seems to work as expected for me, using nginx/1.14.0.
>
> > KO:
> >
> > > https://www.test.com/foo/asdf-12
>
> Why do you think it does not work? What is the input/output/expected
> output?
>
> For example, if you add the new location
>
> location = /web/foo.do {
> return 200 "$uri$is_args$args\n";
> }
>
> and repeat the tests, do you see any difference in output?
>
> > Why if I put a number after a hyphen the regex stops working?
>
> My guesses are:
>
> * you have another location{} that you have configured to match those
> requests, so your shown location{} is not involved
>
> or
>
> * your /web/foo.do location-handler handles those requests differently.
>
> Good luck with it,
>
> f
> --
> Francis Daly francis@daoine.org
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
>


--
Ivan Bianchi
Wikiloc
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Jason Whittington
RE: [IE] Re: Rewrite with number after hyphen
September 05, 2018 08:40PM
FWIW when I debug this sort of thing I like to emit a response header identifying which rule is routing the request, like this:

location /a/ {
add_header X-nginx-debug /a/
proxy_pass http://whatever/;
}

That way you can use F12 tools or some other inspection on the result and see exactly what is triggering. This has saved my bacon more than once ☺.

Jason


> > location /foo {
> > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > }

From: nginx [mailto:[email protected]] On Behalf Of Ivan Bianchi
Sent: Wednesday, September 5, 2018 9:25 AM
To: nginx@nginx.org
Subject: [IE] Re: Rewrite with number after hyphen

Hi Francis,

many thanks for your response and guidelines. Indeed you were right that there was another location capturing the request.

Best regards,

On Mon, Sep 3, 2018 at 2:36 PM Francis Daly <[email protected]<mailto:[email protected]>> wrote:
On Mon, Sep 03, 2018 at 08:13:11AM +0200, Ivan Bianchi wrote:

Hi there,

> > location /foo {
> > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > }

This seems to work as expected for me, using nginx/1.14.0.http://1.14.0.

> KO:
>
> > https://www.test.com/foo/asdf-12

Why do you think it does not work? What is the input/output/expected
output?

For example, if you add the new location

location = /web/foo.do {
return 200 "$uri$is_args$args\n";
}

and repeat the tests, do you see any difference in output?

> Why if I put a number after a hyphen the regex stops working?

My guesses are:

* you have another location{} that you have configured to match those
requests, so your shown location{} is not involved

or

* your /web/foo.do location-handler handles those requests differently.

Good luck with it,

f
--
Francis Daly [email protected]<mailto:[email protected]>
_______________________________________________
nginx mailing list
[email protected]<mailto:[email protected]>
http://mailman.nginx.org/mailman/listinfo/nginx


--
Ivan Bianchi
Wikiloc
This message contains proprietary information from Equifax which may be confidential. If you are not an intended recipient, please refrain from any disclosure, copying, distribution or use of this information and note that such actions are prohibited. If you have received this transmission in error, please notify by e-mail postmaster@equifax.com. Equifax® is a registered trademark of Equifax Inc. All rights reserved.
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Ivan Bianchi
Re: [IE] Re: Rewrite with number after hyphen
September 06, 2018 08:40AM
Hi Jason,

that's a very nice tip! I finally get it enabling *rewrite_log* and
*error_log* at *notice*. but this definitely seems a great alternative.

Many thanks,

On Wed, Sep 5, 2018 at 8:29 PM Jason Whittington <
[email protected]> wrote:

> FWIW when I debug this sort of thing I like to emit a response header
> identifying which rule is routing the request, like this:
>
>
>
> location /a/ {
>
> *add_header X-nginx-debug /a/*
>
> proxy_pass http://whatever/;
>
> }
>
>
>
> That way you can use F12 tools or some other inspection on the result and
> see exactly what is triggering. This has saved my bacon more than once J..
>
>
>
> Jason
>
>
>
>
>
> > > location /foo {
> > > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > > }
>
> *From:* nginx [mailto:[email protected]] *On Behalf Of *Ivan Bianchi
> *Sent:* Wednesday, September 5, 2018 9:25 AM
> *To:* nginx@nginx.org
> *Subject:* [IE] Re: Rewrite with number after hyphen
>
>
>
> Hi Francis,
>
>
>
> many thanks for your response and guidelines. Indeed you were right that
> there was another location capturing the request.
>
>
>
> Best regards,
>
>
>
> On Mon, Sep 3, 2018 at 2:36 PM Francis Daly <[email protected]> wrote:
>
> On Mon, Sep 03, 2018 at 08:13:11AM +0200, Ivan Bianchi wrote:
>
> Hi there,
>
> > > location /foo {
> > > rewrite /foo/(.*) /web/foo.do?a=$1 last;
> > > }
>
> This seems to work as expected for me, using nginx/1.14.0.
>
> > KO:
> >
> > > https://www.test.com/foo/asdf-12
>
> Why do you think it does not work? What is the input/output/expected
> output?
>
> For example, if you add the new location
>
> location = /web/foo.do {
> return 200 "$uri$is_args$args\n";
> }
>
> and repeat the tests, do you see any difference in output?
>
> > Why if I put a number after a hyphen the regex stops working?
>
> My guesses are:
>
> * you have another location{} that you have configured to match those
> requests, so your shown location{} is not involved
>
> or
>
> * your /web/foo.do location-handler handles those requests differently.
>
> Good luck with it,
>
> f
> --
> Francis Daly francis@daoine.org
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
>
>
>
>
> --
>
> Ivan Bianchi
>
> Wikiloc
> This message contains proprietary information from Equifax which may be
> confidential. If you are not an intended recipient, please refrain from any
> disclosure, copying, distribution or use of this information and note that
> such actions are prohibited. If you have received this transmission in
> error, please notify by e-mail postmaster@equifax.com. Equifax® is a
> registered trademark of Equifax Inc. All rights reserved.
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx



--
Ivan Bianchi
Wikiloc
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login