欢迎来到站长教程网!

Nginx

当前位置:主页 > 服务器教程 > Nginx >

Nginx反向署理实现支持长毗连详解

时间:2021-05-04|栏目:Nginx|点击:

媒介

Nginx upstream与后端的毗连默认为短毗连,通过HTTP/1.0向后端提倡毗连,并把请求的"Connection" header设为"close"。Nginx与前端的毗连默认为长毗连,一个用户跟Nginx成立毗连之后,通过这个长毗连发送多个请求。假如Nginx只是作为reverse proxy的话,大概一个用户毗连就需要多个向后端的短毗连。假如后端的处事器(源站或是缓存处事器)处理惩罚并发毗连本领不强的话,就大概导致瓶颈的呈现。

Nginx今朝的upstream毗连成立和获取的机制如下图。Nginx会在一开始建设connection pool(历程间不共享,可以制止锁),提供应所有向前/后的毗连。

Nginx反向代理实现支持长毗邻详解

假如要实现upstream长毗连,则每个历程需要别的一个connection pool,内里都是长毗连。一旦与后端处事器成立毗连,则在当前请求毗连竣事之后不会当即封锁毗连,而是把用完的毗连生存在一个keepalive connection pool内里,今后每次需要成立向后毗连的时候,只需要从这个毗连池内里找,假如找到符合的毗连的话,就可以直接来用这个毗连,不需要从头建设socket可能提倡connect()。这样既省下成立毗连时三次握手的时间耗损,又可以制止TCP毗连的slow start。假如在keepalive毗连池找不到符合的毗连,那就凭据本来的步调从头成立毗连。假设毗连查找时间可以忽略不计,那么这种要领必定是有益而无害的(虽然,需要少量特另外内存)。

Nginx反向代理实现支持长毗邻详解

详细如何来设计这个keepalive connection pool,差异人有差异的选择。好比Nginx今朝的第三方模块upstream keepalive(作者Maxim Dounin)利用了一个queue来做。因为upstream的处事器很大概是多个,所以大概当保持的毗连数多的时候,查找的时间大概会较长。可以给每个upstream处事器都分派一个pool(queue),缩短查找时间。可是总体来说内存操纵很快,影响不会很大。upstream keepalive模块今朝只支持memcached,可是可以重用其代码来到达对http upstream的长毗连。由于Nginx作者之前没有思量upstream的长毗连,所以在设计上要把http upstream keepalive模块化大概较量难,只能通过手动修改代码来做到。

一个完整的让upstream支持长毗连的设置示譬喻下:

#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,用于存放真实地点 
  set $real_script_name $fastcgi_script_name; 
  #假如地点与引号内的正则表达式匹配 
  if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { 
   #将文件地点赋值给变量 $real_script_name 
   set $real_script_name $1; 
   #将文件地点后的参数赋值给变量 $path_info 
   set $path_info $2; 
  } 
  #设置fastcgi的一些参数 
  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; 
 # } 
 #} 
 
} 

总结

上一篇:Nginx实现跨域利用字体文件的设置详解

栏    目:Nginx

下一篇:详解Nginx处事器和iOS的HTTPS安详通信

本文标题:Nginx反向署理实现支持长毗连详解

本文地址:www.jiaocheng88.com.cn/Nginx/61855.html

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:584415406 | 邮箱:584415406#qq.com(#换成@)

Copyright © 2015-2020 小白站长教程网 版权所有 苏ICP备20040415号-3