【问题标题】:SSL Certificate Invalid on back end Node App (Nginx Reverse Proxy)后端节点应用程序上的 SSL 证书无效(Nginx 反向代理)
【发布时间】:2019-10-21 22:45:44
【问题描述】:

所以我在使用 SSL 证书时遇到了一些问题。

我有一个在端口 80 上运行的 react 应用程序。 以及在端口 443 上运行的节点后端。

我有一个指向指向反应应用程序的 IP (xx.xx.xxx.xx) 的域。我正在使用 nginx 代理从前端到后端的请求,因为我在同一台服务器上都有。

这里是 nginx 配置:

server {
  listen 80 ssl;
  server_name xx.xx.xxx.xx;
  ssl_client_certificate /etc/letsencrypt/live/domain.com/cert.pem;
  ssl_certificate /etc/letsencrypt/live/domain.com/cert.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  root /home/ubuntu/build;
  index index.html;

  access_log /var/log/nginx/build.access.log;
  error_log /var/log/nginx/build.error.log;
  location / {
    try_files $uri /index.html =404;
  }
}

upstream backend {
  server 127.0.0.1:443;
  server 127.0.0.1:443 max_fails=1 fail_timeout=30s backup;
  keepalive 64;
}

server {
  listen 443 ssl;
  server_name xx.xx.xxx.xx;
  ssl_client_certificate /etc/letsencrypt/live/domain.com/cert.pem;
  ssl_certificate /etc/letsencrypt/live/domain.com/cert.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  keepalive_timeout 10;
  location / {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    proxy_set_header Connection '';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
   }
}

当向后端发出请求时,我收到以下错误:

net::ERR_CERT_COMMON_NAME_INVALID

这是因为证书对“domain.com”有效,而不是后端正在运行的 IP(我知道您必须为证书使用完全合格的域)。

我的问题是我可以做些什么不同的事情(使用 nginx)来允许我的请求通过反向代理上的 https 发出?

【问题讨论】:

  • 试试proxy_pass httpS://backend;?
  • 已经尝试过@ThanhNguyenVan 但谢谢!

标签: ssl nginx reverse-proxy lets-encrypt nginx-reverse-proxy


【解决方案1】:

您以不同的方式使用标准端口 80 和 443。这些端口是您服务器的入口点,不建议用作在反向代理中运行的端口。

使用反向代理时,我们会将其他端口映射到端口 80 或端口 443,以便分别通过 HTTP 或 HTTPS 公开访问。

如果我们想通过 HTTPS 访问所有内容,我们需要通过反向代理将 react 和 node 应用程序都映射到 443,并将所有 HTTP 访问重定向到 HTTPS。

所以建议的修复步骤:

1) 使用不同的端口,比如 3000 用于反应,3001 用于节点。

2) 配置您的服务器块侦听端口 80 以重定向到 https,如 return 301 https://<yourdomainhere.com>

3) 删除端口 80 服务器块中的 ssl 行。仅在侦听端口 443 的服务器块内使用它们

4) 修改您的 upstream {} 块以将端口 3001 用于节点应用程序。保留proxy_pass http://backend;的使用,这样就可以了。

5) 在侦听端口 443 的服务器块内添加一个带有 proxy_pass http://localhost:3000; 的新位置块。您现在将有两个位置块,一个用于反应,一个用于节点。

6) 使用yourdomainhere.com 定义每个块的server_name,因为通常不允许使用 SSL 证书颁发 IP 地址。我建议使用不同的服务器块将 IP 地址重定向到带有 HTTPS 前缀的域

7) 检查错误,然后重启 nginx。

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 2021-02-04
    • 2017-10-04
    • 2017-06-15
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    相关资源
    最近更新 更多