R
R
Reddaemon2016-04-19 10:45:07
Nginx
Reddaemon, 2016-04-19 10:45:07

Why does Nginx lose part of the URI on 301 redirects?

Why does Nginx lose part of the URI on 301 redirects?
I do something like this:

location /grid {
   return 301 https://$server_name$request_uri;   
}

in the address bar I write domain.ru/foo/bar/ I get to https://domain.ru/bar
Before nginx there is also apache, which proxies to /foo in nginx.
How can this be fixed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-04-19
@AlexListen

because it's better to do this:
rewrite ^/(.*)$ https://$server_name/$1 permanent;

X
xbox, 2016-04-28
@xbox

Try like this. This option works for me.
For example, I use it to redirect all requests from the domain.ru domain to the www.domain.ru domain. In this case, the line with the address and parameters after the redirect is completely preserved.

location / {		
      rewrite ^(.*)$ http://www.domain.ru$1 permanent;
    }

(http/https - substitute what you need)
For the purity of the experiment, use "location /". The rest of the locations can be completely removed for a while.
Instead of the $server_name variable with the domain name, we use an explicit indication of the domain.
In the regular expression, we remove the slash from the first part and from the second. The slash will be passed inside the parameter.
If it works, then after that you can turn it into the look you want (by changing the location, passing the domain as a parameter, etc.). At the same time, look at what leads to the absence of the desired result.
And also pay attention. If you write domain.ru/foo/bar/ in the address bar, then it's not a fact that "location /grid" will process this request. There is a possibility that another location is processing it, which more closely matches the address. You can check which location is working, for example, like this
location /grid {
   return 503;   
}

Set a different return code for different locations without any additional processing. You type the address in the browser, get the error code and understand which location worked it out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question