How to redirect to a different domain using NGINX?

前端 未结 8 837
旧时难觅i
旧时难觅i 2020-12-02 07:10

How can I redirect mydomain.com and any subdomain *.mydomain.com to www.adifferentdomain.com using NGINX?

相关标签:
8条回答
  • 2020-12-02 08:09

    Why use the rewrite module if you can do return? Technically speaking, return is part of the rewrite module as you can read here but this snippet is easier to read imho.

    server {
        server_name  .domain.com;
    
        return 302 $scheme://forwarded-domain.com;
    }
    

    You can also give it a 301 redirect.

    0 讨论(0)
  • 2020-12-02 08:13

    server_name supports suffix matches using .mydomain.com syntax:

    server {
      server_name .mydomain.com;
      rewrite ^ http://www.adifferentdomain.com$request_uri? permanent;
    }
    

    or on any version 0.9.1 or higher:

    server {
      server_name .mydomain.com;
      return 301 http://www.adifferentdomain.com$request_uri;
    }
    
    0 讨论(0)
提交回复
热议问题