A
A
Artyom Zubkov2013-12-26 17:53:40
Nginx
Artyom Zubkov, 2013-12-26 17:53:40

nginx. Proxy requests from sub.domain.com to domain.com/sub/. Why does the client's url sometimes get rewritten?

Working config.

server {
    listen 80;
    server_name ~^(?:www\.)?(?:(?<sd>.*)\.)?domain\.com$;
 
    location / {
        proxy_pass              http://localhost:80/$sd$request_uri;
        proxy_set_header        Host domain.com;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache off;
        proxy_redirect off;
        break;
    }
}

Expected Behavior:
When following sub.domain.tdl, it proxies to domain.com/sub/.
This all works great.
When you go to sub.domain.com/something/, everything also happens as expected, it proxies to domain.com/sub/something/, but if you forget the last slash, then for some reason it rewrites the client's url to domain.tdl/sub /something/ .
How to fix it?
try it here:
d3.artzub.com/test/ - works as expected .
d3.artzub.com/test - rewrites url .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Shikanov, 2013-12-27
@dizballanze

It seems to me that your application does the redirect, not Nginx. Because return status: 301 Moved Permanently.
What is used on the backend?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question