【问题标题】:Nginx server_name & listen matches specified patternsNginx server_name & listen 匹配指定模式
【发布时间】:2015-12-18 17:52:00
【问题描述】:

在此示例中,域已替换为 domain.com

我们的主要问题: 当我输入https://domain.com 时,我没有被重定向到https://www.domain.com,我们目前对此没有规定什么是解决这个问题的最佳方法?

根据我们的 nginx 配置,我们没有为 https://domain.com 指定 443 但仍然可以访问,这是为什么呢?

我们拥有适用于 domain.com 和 www.domain.com 的有效 ssl 证书。 我们没有通配符证书 *.domain.com。

我们的配置:

#All non-matching patterns
server
{
  listen 80;

  #enabling this will cause things to break.
  #2015/12/18 09:21:54 [error] 32165#0: *1661 no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking, client: *censored*, server: 0.0.0.0:443
  #listen 443 ssl;

  #Horrible looking match all pattern.
  server_name _ "" domain.com *.domain.com;

  return 301 https://www.domain.com$request_uri;
}

#Main site ssl enforced
server
{
  listen 443 ssl;

  server_name www.domain.com ios.domain.com android.domain.com;

  ...
}

#Staging / Test site
server
{
  listen 443 ssl;
  listen 80;

  server_name stage.domain.com;

  ...
}

#Rental cars site ssl enforced
server
{
  listen 443 ssl;

  server_name hyrbil.domain.com;

  ...
}

#ios redirect to enforce https
server
{
  listen 80;

  server_name ios.domain.com;

  return 301 https://ios.domain.com$request_uri;
}

#android redirect to enforce https
server
{
  listen 80;

  server_name android.domain.com;

  return 301 https://android.domain.com$request_uri;
}

奖金问题: 是否可以匹配所有 ssl 流量并进行重定向,除非它与特定域匹配,例如让 https://xxx.domain.com 将 301 传递给 https://www.domain.com,即使我没有 xxx.domain.com 的证书而不显示“此页面不安全,您确定要继续吗?

【问题讨论】:

标签: nginx


【解决方案1】:

如果您有一个虚拟主机在 443 上侦听,则所有到达您的 IP 地址的流量都将由该虚拟主机提供服务。

为 domain.com 创建一个 SSL 虚拟主机并在其中放置一个简单的重定向。

或者创建一个“catch all/default” SSL 虚拟主机,并检查 HOST 标头并重定向,例如:

if ($host !~* ^www\.doman\.com$) { rewrite ^(.*)$ http://www.domain.com$1 permanent; } 但它会在您的证书中未包含的所有 FQDN 上显示 SSL 证书错误!

【讨论】:

    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 2016-10-21
    • 2021-02-20
    • 2018-09-19
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多