Nginx redirect to another domain without trailing uri

Solution 1:

Your rewrite statement is changing the domain name but nothing else. The value of $request_uri is the original URI including the leading /cat part. You need to capture the latter part of the URI in the regular expression.

For example:

rewrite ^/cat/(.*)$ http://foo.example.com/$1 permanent;

Or maybe:

rewrite ^/cat(?:/(.*))?$ http://foo.example.com/$1 permanent;

Solution 2:

Another approach is to capture the part in the location directive:

location / {
    proxy_pass http://localhost:8080;
}

location ~ ^/cat(/.+)$ {
    return 301 http://foo.example.com$1$is_args$args;
}