【问题标题】:nginx+socket.io+ssl: WebSocket connection to 'ws://wechat.pageguy.tech/socket.io/?EIO=3&transport=websocket' failed: Unexpected response code: 302nginx+socket.io+ssl:WebSocket 连接到 'ws://wechat.pageguy.tech/socket.io/?EIO=3&transport=websocket' 失败:意外响应代码:302
【发布时间】:2017-02-27 11:37:05
【问题描述】:

我想通过带有ssl的nginx使用socket.io配置一个node.js项目,但是我失败了,当我启动它时出现如下错误:

我花了整整一个中午的时间搜索和阅读文档,但仍然无法弄清楚如何解决这个问题。如果有人以前遇到过这个问题或对此有任何想法,请帮助我,非常感谢。

这是我的客户端 js 代码:

let socket = io.connect('//wechat.pageguy.tech/ws', {transports: ['websocket']});

这是 nginx 代码:

server {
listen       443;
server_name  wechat.pageguy.tech;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header  X-Real-IP  $remote_addr;
#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

ssl on;
ssl_certificate /etc/ssl/private/pageguy.crt;
ssl_certificate_key /etc/ssl/private/pageguy.key.unsecure;

location / {
    #root   /usr/share/nginx/html;
    #index  index.html index.htm;
    proxy_pass http://localhost:3335;
}

location /ws/ {
    proxy_pass http://localhost:3005;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}
}

我的 express 端口是 3335,我的 socket.io 端口是 3005。在我在 nginx 配置文件中添加 ssl 之前一切都很好。

【问题讨论】:

  • 您正在显示 ssl 的配置,但您在访问 ws:// 时收到错误消息,即不是 ssl(这本来是 wss://)。这意味着您显示的配置部分与此问题无关。
  • 我尝试改用“wss://”,但仍然收到相同的错误消息。我认为问题不是架构而是配置,我就是想不通。

标签: ssl express nginx websocket socket.io


【解决方案1】:

没有人帮我555,但我自己解决了问题。

在客户端js代码中:

let socket = io.connect('https//wechat.pageguy.tech/', {transports: ['websocket']});

在 nginx 配置文件中:

location /socket.io/(not /ws/ because its default value)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2021-09-16
    • 2021-07-08
    • 1970-01-01
    • 2017-06-10
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多