我想让我的使用ws
npm模块的节点应用程序使用ssl websockets(wss://)。最重要的是,我想使用在certbot中使用nginx设置的ssl。
我让节点websocket监听端口8080,尽管我可以直接连接到该端口,但是由于该站点是通过ssl服务的,因此由于未加密,因此会引发错误。
对于客户端javascript,您可以将呼叫路由到wss://examplesite.com/websocket
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8080;
}
server {
server_name examplesite.com;
location /websocket {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
# after this is just an example of the rest of the nginx config for a node server on 8675
# that has a static build directory
location / {
proxy_pass http://127.0.0.1:8675;
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_set_header X-Real-IP $remote_addr;
}
location ~ \.(gif|jpg|png|js|txt|html|mp3|css|woff2)$ {
root /root/examplesite.com/build/;
expires 30d;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/examplesite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/examplesite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
您可以使用https://www.npmjs.com/package/wscat测试您的本地ws:// ...:8080和您的wss://.../websocket连接