Home > database >  Nginx redirect for a subdomain
Nginx redirect for a subdomain

Time:10-12

does anyone know how the interaction works in Nginx? I currently have a subdomain, let's call it subdomain1, I want to change it to subdomain2. To be more specific. I run everything in a docker container and my certificate will be for subdomain2. And there will be no more servers with subdomain1. I want to keep the traffic from google for subdomain1, but the name is not appropriate anymore and it needs to be changed to subdomain2. Does something like this work? Will there be any issues?

server {
    server_name subdomain1.mydomain.com;
    return 301 http://www.subdomain2.mydomain.com/$request_uri;
}

CodePudding user response:

Something like that could match :

server {
        listen 8066;
        server_name localhost;

        location / {
            rewrite (.*)$ http://www.google.com$1 redirect;
        }
}

8066 is for my test purpose to redirect to google.com. If y try localhost:8066/foo, I go to https://www.google.com/foo Note that redirect keyword makes it temporary. For a permanent redirection, use permanent instead.

CodePudding user response:

Yes, your approach will work. Following points might be helpful:

  1. Since you want not to have any server for subdomain1 but in this redirection you need to ensure that subdomain1 also pointing to the same server where you have hosted subdomain2
  2. use of $scheme server { server_name subdomain1.mydomain.com; return 301 $scheme://subdomain2.mydomain.com$request_uri; }
  3. Generally people avoid using www before sub-domain.domain.com (you may refer this also)
  • Related