AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 655483
Accepted
ZedTuX
ZedTuX
Asked: 2014-12-31 12:13:33 +0800 CST2014-12-31 12:13:33 +0800 CST 2014-12-31 12:13:33 +0800 CST

为解析服务器 IP 的所有服务器名称提供 Nginx SSL 证书

  • 772

鉴于我在 DNS 中配置了 2 个子域(因此使用我的服务器的 IP 地址对这两个子域进行 ping 操作)并且对于这些子域,我有 2 个不同的 TLS 证书。

我已经以这种方式配置了 nginx:

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}

# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  ''      '';
}

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

access_log /var/log/nginx.log;
error_log /var/log/nginx_errors.log;

# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;

server {
  listen 80 default_server;
  server_name _; # This is just an invalid value which will never trigger on a real hostname.
  return 503;
  server_tokens off; # Hide the nginx version
}


upstream sub1.domain.tld {
  server 172.17.0.27:5000;
}

server {
  server_name sub1.domain.tld;
  server_tokens off; # Hide the nginx version

  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/sub1.domain.tld.crt;
  ssl_certificate_key /etc/nginx/ssl/sub1.domain.tld.key;

  location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd/sub1.htpasswd;
    proxy_pass http://sub1.domain.tld;
  }
}

在这一点上,如果我全力以赴,https://sub1.domain.tld一切正常。现在,如果我尝试访问https://sub2.domain.tld尚未配置的访问,因此不应该回复它接受连接并向我显示证书的问题,因为它与服务器名称不匹配,所以似乎使用此配置,Nginx 发送证书对 443 端口的所有请求。

https://sub2.domain.tld在我通过添加新server指令对其进行配置之前,我应该如何更改我的配置以使访问失败(例如出现 503 错误)?

nginx
  • 2 2 个回答
  • 3087 Views

2 个回答

  • Voted
  1. Steffen Ullrich
    2014-12-31T14:23:29+08:002014-12-31T14:23:29+08:00

    如果您在特定 IP 上侦听 TCP 端口 443,即使您只为解析到此 IP 地址的某些域配置了 SSL,它也会接受此端口上的新 TCP 连接。一旦建立了 TCP 连接,它将进行 SSL 握手并选择最合适的证书,如果使用 SNI(即客户端在 SSL 握手中发送预期的主机名),则要么是匹配的证书,要么是另一个证书,如果它不能确定请求的主机名,或者是否没有为此名称配置证书。

    这意味着,您要么需要拥有此 IP 地址上可访问的所有域的证书,要么接受,在没有适当证书的情况下访问域将导致错误,例如无效证书。这不是 nginx 特有的,而是因为 SSL 是 TCP 之上的一层,有关请求的主机的信息仅在已建立的 TCP 连接中传输。

    • 3
  2. Best Answer
    Tero Kilkanen
    2014-12-31T15:17:08+08:002014-12-31T15:17:08+08:00

    您可以像这样添加另一个服务器块:

    server {
        listen 443 ssl default_server;
        server_name _;
    
        ssl_certificate /etc/nginx/ssl/default.crt;
        ssl_certificate_key /etc/nginx/ssl/default.key;
    
        return 503;
    }
    

    对于默认证书,您可以制作自签名证书。这将在客户端触发无效证书错误,就像 Steffen 提到的那样。如果用户接受证书,那么他将收到 503 状态码。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve