0

There is a partially related question here but it doesn't help me.

For site.com I have

location /sub/ {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://192.168.1.1/ ;
}

The 192.168.1.1 IP has a VM with an Apache server on it.

Now when I go to either site.com/sub or site.com/sub/, it works fine as expected.

But when I go to site.com/sub/sub1, it redirects to 192.168.1.1/sub1/ whereas site.com/sub/sub1/ (with trailing slash) works as expected.

Using a proxy_redirect doesn't seem to make a difference. I also tried using with a regex but to no avail.

location ~/sub/?(.*)$ {
        proxy_set_header Accept-Encoding "";
        proxy_pass http://192.168.1.1/$1 ;
    }

Setting the header Host just redirects to site.com/sub1/.

Nginx logs show a 301 redirect.

I can easily solve the problem by simply creating a new location block for /sub/sub1/ but I would really like to be able to handle it in the same block.

As site.com/sub/sub1/ with the trailing slash already works fine, how can I get site.com/sub/sub1 to work correctly instead of redirecting to 192.168.1.1/sub1? What am I missing here?

3
  • Use curl -I to see the exact value of the Location header in the HTTP response. Jun 13 '20 at 8:16
  • that's http :// example.com/sub1/ (added spaces to show the complete path)
    – Yogesch
    Jun 13 '20 at 10:34
  • @RichardSmith thanks for the tip. I think I fixed my problem. But would you also be kind enough to explain a bit what was actually going on?
    – Yogesch
    Jun 13 '20 at 10:47
0

I believe I may have solved the problem by using a proxy_redirect. What I had tried first (when posting the question) was a proxy redirect for

proxy_redirect https://example.com/ https://example.com/sub

What worked was

proxy_redirect http://example.com/ http://example.com/sub

I got to the answer by carefully examining the nginx logs and the curl -I output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.