Nginx 重定向不在 SSL 证书中的根域

Nginx redirect root domain which is not in SSL certificate

我有一个 *.example.com 的 SSL 通配符证书,它对根域无效。我想要 Ubuntu 14.04 到

上的 Nginx
  1. 只接受定义主机的请求
  2. 将根域和 www 子域的所有 http 请求重定向到 https www 子域
  3. return 404 仅在端口 443 上用于根域,例如如果请求是 https://example.com

我使用下面复制的配置成功地实现了 1 和 2。

server {
    #listen 80;
    #isten 443;
   return 404;
}

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

server {

    listen 443 ssl;
    #certificate and key referenced in common.conf
    server_name www.example.com;

    root /usr/share/nginx/html/example.com;
    index index.php index.html index.htm;

    include common/common.conf;
}

当我从上面的第二行和第三行删除注释标记时,希望 return 404 仅用于 https://example.com - 没有任何效果。例如,对于 https://www.example.com/http://,我在 Chrome 中得到 ERR_CONNECTION_CLOSED ww.example.com/.

我应该怎么做才能同时实现 1)、2) 和 3)?

非常感谢。

... wildcard certificate for *.example.com which is not valid for the root domain ... return 404 for the root domain only on port 443, e.g. if reuqest is https://example.com

这是不可能的。对于 https,HTTP 响应是在 URL 中为主机建立的 TLS 连接内生成的。因此,要 return 访问 https://example.com 的 404,您必须首先拥有经过验证的 TLS 连接。但是因为 example.com 不包含在您的证书中,您在尝试建立 TLS 连接时会收到验证错误,因此没有成功建立 TLS 连接,并且无法在连接内 returned 404。