Configuring Nginx Reverse proxy WebSocket, taking proxy novnc as an example

Source: Internet
Author: User
Tags imap what is nginx nginx reverse proxy

What is Nginx?

Nginx (engine x) is a high-performance HTTP and reverse proxy server, also a IMAP/POP3/SMTP server.

Nginx is a lightweight Web server/reverse proxy server and e-mail (IMAP/POP3) proxy server that is released under a bsd-like protocol. It is characterized by the possession of less memory, concurrency is strong, in fact, nginx concurrency is actually in the same type of Web server performance better.

What is WebSocket?

The WebSocket protocol is a choice for creating a webapp that requires real-time bidirectional communication between the client and server side. As part of the HTML5, WebSocket is easier to develop than the original approach to developing such apps.

The WebSocket protocol is a new network protocol based on TCP. It implements browser-to-server full-duplex (Full-duplex) communication-allowing the server to proactively send information to the client.

During the implementation of the WebSocket connection, the WebSocket connection request is made via the browser, and then the server responds, which is often referred to as a "handshake". In the WebSocket API, the browser and server only need to do a handshake, and then a fast channel is formed between the browser and the server. The data can be transmitted to each other directly between the two. In this WebSocket agreement, there are two major benefits to our instant service:

1. Header. The header that communicates with each other is very small-about 2 Bytes.

2. Server Push. Server push, the server no longer passively receives the browser's request to return the data, but is actively pushed to the browser when there is new data.

What is NOVNC?

NOVNC provides a HTML5 canvas on a Web page that accesses the VNC service provided by the Vncserver on the machine and requires a conversion of TCP to WebSocket to be displayed in the HTML5. Web page is a client, similar to win under the Vncviewer, but this time is not to fill the bare vnc service Ip+port, but by the NOVNC provided by the WebSockets agent, on the NOVNC proxy server to configure each VNC service, NOVNC provides an identity to the VNC service configured by the reverse proxy.

WebSocket Agent

To convert a connection between a client and a server from http/1.1 to WebSocket, use the protocol switching mechanism provided in http/1.1.

However, there is a subtle point: because "Upgrade" is a hop-over header, it is not passed from the client to the proxy server. With a forward proxy, customers can use the Connect method to circumvent this problem. However, this does not apply to reverse proxies because the client does not know any proxy servers and requires special processing on the proxy server.

Starting with version 1.3.13, Nginx implements a special mode of operation, if the proxy server returns a response to Code 101 (Interchange Protocol), the client and the proxy server are tunneled, and the client passes the "Upgrade" request header in the request.

As mentioned above, the hop-on headings, including "Upgrade" and "Connection", are not passed from the client to the proxy server, so in order for the proxy server to know the intent of the client to switch the protocol to WebSocket, these headings must be explicitly passed:

http {    map  $http _upgrade  $connection _upgrade {         default upgrade;         '        close;    }    server {         listen 80;  #修改监听的端口          server_name _;        location / {             proxy_pass   #修改为需要被反向代理的WebSocket的IP和端口号             proxy_http_version 1.1;             proxy_set_header Upgrade  $http _ upgrade;            proxy_set_header  connection  $connection _upgrade;        }    } 

By default, if the proxy server does not transmit any data within 60 seconds, the connection is closed. This timeout can be increased by proxy_read_timeout instructions. Alternatively, the proxy server can be configured to periodically send a websocket ping frame to reset the timeout and check whether the connection still exists.

Example--taking proxy novnc as an example

1) Edit Nginx configuration file


2) Restart Nginx Service

Service Nginx Restart

3) Testing

Connection Successful

Configure Nginx Reverse proxy websocket, take proxy novnc as an example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.