【问题标题】:nginx + node + ssl + websockets, on one servernginx + node + ssl + websockets,在一台服务器上
【发布时间】:2018-12-12 18:42:39
【问题描述】:

我已经能够找到有关 nginx、node、ssl 和 websockets 的各种组合的指南,但从来没有一起找到,也从来没有在单个服务器上。

最终,这就是我所追求的,我不确定它是否可能:

  • 单服务器(Ubuntu 14.04)
  • 强制 HTTPS(浏览到 http://site 转发到 https://)
  • 节点应用托管在 localhost:3000
  • 节点应用使用网络套接字
  • 它是一个单页的 React 应用程序,根本没有路由,所以我不需要路由。我再说一遍,我只托管一个没有任何导航的页面。

使用以下配置,除了 websockets 之外,我的一切都正常工作 - 如果我直接浏览到节点服务器并且不使用 nginx(浏览到 http://my.domain:3000),客户端会抛出一个不会发生的错误:

bundle.js:26 WebSocket connection to 'wss://<my domain>/socket.io/?EIO=3&transport=websocket&sid=x1uQtRzF3gYYEvfIAAAi' failed: Error during WebSocket handshake: Unexpected response code: 400

server {
  listen 80;
  return 301 https://my/domain$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443;

  ssl_certificate     /path/cert.crt;
  ssl_certificate_key /path/key.key;
  ssl_session_cache shared:SSL:10m;

  server_name blaze.chat;

  location / {
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_pass              http://localhost:3000;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Host $host;
    proxy_redirect          http://localhost:3000 https://my.domain;
  }

}

【问题讨论】:

    标签: node.js ssl nginx websocket socket.io


    【解决方案1】:

    好吧,搞定了...发现很多文章都显示了类似的内容,但缺少这些关键行:

    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
    

    就我而言,没有这些行,websockets 将无法工作,这与许多其他提出类似问题的帖子相反。不知道有什么区别。具有讽刺意味的是,它在 Nginx Websocket Proxying doco 中被列为要求...应该从那里开始,我的错。

    http://nginx.org/en/docs/http/websocket.html

    旁注,我只是在/ 的根路径上使用它,效果很好。

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2018-05-30
      • 2016-01-20
      • 2020-04-02
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2021-04-13
      相关资源
      最近更新 更多