如何使用nginx将域重写到不同域中的特定URL?

时间:2017-03-01 20:34:32

标签: nginx

我有一个域需要重写以指向使用nginx的不同域中的特定路径,如下所示:

domain2.com - > https://stage.domain1.com/web/#/45MKzL7H

两个域都指向同一主机(DNS)。这是我的nginx default.conf文件:

server {
    listen       80;
    server_name  domain1.com domain2.com;

    if ($host = 'domain2.com') {
      rewrite ^ https://stage.domain1.com/web/#/45MKzL7H;
    }

    rewrite ^/web/config/config.json /web/config/aws_stage.json;

    if ($request_uri = '/health-check') {
      return 200 "";
    }

    if ($http_x_forwarded_proto != 'https') {
      rewrite ^ https://$host$request_uri? permanent;
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /web {
      rewrite ^/web(/.*)$ $1 last;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;

谢谢!

1 个答案:

答案 0 :(得分:0)

为每个域使用单独的server块。

server {
    listen       80;
    server_name  domain2.com;
    return 301 https://stage.domain1.com/web/#/45MKzL7H;
}
server {
    listen       80;
    server_name  domain1.com;
    ...
}

有关详情,请参阅this document