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 ~]#

最新文章

  1. web前端开发学习:jQuery的原型中的init
  2. noi 9271 奶牛散步
  3. [HIHO1062] 最近公共祖先&#183;一(lca, 并查集, 二分, 神trick)
  4. 高亮代码显示之HTML困惑
  5. Java判断文件编码格式
  6. easyui-textbox
  7. 【NOI2004】郁闷的出纳员
  8. Handler导致内存泄露分析
  9. 【Remoting-5代码实现】
  10. Angular-UI-Router 学习笔记
  11. Kafka (一)
  12. 驱动7段LED显示器
  13. 手机版WEB开发经验分享,手机版网站开发注意事项,网站自适应,手机版网站自适应,移动安卓APP自适应
  14. 201521123104《Java程序设计》第11周学习总结
  15. 使用C++实现图形的旋转、缩放、平移
  16. 关于Selenium WebDriver的geckodriver
  17. 总结:Java 集合进阶精讲1
  18. struts2返回json字符串
  19. BZOJ 2594 【WC2006】 水管局长数据加强版
  20. oracle分析函数 (转)

热门文章

  1. linux-部署1
  2. Centos 7使用docker部署LAMP搭建wordpress博客系统
  3. du和ls的区别:如何正确计算文件大小
  4. B1047 编程团体赛 (20 分)
  5. 在Azure DevOps Server (TFS)中实现VUE项目的自动打包
  6. IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi......
  7. Leetcode 344:Reverse String 反转字符串(python、java)
  8. springboot mybatis常见异常及处理方法
  9. PHP stdclass转array的方法
  10. windows环境中hbase源码编译遇到的问题