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 ~]#
Nginx 核心配置-长连接配置的更多相关文章
- centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课
centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB ...
- Nginx与Tomcat、Client之间请求的长连接配置不一致问题解决[转]
http://bert82503.iteye.com/blog/2152613 前些天,线上出现“服务端长连接与客户端短连接引起Nginx的Writing.Active连接数过高问题”,这个是由于“服 ...
- nginx配置长连接
http { keepalive_timeout 20; --长连接timeout keepalive_requests 8192; --每个连接最大请求数 } events { worker_con ...
- Nginx反向代理与Backend直接配置长连接
使用了Nginx的反向代理配置如下: upstream test{ keepalive 1; server 192.168.1.63:4000; } server { listen 4000; ser ...
- nginx 代理tcp长连接短连接配置
https://blog.csdn.net/tayinyinyueyue/article/details/78932697 nginx使用ngx_stream_core_module模块代理tcp长连 ...
- nginx配置长连接(ajax60秒请求超时)
个人博客 地址:http://www.wenhaofan.com/article/20180911150337 1.在使用ajax做轮训的时候前台发出的ajax请求总是会在60秒之后返回405超时响应 ...
- Nginx中的长连接
在nginx中,对于http1.0与http1.1是支持长连接的 我们知道,http请求是基于TCP协议之上的,那么,当客户端在发起请求前,需要先与服务端建立TCP连接,而每一次的TCP连接是需要三次 ...
- 我为什么要谈KeepAlive(文末增加nginx 负载tcp长连接保持 demo)
http://blog.sina.com.cn/s/blog_e59371cc0102ux5w.html 最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Client访 ...
- Nginx 核心配置详解
目录 Nginx 核心配置详解 Nginx 四层访问控制: Nginx账户认证功能: 自定义错误页面: 自定义访问日志: 检测文件是否存在: 长连接配置: 作为下载服务器配置: 作为上传服务器: 其他 ...
随机推荐
- JavaScript中的对象与原型—你不知道的JavaScript上卷读书笔记(四)
一.对象 对象可以通过两种形式定义:声明(文字)形式和构造形式.即: var myObj = { key: value // ... }; 或: var myObj = new Object(); m ...
- 项目:git+gitlab+jenkins+ansible上线网站
项目需求 1. 在gitlab中创建一个项目 nginxinstall2. 编写playbook,实现一键部署nginx.部署一个静态测试页.测试部署结果要求: 部署nginx 端口:83 运行身份: ...
- [BJOI2019]勘破神机(第一类斯特林数,斐波那契数列)
真的是好题,只不过强行多合一有点过分了…… 题目大意: $T$ 组数据.每个测试点中 $m$ 相同. 对于每组数据,给定 $l,r,k$,请求出 $\dfrac{1}{r-l+1}\sum\limit ...
- Linux性能优化实战学习笔记:第五十讲
一.上节回顾 上一节,我以 ksoftirqd CPU 使用率高的问题为例,带你一起学习了内核线程 CPU 使用率高时的分析方法.先简单回顾一下. 当碰到内核线程的资源使用异常时,很多常用的进程级性能 ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- Windows的一些使用技巧/设置
仅为个人记录,关闭与否还请读者斟酌 1,加速关机速度 运行gpedit.msc: 计算机管理,管理模块 - 系统 -关机选项 关闭会阻止或取消关机的应用程序的自动终止功能. 2,组策略关闭小娜后,只把 ...
- java中super()和this()、super和this的区别
1.super()和this()区别: super():调用父类无形参的构造方法: super(形参):调用父类中某个带形参的构造方法: this(形参):调用本类中另一种形式的构造方法: 注意:放在 ...
- 【网络知识之五】TCP
TCP协议:传输控制协议. 一.TCP保证可靠性的机制1.校验和 TCP报头有16位检验和: 由发送端填充, 检验形式有CRC校验等. 如果接收端校验不通过, 则认为数据有问题. 此处的校验和不光包含 ...
- PHP 函数禁用设置方法
PHP 函数禁用设置方法先找到php.ini 然后搜索disable_function 直接在上面添加你要禁用的函数就可以了 然后记得重启php-fpm(如果装了php-fpm)
- plsql developer启动变慢的原因
导致原因 在plsql developer工具里面有打印的选项,进入打印设置后会调用打印机设置,显示所有已创建的打印机连接.如果当前电脑默认打印机是网络打印机,并且此网络打印机处于不可用状态时,那么p ...