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连接上的任何消息都属于先前标识的客户端.