Nginx反向代理實(shí)現(xiàn)支持長(zhǎng)連接詳解
前言
Nginx upstream與后端的連接默認(rèn)為短連接,通過(guò)HTTP/1.0向后端發(fā)起連接,并把請(qǐng)求的"Connection" header設(shè)為"close"。Nginx與前端的連接默認(rèn)為長(zhǎng)連接,一個(gè)用戶跟Nginx建立連接之后,通過(guò)這個(gè)長(zhǎng)連接發(fā)送多個(gè)請(qǐng)求。如果Nginx只是作為reverse proxy的話,可能一個(gè)用戶連接就需要多個(gè)向后端的短連接。如果后端的服務(wù)器(源站或是緩存服務(wù)器)處理并發(fā)連接能力不強(qiáng)的話,就可能導(dǎo)致瓶頸的出現(xiàn)。
Nginx目前的upstream連接建立和獲取的機(jī)制如下圖。Nginx會(huì)在一開(kāi)始創(chuàng)建connection pool(進(jìn)程間不共享,可以避免鎖),提供給所有向前/后的連接。

如果要實(shí)現(xiàn)upstream長(zhǎng)連接,則每個(gè)進(jìn)程需要另外一個(gè)connection pool,里面都是長(zhǎng)連接。一旦與后端服務(wù)器建立連接,則在當(dāng)前請(qǐng)求連接結(jié)束之后不會(huì)立即關(guān)閉連接,而是把用完的連接保存在一個(gè)keepalive connection pool里面,以后每次需要建立向后連接的時(shí)候,只需要從這個(gè)連接池里面找,如果找到合適的連接的話,就可以直接來(lái)用這個(gè)連接,不需要重新創(chuàng)建socket或者發(fā)起connect()。這樣既省下建立連接時(shí)三次握手的時(shí)間消耗,又可以避免TCP連接的slow start。如果在keepalive連接池找不到合適的連接,那就按照原來(lái)的步驟重新建立連接。假設(shè)連接查找時(shí)間可以忽略不計(jì),那么這種方法肯定是有益而無(wú)害的(當(dāng)然,需要少量額外的內(nèi)存)。

具體如何來(lái)設(shè)計(jì)這個(gè)keepalive connection pool,不同人有不同的選擇。比如Nginx目前的第三方模塊upstream keepalive(作者M(jìn)axim Dounin)使用了一個(gè)queue來(lái)做。因?yàn)閡pstream的服務(wù)器很可能是多個(gè),所以可能當(dāng)保持的連接數(shù)多的時(shí)候,查找的時(shí)間可能會(huì)較長(zhǎng)??梢越o每個(gè)upstream服務(wù)器都分配一個(gè)pool(queue),縮短查找時(shí)間。但是總體來(lái)說(shuō)內(nèi)存操作很快,影響不會(huì)很大。upstream keepalive模塊目前只支持memcached,但是可以重用其代碼來(lái)達(dá)到對(duì)http upstream的長(zhǎng)連接。由于Nginx作者之前沒(méi)有考慮upstream的長(zhǎng)連接,所以在設(shè)計(jì)上要把http upstream keepalive模塊化可能比較難,只能通過(guò)手動(dòng)修改代碼來(lái)做到。
一個(gè)完整的讓upstream支持長(zhǎng)連接的配置示例如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
client_max_body_size 20M;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
upstream aauCfg_backend {
server 127.0.0.1:97;
keepalive 16;
}
upstream HFC_backend {
server 127.0.0.1:8090;
keepalive 16;
}
upstream manager_backend {
server 127.0.0.1:8095;
keepalive 16;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root html/tools;
index index.html index.htm index.php;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
if (!-e $request_filename) {
#rewrite ^/(.*)$ /index.php/$1 last;
#break;
rewrite ^/(.*)$ /index.php/$1;
}
}
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location ^~ /aauCfg/ {
#proxy_pass http://$remote_addr:97$request_uri;
proxy_pass http://aauCfg_backend;
}
location ^~ /HFC/ {
#proxy_pass http://$remote_addr:8090$request_uri;
proxy_pass http://HFC_backend;
}
location ^~ /manager/ {
#proxy_pass http://$remote_addr:8095$request_uri;
proxy_pass http://manager_backend;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ .php
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include fastcgi_params;
#定義變量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定義變量 $real_script_name,用于存放真實(shí)地址
set $real_script_name $fastcgi_script_name;
#如果地址與引號(hào)內(nèi)的正則表達(dá)式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#將文件地址賦值給變量 $real_script_name
set $real_script_name $1;
#將文件地址后的參數(shù)賦值給變量 $path_info
set $path_info $2;
}
#配置fastcgi的一些參數(shù)
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
參考:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
相關(guān)文章
Nginx中return和rewrite的寫(xiě)法及區(qū)別淺析
在Nginx中,return和rewrite都用于重定向,但有所區(qū)別,return立即終止請(qǐng)求并返回指定HTTP狀態(tài)碼,不額外請(qǐng)求,rewrite則重寫(xiě)URI并引發(fā)新請(qǐng)求,需要的朋友可以參考下2024-09-09
SpringBoot前端后端分離之Nginx服務(wù)器下載安裝過(guò)程
Nginx是一款輕量級(jí)的Web服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,這篇文章主要介紹了SpringBoot前端后端分離之Nginx服務(wù)器,需要的朋友可以參考下2022-08-08
詳解Nginx服務(wù)器的配置中開(kāi)啟文件Gzip壓縮的方法
這篇文章主要介紹了Nginx服務(wù)器的配置中開(kāi)啟文件Gzip壓縮的方法,可以對(duì)CSS和JavaScript以及各種圖片等web傳輸?shù)奈募M(jìn)行壓縮,需要的朋友可以參考下2016-01-01
Nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼
這篇文章主要介紹了Nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Nginx優(yōu)化設(shè)計(jì)方案小結(jié)
本文主要介紹了Nginx優(yōu)化設(shè)計(jì)方案小結(jié),幫助大家在nginx的使用和優(yōu)化中提供一個(gè)參考的方向,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Dockerfile打包nginx鏡像的實(shí)現(xiàn)步驟
本文主要介紹了Dockerfile打包nginx鏡像的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
Linux下Nginx服務(wù)設(shè)置開(kāi)機(jī)自啟動(dòng)的命令詳解
這篇文章主要介紹了Linux下Nginx服務(wù)設(shè)置開(kāi)機(jī)自啟動(dòng)的命令,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-09-09

