Nginx核心配置-長連接配置


                Nginx核心配置-長連接配置

                                       作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

 

 

一.長連接配置參數說明

keepalive_timeout number; 
  設定保持連接超時時長,0表示禁止長連接,默認為75s,通常配置在http字段作為站點全局配置。

keepalive_requests number;
  在一次長連接上所允許請求的資源的最大數量,默認為100次。

 

二.配置長連接實戰

1>.編輯主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 

events {
    worker_connections  100000;
    use epoll;
    accept_mutex on;
    multi_accept on; 
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    gzip  on;
    charset utf-8;

    #開啟長連接后,返回客戶端的會話保持時間為60s,單次長連接累計請求達到指定次數請求或65秒就會被斷開,后面的60為發送給客戶端應答報文頭部中顯示的超時時間設置為60s:如不設置
客戶端將不顯示超時時間。    keepalive_timeout  65 60;

    #在一次長連接上所允許請求的資源的最大數量
    keepalive_requests 3;
    
    #導入其他路徑的配置文件
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加載配置文件內容

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      2840     1  0 09:37 ?        00:00:00 nginx: master process nginx
nginx     4604  2840  0 13:23 ?        00:00:00 nginx: worker process
nginx     4605  2840  0 13:23 ?        00:00:00 nginx: worker process
nginx     4606  2840  0 13:23 ?        00:00:00 nginx: worker process
nginx     4607  2840  0 13:23 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      2840     1  0 09:37 ?        00:00:00 nginx: master process nginx
nginx     4769  2840  1 14:12 ?        00:00:00 nginx: worker process
nginx     4770  2840  1 14:12 ?        00:00:00 nginx: worker process
nginx     4771  2840  1 14:12 ?        00:00:00 nginx: worker process
nginx     4772  2840  1 14:12 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

 

三.測試長連接配置

1>.安裝測試軟件telnet

[root@node108.yinzhengjie.org.cn ~]# yum -y install telnet
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package telnet.x86_64 1:0.17-64.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                 Arch                                    Version                                        Repository                             Size
============================================================================================================================================================================
Installing:
 telnet                                  x86_64                                  1:0.17-64.el7                                  base                                   64 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package

Total download size: 64 k
Installed size: 113 k
Downloading packages:
telnet-0.17-64.el7.x86_64.rpm                                                                                                                        |  64 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:telnet-0.17-64.el7.x86_64                                                                                                                              1/1 
  Verifying  : 1:telnet-0.17-64.el7.x86_64                                                                                                                              1/1 

Installed:
  telnet.x86_64 1:0.17-64.el7                                                                                                                                               

Complete!
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# yum -y install telnet

2>.測試連接次數

[root@node108.yinzhengjie.org.cn ~]# telnet 172.30.1.101 80
Trying 172.30.1.101...
Connected to 172.30.1.101.
Escape character is '^]'.
GET / HTTP/1.1 HOST:node101.yinzhengjie.org.cn 
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 06:17:25 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 73
Last-Modified: Tue, 17 Dec 2019 04:54:00 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5df85f68-49"
Accept-Ranges: bytes

<h1 style='color:rgb(255,0,0)'>https://www.cnblogs.com/yinzhengjie/</h1> GET / HTTP/1.1 HOST:node101.yinzhengjie.org.cn

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 06:17:38 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 73
Last-Modified: Tue, 17 Dec 2019 04:54:00 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5df85f68-49"
Accept-Ranges: bytes

<h1 style='color:rgb(255,0,0)'>https://www.cnblogs.com/yinzhengjie/</h1> GET / HTTP/1.1 HOST:node101.yinzhengjie.org.cn

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 06:18:00 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 73
Last-Modified: Tue, 17 Dec 2019 04:54:00 GMT
Connection: close
ETag: "5df85f68-49"
Accept-Ranges: bytes

<h1 style='color:rgb(255,0,0)'>https://www.cnblogs.com/yinzhengjie/</h1>
Connection closed by foreign host.
[root@node108.yinzhengjie.org.cn ~]# 

3>.測試連接超時時間

[root@node108.yinzhengjie.org.cn ~]# telnet 172.30.1.101 80          #連接成功后啥也不要操作,等65秒過后會自動斷開。
Trying 172.30.1.101...
Connected to 172.30.1.101.
Escape character is '^]'.
Connection closed by foreign host.
[root@node108.yinzhengjie.org.cn ~]# 

4>.使用curl命令查看nginx的響應頭部信息

[root@node108.yinzhengjie.org.cn ~]# curl -I  http://node101.yinzhengjie.org.cn/
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 06:19:24 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 73
Last-Modified: Tue, 17 Dec 2019 04:54:00 GMT
Connection: keep-alive
Keep-Alive: timeout=60          #這個時間告訴客戶端是60,而咱們上面配置的是告訴客戶端是60秒斷開,其實是65秒才會真正的斷開喲~
ETag: "5df85f68-49"
Accept-Ranges: bytes

[root@node108.yinzhengjie.org.cn ~]# 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM