Home nginx Proxy Nginx WebSocket

Proxy Nginx WebSocket

Author

Date

Category

How to make traffic proxy on the website in the nginx config?
http://nginx.org/ru/docs/http/websocket.html

Suppose if the site here is located on 80 port, and Webskets at WS: //Site.ru: 2000


Answer 1, Authority 100%

The question resolved just:

in http {} add

Map $ http_upgrade $ connection_upgrade {
  Default Upgrade;
  '' Close;
}
Upstream WebSocket {
 Server your_ype: 2000;
}

in Server {} add

location / WebSocket {
  proxy_pass http: // WebSocket;
  proxy_http_version 1.1;
  proxy_set_header upgrade $ http_upgrade;
  Proxy_Set_Header Connection "Upgrade";
  proxy_set_header X-FORWARDED-FOR $ proxy_add_x_forwarded_for;
}

in nginx.conf add

keepalive_timeout 604800;
proxy_connect_timeout 604800;
proxy_send_timeout 604800;
proxy_read_timeout 604800;

Websices discover in this way

var socket = new websocket ("WS: // Your_IP_I_Domen / Websocket");

Answer 2, Authority 14%

This config does not work.
An error in the browser console is issued:

WebSocket Connection to WS: //Site.com/ws Failed: Error During
WebSocket Handshake: UNExpected Response Code: 200

This error is very, very many questions on all forums, but not a single answer.
It seems that it does not know how to proxy NGINX WS.
The problem is treated easily, just need to connect to the JS code to establish a portion of the port

var socket = new websocket ("WS: // Your_IP_I_Domen: 9999");

In this case, everything works immediately.

From here Question: Why proxy WS through Nginx?

Check ORIGIN (host connected to WS) can easily be a server itself.
Only one thing comes to mind: it is necessary to proceed to avoid A la DDOS attacks on the WS port, so that the appeals first filtered Nginx and then the WS server is already.
Because theoretically WS server will fall much faster than Nginx.

Does this make sense – to protect this WS server? Who can justify?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions