计算机知识分享网

【nginx】Linux中Nginx突破连接数1024的限制

首先修改Linux操作系统,突破1024限制
vim /etc/security/limits.conf
在最后面增加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
查看当前资源大小
[root@localhost nginx]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 30633
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
配置 Nginx的服务信息
[root@localhost nginx]# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

##### 需要增加最后这一行,突破限制
LimitNOFILE=65535

#---------------------------------------------------------------
##### Ubuntu
sudo vim /lib/systemd/system/nginx.service # (仅适用于 ubuntu)
添加如下:
[Service]
LimitNOFILE=65535
配置nginx.conf配置信息
# 主要增加这个突破限制
worker_rlimit_nofile 65535;
events {
    worker_connections  10000;
}
重启服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl stop nginx.service
[root@localhost ~]# systemctl start nginx.service
根据Nginx的pid查看的worker信息
[root@localhost nginx]# grep -i 'Max open files' /proc/$(cat /var/run/nginx.pid)/limits
Max open files            1024                 4096                 files     
[root@localhost nginx]# ps --ppid $(cat /var/run/nginx.pid) -o %p|sed '1d'|xargs -I{} cat /proc/{}/limits|grep open.files
Max open files            65535                65535                files     
Max open files            65535                65535                files     

#Nginx