Nginx:WebSockets的X-Forwarded-For替代品是什么? [英] Nginx: What is the X-Forwarded-For alternative for WebSockets?

查看:238
本文介绍了Nginx:WebSockets的X-Forwarded-For替代品是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WebSockets时,是否可以将客户端身份传递给Nginx(以获得粘性会话)?类似于HTTP的"X-Forwarded-For"标头?

Is there any way to pass client identity to Nginx (to get sticky session) when using WebSockets? Something similar to "X-Forwarded-For" header for HTTP ?

推荐答案

Websocket通过HTTP升级握手开始其生活.握手成功完成后,您将恢复长期运行的双向Websocket连接.

Websockets start their life under a HTTP upgrade handshake. Once the handshake is successfully completed you get back a long running bidirectional websocket connection.

如果您将Nginx用作websocket的代理,则也可以使用"X-Forwarded-For",但只能在握手时使用.参见例如此简单配置:

If you use Nginx as a proxy for websockets then you can also use "X-Forwarded-For" but only on the handshake. See for example 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";
    }
}

...以及此页面上的一些引用.

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

You configure what Nginx should send along in the upgrade request (the info you use to identify the client) and it will be your backend server's job to use the information from the handshake to identify the client and then associate the websocket connection to your client. Based on that association, any message that comes on that websocket connection belongs to the previously identified client.

这篇关于Nginx:WebSockets的X-Forwarded-For替代品是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆