skip to Main Content

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;
}

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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)
    Login or Signup to reply.
  3. Section server in nginx has two required parameters listen and server_name. Add listen to your config and it will work

    Man about server https://nginx.org/en/docs/http/ngx_http_core_module.html#server

    Example

    server {
        listen 8080;
        server_name _;
        return 301 http://www.google.com/$request_uri;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search