首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用nginx将域名重写为不同域名中的特定URL?

问如何使用nginx将域名重写为不同域名中的特定URL?
EN

Stack Overflow用户
提问于 2017-03-02 04:34:32
回答 1查看 30关注 0票数 0

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

域名2.com --> https://stage.domain1.com/web/#/45MKzL7H

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

代码语言:javascript
复制
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;

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-03-02 06:50:18

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

代码语言:javascript
复制
server {
    listen       80;
    server_name  domain2.com;
    return 301 https://stage.domain1.com/web/#/45MKzL7H;
}
server {
    listen       80;
    server_name  domain1.com;
    ...
}

有关详细信息,请参阅this document。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42541536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档