【问题标题】:Nginx proxy to node.js server SSL ERR_SSL_PROTOCOL_ERRORNginx 代理到 node.js 服务器 SSL ERR_SSL_PROTOCOL_ERROR
【发布时间】:2021-07-01 22:33:08
【问题描述】:

编辑:

我已经验证 nodejs 在正确的端口上运行,在 http 上,我也尝试过使用和不使用:

app.use('trust proxy', true);

编辑 2:

我关闭了nodejs服务器并尝试仅使用nginx提供静态文件,并且错误仍然存​​在,所以显然这与nginx和我的ssl证书有关。

我的域是来自 freenom 的免费域,并且 ssl 证书是使用 certbot 生成的。

原文:

我有一个 nodejs 服务器正在运行,并且想使用 nginx 和代理到 nodejs 服务器。 (Nginx https -> nodejs http)

运行nginx -t 不会出错。

在ubuntu上20.04.2nginx 1.18.0node 14.5.5

我已验证我的网站通过 http(在端口 3000 上)可以正常工作,但在 https 上通过浏览器访问时出现以下错误:

ERR_SSL_PROTOCOL_ERROR

如果我使用 openssl cli 尝试连接,我会得到这个

openssl s_client -connect my_domain.com:443 -servername my_domain.com
CONNECTED(00000003)
139662603941184:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:331:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 5 bytes and written 310 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---

/etc/nginx/conf.d/ssl.conf

server {
    listen 443 ssl;

    ssl_certificate     /server/resources/cert.pem;
    ssl_certificate_key /server/resources/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
}

【问题讨论】:

  • 如果您能看到任何错误,请添加有关该错误的更多详细信息
  • 此错误通常与访问未启用 SSL 的服务器有关。检查 nginx 的日志文件,尤其是错误日志。另请注意,nginx 配置的其他部分中的错误可能会导致此类问题,例如某些服务器也在侦听端口 443 但未启用 ssl。
  • @SteffenUllrich access.logerror.log 都是空的
  • 不太可能,您可能查看了错误的文件。或者,也许您禁用了日志记录。至少应该有关于服务器启动的信息,并且通常会有关于 ssl 错误的信息。或者my_domain.com:443实际上并没有访问你配置的服务器,即DNS没有解析到服务器的IP地址。
  • @SteffenUllrich 我验证日志记录已启用并检查了正确的日志文件,并更改了my_domain 并尝试使用 ip,仍然产生相同的错误。

标签: node.js nginx ssl proxy


【解决方案1】:

如果您使用 Cloudflare,可能 Cloudflare 尚未为您颁发 SSL 证书,或者 Cloudflare 无法通过安全连接连接到源站。检查您的仪表板。

【讨论】:

    【解决方案2】:

    下面是nginx.conf的工作配置

    我还使用 certbot +letsencrypt 设置了 SSL。

    server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com;
        return 301 https://$server_name$request_uri;
    }
    
    
    server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name example.com www.example.com;
        root "/home/ubuntu/domain/code/directory/path/";
        index index.html index.htm;
        client_max_body_size 75M;   # adjust to taste
    
        location /api {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_read_timeout 600s;
        }
    
        location / {
            try_files $uri $uri/ /index.html;
        }
    
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_session_timeout 1h;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        add_header Strict-Transport-Security “max-age=15768000” always;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    }
    

    我猜上面的配置可能会解决你的问题。

    网址为https://www.example.com/api/ping 重定向到服务器上的http://localhost:3000/api/ping

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2016-03-22
      • 2019-07-27
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多