【问题标题】:nginx redirect with a subdomain and naked domainnginx 使用子域和裸域重定向
【发布时间】:2020-11-28 01:44:44
【问题描述】:

我试图在我的 nginx 配置中找出我的域的重定向。我有这三个域名:

domain.com //points to server 1
www.domain.com //points to hubspot server 2, no a record to change naked domain
app.domain.com //points to server 1
  • 我需要 domain.com 重定向到www.domain.com
  • 我需要 app.domain.com 保持不重定向

我正在尝试的一切都是将所有网址重定向到www.domain.com

我做错了什么?

一次尝试,重定向所有内容:

server {
    listen 80;
    server_name domain.com;
    return 301 www.domain.com;
}

server {
    root var/www/domain/public;
    index index.php index.html;
    server_name app.domain.com;
}

再次尝试,重定向所有内容:

server {
    listen 80;
    root var/www/domain/public;
    index index.php index.html;
    server_name domain.com app.domain.com;
    return 301 www.domain.com
}

这个完全破坏了我的配置:

server {
    listen 80;
    root var/www/domain/public;
    index index.php index.html;
    server_name domain.com app.domain.com;
  
    location domain.com {
      return 301 www.domain.com;
    }
}

更新:

我已经更新了我的配置来说明:

server {
        server_name     domain.com;
        return          301 $scheme://www.domain.com$request_uri;

}

server {
    root /var/www/domain/public;
    index index.php index.html;
    server_name app.domain.com;


    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
}

这是目前正在发生的事情:

  • domain.com 和 app.domain.com 都指向我们的谷歌云服务器。我希望 app.domain.com 继续指向此处,并将 domain.com 到 301 重定向到 hubspot
  • www.domain.com 指向我们的 hubspot 服务器

目前发生的情况是:

  • www.domain.com 去正确的地方(中心点)
  • app.domain.com 转到正确的位置(谷歌云)
  • domain.com 仍然会转到错误的位置(谷歌云),但使用 curl -I -L domain.com 表示它是 301 重定向到正确的位置!我很困惑。

我的一些朋友为我测试地址说他们被正确的 301 重定向,但有些不是(包括我自己)

这是 curl 命令的打印输出:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Sun, 09 Aug 2020 22:33:49 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: ht tp://www.domain.com/

HTTP/1.1 200 OK
Date: Sun, 09 Aug 2020 22:33:49 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 54566
Connection: keep-alive

【问题讨论】:

    标签: nginx redirect ubuntu-16.04


    【解决方案1】:

    www.example.comapp.example.com 使用相同的根目录?如果是,请参见下面的示例。

    对于example.com,如果您需要重定向到 www 子域:

    server {
        listen 80;
        server_name example.com;
        return 301 http://www.example.com$request_uri;
    }
    

    对于www.example.comapp.example.com,没有来自/var/www/example/public 的重定向和输出数据:

    server {
        listen 80;
        server_name www.example.com app.example.com;
    
        root /var/www/example/public;
        index index.php index.html;
    }
    

    【讨论】:

    • 裸域和app.domain使用同一个root。我已经尝试过您的解决方案并将重定向移动到它自己的服务器块中,但它似乎对网站没有任何影响(至少,它不会重定向到 www)任何想法为什么?谢谢!
    • 裸域和 app.domain 使用相同根目录的原因是因为我们的营销网站使用 hubspot,它不提供 IP 地址,所以我无法更改裸域的记录域,需要将其重定向到 www 子域
    • 您能否详细描述一下您访问 example.com 时会发生什么?你用的是http还是https?如果您使用 https,那么在我发送的配置中,很可能 app.example.com 的使用早于 example.com 并且您没有收到到 www 的重定向,因为配置中没有描述 https 的方案。
    • 我已经用我当前的配置和使用curl -I -L domain.com 测试站点的打印输出更新了上面的帖子。以上是我关于指向网站上任何 URL 的唯一信息,因此之前没有 HTTPS 信息,尽管我确实有一些让 certbot 控制的内容在它之后加密
    • (www 不是任何服务器块的一部分,因为它的路由现在由谷歌云 DNS 和 Hubspot 控制)
    猜你喜欢
    • 2015-03-03
    • 2014-05-12
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2021-07-30
    • 2017-07-17
    相关资源
    最近更新 更多