Nginx: What is the X-Forwarded-For Alternative for WebSockets?

Is there a way to pass the Nginx client id (to get a sticky session) when using WebSockets? Something like the "X-Forwarded-For" header for HTTP?

+6
source share
1 answer

Web sites begin life under the handshake of an HTTP update. Once the handshake is successfully completed, you will return to a long bi-directional Internet connection.

If you use Nginx as a proxy for websites, you can also use "X-Forwarded-For", but only with a 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"; } } 

... and some links to this page .

You configure what Nginx should send in the update request (the information you use to identify the client), and this will be your job on the server to use the information from the handshake to identify the client, and then connect the connection to the web server with your client . Based on this association, any message that arrives at this web socket connection belongs to a previously identified client.

+6
source

Source: https://habr.com/ru/post/979299/


All Articles