Score:1

Why does this regexp match, but not location / in NGINX

in flag

I basically have those two locations in NGINX (Return codes added for debugging purpose)

location / {
    return 401;
}


location ~ "^/(assets|((de|en|es|fr|it|zh|ru)-[a-z]{3}))" {
    return 402;
}
         

The idea is to return 401 on GET https://www.example.com/, but it always returns 402

How can that be?

$ http https://www.example.com/ --print Hh
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: www.example.com
User-Agent: HTTPie/1.0.3

HTTP/1.1 402 Payment Required
Connection: keep-alive
Content-Length: 176
Content-Type: text/html
Date: Mon, 07 Jun 2021 10:07:37 GMT
Server: nginx/1.18.0 (Ubuntu)

I also tested the regexp on https://nginx.viraptor.info/ and it does not match a www.example.com/ request, so I believe it should run into the location / block?

EDIT: Before that I have some

rewrite ^/$ /de-abc/;
Richard Smith avatar
jp flag
`location /` only matches the request if no other `location` matches it. Is this the entire contents of the `server` block? If you want to specifically match `/`, use `location = /`.
in flag
It's not the entire contents, I can try to boil it down - but why does it look like the regexp match?
Richard Smith avatar
jp flag
Please edit the question and add more details. Clearly the original request does not match that `location`, but we have no idea how the original request is processed.
Michael Hampton avatar
cz flag
Please post the output of `nginx -T`
in flag
I found it - thanks
Score:0
in flag

The reasons was a rewrite which was not a redirect earlier in the config

rewrite ^/$ /de-abc/;

I intented to use

rewrite ^/$ /de-abc/ redirect;
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.