gpt4 book ai didi

javascript - Nginx add_header Sec-WebSocket-Protocol 不起作用

转载 作者:行者123 更新时间:2023-11-30 16:28:26 35 4
gpt4 key购买 nike

我使用 nginx 作为我的 WebSocket 流量的代理,它运行良好。我的一个应用需要使用 STOMP,为此我选择了 STOMP js 库,它试图通过 Sec-WebSocket-Protocol header 协商协议(protocol),但我在 nginx 响应中添加这个 header 时遇到了麻烦。

nginx version: nginx/1.6.2

下面是我的服务器 block :

server {
listen 80;
server_name mware.example.com;

client_max_body_size 100M;
keepalive_timeout 5;

root /srv/applications;
autoindex off;

location / {
# WebSocket
location = /ws {
proxy_read_timeout 10m;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:8000/;
proxy_redirect http://127.0.0.1:8000/ /;

# Custom headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#add_header Sec-WebSocket-Protocol $http_sec_websocket_protocol;
add_header Sec-WebSocket-Protocol 'v10.stomp, v11.stomp';
}
}
}

因此尝试通过 $http_sec_websocket_protocol 变量添加 header 并逐字指定它,但这些对我都不起作用。以下是 localhost 上的数据包捕获。

发送的数据包:

T 10.3.2.100:36798 -> y.y.y.y:80 [AP]
GET /ws HTTP/1.1.
Host: mware.example.com.
Connection: Upgrade.
Pragma: no-cache.
Cache-Control: no-cache.
Upgrade: websocket.
Origin: file://.
Sec-WebSocket-Version: 13.
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36.
Accept-Encoding: gzip, deflate, sdch.
Accept-Language: en-US,en;q=0.8.
Cookie: __utma=82131859.1920436741.1415282304.1421757291.1421760088.10; session_id_api=x.x.x.x-87eeca97-c6db-4010-bef8-f4875a6feb52; session_id_portal=x.x.x.x-ace62281-2446-4708-aa9d-4390eb41e02a.
Sec-WebSocket-Key: bGN7hB4fw7/JuF6A5vSf6A==.
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits.
Sec-WebSocket-Protocol: v10.stomp, v11.stomp.

Nginx 响应:

T y.y.y.y:80 -> 10.3.2.100:36798 [AP]
HTTP/1.1 101 Switching Protocols.
Server: nginx/1.6.2.
Date: Tue, 17 Nov 2015 09:35:18 GMT.
Connection: upgrade.
Upgrade: websocket.
Sec-WebSocket-Accept: D34d7GBjjQkSC8jrjpixNmMxDMc=.

您可以看到缺少 Sec-WebSocket-Protocol header 。

在应用程序级别,我通过 nginx 接收格式正确的数据包,添加了自定义 header 等。

Upgrade: websocket
Connection: upgrade
Host: mware.example.com
X-Real-IP: x.x.x.x
REMOTE_PORT: 36798
X-Forwarded-For: x.x.x.x
Pragma: no-cache
Cache-Control: no-cache
Origin: file://
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: __utma=82131859.1920436741.1415282304.1421757291.1421760088.10; session_id_api=x.x.x.x-87eeca97-c6db-4010-bef8-f4875a6feb52; session_id_portal=x.x.x.x-ace62281-2446-4708-aa9d-4390eb41e02a
Sec-WebSocket-Key: bGN7hB4fw7/JuF6A5vSf6A==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: v10.stomp, v11.stomp

最佳答案

http://nginx.org/r/add_header

Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.

您使用 101 代码进行响应。

从 1.7.5 版开始就有 always 标志,但您使用的是旧版本。您也可以考虑使用第三方模块,例如 headers_morelua

关于javascript - Nginx add_header Sec-WebSocket-Protocol 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754098/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com