跳转到内容
彼岸论坛
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 请问大佬们如何正确配置 Nginx 使其能够启用 SSE(server-sent-events)


小天管理

已推荐帖子

我在本地测试完全没有问题,本地的 nginx 监听 80 端口转发到 express 也是完全没有问题,但是直到启用 ssl ,开启 443 端口后,服务端发送消息,客户端就无法接收了,完全找不到头绪,网上的例子都试了就是没有用 以下是我的 nginx 配置

server {
  listen 443 ssl http2;
  listen [::]:443 http2 ssl;
  server_name  www.mydomain.com;

  ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/www.mydomain.com/privkey.pem;

  access_log off;

  location / {
    proxy_pass http://127.0.0.1:3000/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_buffering off;
    proxy_cache off;
    proxy_read_timeout 300s;
    chunked_transfer_encoding off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

我的服务端代码

eventRouter.get('/event/:userID', async (req, res) => {
  const headers: OutgoingHttpHeaders = {
    'Content-Type': 'text/event-stream',
    'Connection': 'keep-alive',
    "Cache-Control": 'no-cache',
    "X-Accel-Buffering": 'no'
  }
  res.writeHead(200, headers)
  res.write(JSON.stringify({ type: 'handshake' } satisfies IEventMessage))
}

有知道的大佬吗,实在不行我滚回 long-loop 了

意见的链接
分享到其他网站

加入讨论

您现在可以发表并稍后注册. 如果您是会员,请现在登录来参与讨论.

游客
回复主题...

×   粘贴为富文本.   粘贴为纯文本来代替

  只允许使用75个表情符号.

×   您的链接已自动嵌入.   显示为链接来代替

×   您之前的内容已恢复.   清除编辑器

×   您无法直接粘贴图片.要从网址上传或插入图片.

  • 游客注册

    游客注册

  • 会员

  • 最新的状态更新

    没有最新的状态更新
  • 最近查看

    • 没有会员查看此页面.
×
×
  • 创建新的...