Nodejs + socket. io use nginx Reverse proxy to prompt 400 Bad Request

Source: Internet
Author: User
Tags socket nginx reverse proxy

Nginx 1.3.12 and later versions support websocket. Therefore, update nginx first. I am using version 1.6.0.

Server {
Listen 80;
Server_name 111cn.net www.111cn.net;
Root/www/web/123/public_html;
Index index.html index. php index.htm;
Location /{
Proxy_pass http: // 127.0.0.1: 8000;
......
Proxy_http_version 1.1;
Proxy_set_header Upgrade $ http_upgrade;
Proxy_set_header Connection "upgrade ";
     }
}

The red part is the most critical content. Add these three lines to your. conf file.

The original English text is as follows:

WebSocket proxying

To turn a connection between a client and server from HTTP/1.1 into WebSocket, the protocol switch mechanic available in HTTP/1.1 is used.

There is one subtlety however: since the "Upgrade" is a hop-by-hop header, it is not passed from a client to proxied server. with forward proxying, clients may use the CONNECT method to circumvent this issue. this does not work with reverse proxying however, since clients are not aware of any proxy servers, and special processing on a proxy server is required.

Since version 1.3.13, nginx implements special mode of operation that allows setting up a tunnel between a client and proxied server if the proxied server returned a response with the code 101 (Switching Protocols ), and the client asked for a protocol switch via the "Upgrade" header in a request.

As noted above, hop-by-hop headers including "Upgrade" and "Connection" are not passed from a client to proxied server, therefore in order for the proxied server to know about the client's intention to switch a protocol to WebSocket, these headers have to be passed explicitly:

Location/chat /{
Proxy_pass http: // backend;
Proxy_http_version 1.1;
Proxy_set_header Upgrade $ http_upgrade;
Proxy_set_header Connection "upgrade ";
}

A more sophisticated example in which a value of the "Connection" header field in a request to the proxied server depends on the presence of the "Upgrade" field in the client request header:

Http {
Map $ http_upgrade $ connection_upgrade {
Default upgrade;
''Close;
    }

Server {
...

Location/chat /{
Proxy_pass http: // backend;
Proxy_http_version 1.1;
Proxy_set_header Upgrade $ http_upgrade;
Proxy_set_header Connection $ connection_upgrade;
        }
    }

By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds. this timeout can be increased with the proxy_read_timeout directive. alternatively, the proxied server can be configured to periodically send WebSocket ping frames to reset the timeout and check if the connection is still alive.

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: info-contact@alibabacloud.com 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.