dwvldg
dwvldg

Reputation: 317

Nginx proxy_pass after rewrite loses URI segments

I am trying to rewrite a url then proxy_pass the result to another server. Here is my setup:

server {
    rewrite ^/foo(.*)$ $1 last;
    location /bar {
        proxy_pass http://myserver/;
    }
}

So when I make a GET request for /foo/bar/dir/file.txt I expect the first rewrite to remove /foo, the location block to catch the result of the rewrite, then proxy_pass to http://myserver:8000/dir/file.txt because the trailing slash on the proxy_pass will strip off /bar.

This is what I expect however when I have the trailing slash then /dir/file.txt is lost in the proxy_pass, and the request is only made to http://mysever/ root URI. I confirmed when I take off the slash then /bar/dir/file.txt is indeed the request the upstream server.

Is there some extra step I am missing?

Upvotes: 0

Views: 357

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15687

Sounds weird, I expect the same behaviour from this config as you. What if you strip the /bar prefix inside the location block? Maybe this workaround would work?

    location /bar {
        rewrite ^/bar(.*) $1 break;
        proxy_pass http://myserver;
    }

Upvotes: 0

Related Questions