【问题标题】:nginx subdomain ssl redirect redirects top level domainnginx 子域 ssl 重定向重定向顶级域
【发布时间】:2014-01-13 02:08:23
【问题描述】:

我为我的子域上的所有 HTTP 流量设置了一个重定向以通过 HTTPS,但我注意到当我访问 http://mydomain.com 时,它会重定向到 https://subdomain.mydomain.comhttps://mydomain.com没有遇到问题。

澄清一下,

http://mydomain.com 不应重定向,但它会正确重定向到 https://subdomain.mydomain.com

http://subdomain.mydomain.com 应该重定向到https://subdomain.mydomain.com

这是我的 nginx 配置文件

server {
    listen *:80;
    server_name subdomain.mydomain.com;
    server_tokens off;
    root /nowhere; # this doesn't have to be a valid path since we are redirecting, you don't have to change it.
    rewrite ^ https://$server_name$request_uri permanent;
}

server {
    listen 443 ssl;
    server_name subdomain.mydomain.com;
    server_tokens off;
    .... other stuff ...
}

【问题讨论】:

  • mydomain.com 的服务器块在哪里?
  • 它不存在,因为 mydomain 应该不会去任何地方,直到我想出我想要放在那里的东西。
  • 如果不存在,则使用第一个可用的服务器块,从而重定向。您必须为mydomain.com 设置一个服务器块,以打印默认消息或其他内容。
  • 福布斯,非常感谢。这说明了一切。随意添加答案,我会将其标记为解决方案。

标签: nginx


【解决方案1】:

引用http://nginx.org/en/docs/http/request_processing.html

nginx 首先决定哪个服务器应该处理请求。让我们 从一个简单的配置开始,其中所有三个虚拟服务器 监听端口 *:80:

server {
    listen      80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      80;
    server_name example.net www.example.net;
    ...
}

server {
    listen      80;
    server_name example.com www.example.com;
    ...
}

在这个配置中,nginx 只测试请求的头部字段 “主机”来确定应将请求路由到哪个服务器。如果 它的值不匹配任何服务器名称,或者请求不匹配 完全包含这个头域,那么 nginx 会将请求路由到 此端口的默认服务器。在上面的配置中, 默认服务器是第一个——这是 nginx 的标准默认值 行为。

这意味着在您的示例中,当您向 http://mydomain.com 发出请求时,唯一可用的服务器块来服务请求是 server_name subdomain.mydomain.com; 块,因此它重定向到 https。最简单的解决方案是创建一个名为的新服务器块,它只返回一个 http 状态代码,可能还有一条消息。例如

server {
  listen 80;
  server_name mydomain.com;
  return 403 "nothing to see here";
}

您也可以像 listen 80 default_server; 一样使用 default_server 来确保所有“未配置”的主机名都会出现在此块中

【讨论】:

  • 我设置了一个临时占位符页面,它运行良好。谢谢!
猜你喜欢
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 2011-05-18
相关资源
最近更新 更多