nginxwebsocket有什么特點

這篇文章主要介紹“nginx websocket有什么特點”,在日常操作中,相信很多人在nginx websocket有什么特點問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nginx websocket有什么特點”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

創(chuàng)新互聯(lián)建站-專業(yè)網站定制、快速模板網站建設、高性價比肥城網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式肥城網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋肥城地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

websocket協(xié)議簡介

websocket是基于TCP的應用層協(xié)議,用于在C/S架構的應用中實現雙向通信,rfc文檔說明rfc6455。
websocket在建立連接時會使用HTTP協(xié)議,所以websocket協(xié)議是基于HTTP協(xié)議實現的。

目前很多卡牌類的游戲選擇使用websocket協(xié)議進行通信。

websocket具備如下特點:

1.可以進行雙向通信,會話通信實時性強。

2.建立起websocket連接,可以一直保持連接,在此期間可以源源不斷的發(fā)送消息,直到關閉請求。避免了HTTP的非狀態(tài)性(連接時總開銷減少)。和http相比連接創(chuàng)建后,客戶端服務端進行數據交換時,協(xié)議控制的數據包頭部較小(通信量減少)。

3.web服務器與客戶端之間建立起連接后,所有的通信都依靠這個專用協(xié)議進行。通信過程中可互相發(fā)送JSON、XML、HTML或圖片等任意格式的數據。不論服務器還是客戶端,任意一方都可直接向對方發(fā)送數據。

4.更好的二進制支持,支持擴展。

websocket協(xié)議格式:

0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
|N|V|V|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支持websocket說明

在使用nginx開啟websocket配置時,可以通過在server{}塊中配置websocket配置。

server {
      listen   80;
      server_name www.domain.com;
      location / {
         proxy_pass   http://127.0.0.1:8080/; //回源地址
     proxy_http_version 1.1;
         proxy_read_timeout   600s; //超時設置
         //啟用支持websocket
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
      }
}

如上配置它表明websocket請求連接的時候,升級連接將http連接變成websocket連接。

但這里會有一個問題,這樣在server{}塊中配置websocket,該server{}必須處理websocket流量,所有發(fā)往后端的流量都帶上upgrade和connection頭。

proxy_set_header  Upgrade $http_upgrade;

proxy_set_header  Connection $connection_upgrade;

nginx websocket源碼優(yōu)化

在server{}中配置websocket時,所有發(fā)往后端upstream的流量會添加websocket頭。

假如發(fā)往后端upstream的請求,有的需要添加websocket頭升級為websocket,有的請求不需要,如果繼續(xù)使用原生的nginx配置,會導致該場景該配置,出現問題。

所以該場景下需改動和優(yōu)化nginx源碼,來區(qū)分client的流量是否是websocket。

ngx_http_proxy_websocket_headers:區(qū)別在于connection和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

// 通過headers來init

rc = ngx_http_proxy_init_headers(cf, conf, &conf->websocket_headers,

ngx_http_proxy_websocket_headers);

if (rc != NGX_OK) {

return NGX_CONF_ERROR;

}

#endif

static ngx_int_t

ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,

ngx_uint_t offset)

{

...

// 獲取upgrade標志

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) {

// 判斷是否含有websocket,添加標志

} else if (ngx_strcasestrn(r->headers_in.upgrade->value.data, "websocket", 9 - 1)) {

r->is_websocket_request |= NGX_WEBSOCKET_HEADER_UPGRADE;

}

// 兩種標志都有,r->websocket_request 標志位置位

if (r->is_websocket_request == (NGX_WEBSOCKET_HEADER_UPGRADE | NGX_WEBSOCKET_HEADER_CONNECTION)) {

r->websocket_request = 1;

r->http_version = NGX_HTTP_VERSION_11;

} else {

r->websocket_request = 0;

}

ngx_http_proxy_process_header配置upstream的標志位

static ngx_int_t

ngx_http_proxy_process_header(ngx_http_request_t *r)

{

...

if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS) {

u->keepalive = 0;

if (r->headers_in.upgrade) {

u->upgrade = 1;

}

}

...

}

在ngx_http_upstream_send_response種使用該標志位

static void

ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)

{

...

if (u->upgrade) {

ngx_http_upstream_upgrade(r, u);

return;

}

...

}

ngx_http_upstream_upgrade給上下游設置讀寫事件

static void

ngx_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;

r->write_event_handler = ngx_http_upstream_upgraded_write_downstream;

...

}

每個讀寫時間所用的函數都是一樣的,入參不同:from_upstream代表是否是后端,do_write代表是否是寫事件

下面以upstream的讀事件為例

static void

ngx_http_upstream_process_upgraded(ngx_http_request_t *r,

ngx_uint_t from_upstream, ngx_uint_t do_write)

{

...

// from_upstream為1,src為upstream(上游),dst為downstream(下游)

if (from_upstream) {

src = upstream;

dst = downstream;

b = &u->buffer;




}

for ( ;; ) {

// do_write為0忽略。

if (do_write) {}

if (size && src->read->ready) {

// src為upstream,用來讀

n = src->recv(src, b->last, size);

// n >0 接收大于0的字節(jié)數,do_write置為1,continue進行寫入

if (n > 0) {

do_write = 1;

b->last += n;




if (from_upstream) {

u->state->bytes_received += n;

}

continue;

}

// 加入三個計時器,upstream讀寫都加,downstream只加寫入,相當于除了client的接收沒加計時器,都加了

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(downstream->write);

}

...

}

到此,關于“nginx websocket有什么特點”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

網頁題目:nginxwebsocket有什么特點
URL地址:http://bm7419.com/article30/gihspo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供全網營銷推廣、標簽優(yōu)化用戶體驗、品牌網站設計、定制網站企業(yè)建站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站建設