node.js - Nginx 为 WebSockets 处理 SSL

标签 node.js ssl nginx websocket

我是 Nginx 的新手,我感觉自己就像一只被困在核电站设施中的猴子——没有任何意义——我非常想得到一些香蕉。

无论如何,我正在使用 Nginx 服务器来处理 SSL 并将所有请求代理到 NodeJS 应用程序。除了 WebSockets 之外,一切都很好。客户端给我一个 ERR_INSECURE_RESPONSE 错误。服务器是live .我错过了什么?你有什么建议?

NodeJS

const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)

app.use(express.static('../app'))

io.on('connection', (socket) => {
  console.log('CONNECTED')
})

server.listen(5000)

Nginx 配置(取自本教程 Deploying a NodeJS app with ssl)

# HTTP — redirect all traffic to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}
# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name olmeo.us;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/olmeo.us/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/olmeo.us/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;        
    }
}

SSL 配置(包括 snippets/ssl-params.conf)

# See https://cipherli.st/ for details on this configuration
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off; # Requires nginx >= 1.5.9
ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

# Add our strong Diffie-Hellman group
ssl_dhparam /etc/ssl/certs/dhparam.pem;

客户端

io.connect('https://52.29.55.217')

最佳答案

您的 SSL 证书可能是针对给定域而不是 IP 地址提供的,并且您使用的是 IP 而不是域来连接:

io.connect('https://52.29.55.217')

除非您的证书在它涵盖的主机列表中包含该 IP 地址(极不可能),否则这将不起作用。尝试使用使用 Let's Encrypt 创建证书时使用的确切域名(不是子域,不是 IP,而是确切的域名)。

关于node.js - Nginx 为 WebSockets 处理 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45097617/

相关文章:

mysql - Node.js连接失败,无法连接到RabbitMQ

node.js - 是否可以替换或别名 Node.js 中的核心模块?

ssl - 忽略 Xamarin.Forms (PCL) 中的 SSL 证书错误

c# - 如何发送客户端特定的受信任证书颁发机构来决定用户可以在 asp.net web 表单站点上使用哪些客户端证书

nginx - 在 nginx.conf 中使用环境变量或参数

javascript - POST 请求未将文件上传到服务器

javascript - NodeJS 中 main() 的目的是什么?

WCF 通过 HTTPS -> 代理 -> HTTP

ssl - Nginx - 仅在 SSL 证书存在时启用 SSL

python - 如何使用 nginx + uwsgi 部署 Flask-restplus 应用程序