Nginx:什么是X-Forwarded-For WebSockets的替代品?

在使用WebSockets时,有没有办法将客户端身份传递给Nginx(以获得粘性会话)?类似于HTTP的“X-Forwarded-For”标题?
最佳答案
Websockets在HTTP升级握手下开始他们的生活.握手成功完成后,您将获得长时间运行的双向websocket连接.

如果您使用Nginx作为websockets的代理,那么您也可以使用“X-Forwarded-For”,但仅限于握手.参见例如this simple configuration:

# WebSocket Proxy
#
# Simple forwarding of unencrypted HTTP and WebSocket to a different host
# (you can even use a different host instead of localhost:8080)

server {
    listen 80;

    # host name to respond to
    server_name ws.example.com;

    location / {
        # switch off logging
        access_log off;

        # redirect all HTTP traffic to localhost:8080
        proxy_pass http://localhost:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

……以及this page的一些参考资料.

您可以配置Nginx应该在升级请求中发送的内容(用于标识客户端的信息),并且后端服务器的工作是使用握手中的信息来标识客户端,然后将websocket连接关联到客户端.基于该关联,该websocket连接上的任何消息都属于先前标识的客户端.

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。