Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of nginx websocket

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the characteristics of nginx websocket". In daily operation, I believe many people have doubts about the characteristics of nginx websocket. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the characteristics of nginx websocket?" Next, please follow the editor to study!

Brief introduction of websocket Protocol

Websocket is an application layer protocol based on TCP, which is used to realize two-way communication in the application of Cramp S architecture. Rfc documents describe rfc6455.

Websocket uses the HTTP protocol when establishing connections, so the websocket protocol is based on the HTTP protocol.

At present, many card games choose to use websocket protocol for communication.

Websocket has the following characteristics:

1. Can carry on the two-way communication, the session communication is strong real-time.

two。 When a websocket connection is established, the connection can be maintained all the time, during which messages can be sent continuously until the request is closed. Avoids the non-state of HTTP (the total cost of connecting is reduced). Compared with http, after the connection is created, when the client server exchanges data, the packet header controlled by the protocol is smaller (the traffic is reduced).

After a connection is established between the 3.web server and the client, all communication depends on this dedicated protocol. In the process of communication, you can send data in any format such as JSON, XML, HTML or pictures to each other. Either the server or the client can send data directly to the other.

4. Better binary support, support for extensions.

Websocket protocol format:

0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 7 7 8 0 1 2 3 4 4 5 6 7 8 9 0 1 -+ | F | R | R | opcode | M | Payload len | Extended payload length | | I | S | S | (4) | A | (7) | (16x64) | | N | V | S | | (if payload len==126/127) | | 1 | 2 | 3 | K | +-| -+-+ | Extended payload length continued If payload len = = 127 | +-+-+ | | Masking-key If MASK set to 1 | +-+-+ | Masking-key (continued) | Payload Data | +-- ---+: Payload Data continued...: +- -+ | Payload Data continued... | +-+ nginx supports websocket description

When you open the websocket configuration using nginx, you can configure the websocket configuration in the server {} block.

Server {listen 80; server_name www.domain.com; location / {proxy_pass http://127.0.0.1:8080/; / / back-to-origin address proxy_http_version 1.1; proxy_read_timeout 600s; / / timeout setting / / enable support for websocket proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade" }}

When configured as above, it indicates that when websocket requests a connection, upgrading the connection changes the http connection to a websocket connection.

However, there is a problem here, so that websocket is configured in the server {} block, which server {} must handle websocket traffic, and all traffic destined for the backend has upgrade and connection headers.

Proxy_set_header Upgrade $http_upgrade

Proxy_set_header Connection $connection_upgrade

Nginx websocket source code optimization

When websocket is configured in server {}, websocket headers are added to all traffic destined for the back-end upstream.

If you send a request to the backend upstream, some need to add websocket headers to upgrade to websocket, while others do not. If you continue to use the native nginx configuration, there will be problems with the configuration in this scenario.

Therefore, in this scenario, the nginx source code needs to be modified and optimized to distinguish whether the traffic of client is websocket.

Ngx_http_proxy_websocket_headers: the difference is between connection and upgrade

Static ngx_keyval_t ngx_http_proxy_websocket_headers [] = {{ngx_string ("Host"), ngx_string ("$proxy_host")}, {ngx_string ("Connection"), ngx_string ("Upgrade")}, {ngx_string ("Content-Length"), ngx_string ("$proxy_internal_body_length")}, {ngx_string ("Transfer-Encoding"), ngx_string ("$proxy_internal_chunked")}, {ngx_string ("TE") Ngx_string ("")}, {ngx_string ("Keep-Alive"), ngx_string ("")}, {ngx_string ("Expect"), ngx_string ("")}, {ngx_string ("Upgrade"), ngx_string ("websocket")}, {ngx_null_string, ngx_null_string}} # ifdef NGX_WEBSOCKET_INNER// initrc = ngx_http_proxy_init_headers (cf, conf, & conf- > websocket_headers,ngx_http_proxy_websocket_headers) through headers; if (rc! = NGX_OK) {return NGX_CONF_ERROR } # endifstatic ngx_int_tngx_http_process_connection (ngx_http_request_t * r, ngx_table_elt_t * offset) {. / / get the upgrade flag if (ngx_strcasestrn (h-> value.data, "Upgrade", 7-1)) {r-> is_websocket_request | = NGX_WEBSOCKET_HEADER_CONNECTION;}.} if (r-> headers_in.upgrade = = NULL) {goto not_websocket_request } else if ((r-> is_websocket_request & NGX_WEBSOCKET_HEADER_CONNECTION) = = 0) {goto not_websocket_request / /} else if (ngx_strncasecmp (r-> headers_in.upgrade- > value.data, (u_char *) "websocket", 9) = 0) {/ / determine whether there is websocket, add the flag} else if (ngx_strcasestrn (r-> headers_in.upgrade- > value.data, "websocket", 9-1)) {r-> is_websocket_request | = NGX_WEBSOCKET_HEADER_UPGRADE } / / there are two kinds of flags, r-> websocket_request flag position bit if (r-> is_websocket_request = = (NGX_WEBSOCKET_HEADER_UPGRADE | NGX_WEBSOCKET_HEADER_CONNECTION)) {r-> websocket_request = 1X r-> http_version = NGX_HTTP_VERSION_11;} else {r-> websocket_request = 0 } ngx_http_proxy_process_header configure the flag bit of upstream static ngx_int_tngx_http_proxy_process_header (ngx_http_request_t * r) {. If (u-> headers_in.status_n = = NGX_HTTP_SWITCHING_PROTOCOLS) {u-> keepalive = 0 upgrade if (r-> headers_in.upgrade) {u-> upgrade = 1 The flag bit static voidngx_http_upstream_send_response (ngx_http_request_t * r, ngx_http_upstream_t * u) {... if (u-> upgrade) {ngx_http_upstream_upgrade (r, u); return is used in ngx_http_upstream_send_response. }...} ngx_http_upstream_upgrade sets read and write events static voidngx_http_upstream_upgrade (ngx_http_request_t * r, ngx_http_upstream_t * u) {... u-> read_event_handler = ngx_http_upstream_upgraded_read_upstream;u- > write_event_handler = ngx_http_upstream_upgraded_write_upstream;r- > read_event_handler = ngx_http_upstream_upgraded_read_downstream for upstream and downstream. R-> write_event_handler = ngx_http_upstream_upgraded_write_downstream .} the function used for each read and write time is the same, and the input parameters are different: from_upstream represents whether the backend is the backend, and do_write represents the write event below. Take the read event of upstream as an example: static voidngx_http_upstream_process_upgraded (ngx_http_request_t * r _ c is upstream (upstream). Dst is downstream (downstream) if (from_upstream) {src = upstream Dst = downstream;b = & u-> buffer;} for (;;) {/ / do_write is 0 ignore. If (do_write) {} if (size & & src- > read- > ready) {/ / src is upstream and is used to read n = src- > recv (src, b-> last, size); / / n > 0 receives bytes greater than 0, do_write is set to 1 do_write continue to write if (n > 0) {do_write = 1bot b-> last + = nscape if (from_upstream) {u-> state- > bytes_received + = n; continue } / / add three timers: upstream reads and writes are added, downstream only writes, which means if (upstream- > write- > active & &! upstream- > write- > ready) {ngx_add_timer (upstream- > write, u-> conf- > send_timeout);} else if (upstream- > write- > timer_set) {ngx_del_timer (upstream- > write) } if (upstream- > read- > active & &! upstream- > read- > ready) {ngx_add_timer (upstream- > read, u-> conf- > read_timeout);} else if (upstream- > read- > timer_set) {ngx_del_timer (upstream- > read);} if (downstream- > write- > active & &! downstream- > write- > ready) {ngx_add_timer (downstream- > write, clcf- > send_timeout);} else if (downstream- > write- > timer_set) {ngx_del_timer (ngx_del_timer > ngx_del_timer) }. At this point, the study on "what are the characteristics of nginx websocket" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report