Score:2

Nginx redirect to another domain without trailing uri

us flag

I am trying to get http://foo.mydomain.xyz/one/two/three.json by calling http://bar.mydomain.xyz/cat/one/two/three.json. I am using the following configuration:

server {
        listen 80;
        listen [::]:80;
        server_name bar.mydomain.xyz;
        absolute_redirect off;

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

        location /cat {
          rewrite ^(/cat) http://foo.mydomain.xyz$request_uri permanent;
        }
}
server {
        listen 80;
        listen [::]:80;
        server_name foo.mydomain.xyz;

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

Using this configuration when I am calling: http://bar.mydomain.xyz/cat/ it is redirecting me to http://foo.mydomain.xyz/ successfully. But when I am calling http://bar.mydomain.xyz/cat/one/two/three.json it is returning http://foo.mydomain.xyz/cat/one/two/three.json. Notice the /cat is not removing from the url. How can I solve this?

Richard Smith avatar
jp flag
`$request_uri` is the original URI, you need to capture the latter part of the URI in the `rewrite` regex. Try: `rewrite ^/cat/(.*)$ http://foo.mydomain.xyz/$1 permanent;`
us flag
@RichardSmith You can post this as answer. Thanks
Score:1
jp flag

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;
us flag
in my case I am trying to go to `https://foo.mydomain.xyz/bar/v1/search?text=mytext&size=25` from `https://bar.mydomain.xyz/v1/search?text=mytext&size=25`. When I trying `rewrite` it is redirecting only to `https://bar.mydomain.xyz/v1/search` portion. Parameters are not passing.
Score:1
us flag

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;
}
us flag
It is returning `invalid number of arguments in "return" directive in /etc/nginx/sites-enabled/mydomain.com:19`
us flag
Sorry, my mistake, I left an extra `permanent` word over there.
us flag
@tero-kilman, in my case I am trying to go to `https://foo.mydomain.xyz/bar/v1/search?text=mytext&size=25` from `https://bar.mydomain.xyz/v1/search?text=mytext&size=25`. When I trying `return` it is redirecting only to `https://bar.mydomain.xyz/v1/search` portion. Parameters are not passing.
us flag
I added the arguments to the redirect in my answer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.