提高服务器并发量,有关系统配置的常规方法
一般情況下, 服務器的性能除了編程技巧之外,還有一些操作系統本身的限制。這里我們假設服務器CPU 內存都是能滿足需求的。來說說Linux 服務器的一些提高性能的方法。
對于服務器,每當有一個連接到來都要消耗一個文件描述符,即系統對文件描述符的限制就成了高性能的障礙。我們可以用ulimit可以查看當前系統對資源的一些限制。
這里可以看到有我們平時debug 程序的dump core 文件大小和文件描述符,消息隊列一個消息的長度的限制等等。這里我們所看到的文件描述符,可以通過ulimit 進行配置
root:/etc# ulimit -SHn 500000 root:/etc# 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) 15739 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 500000 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) 15739 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited經過測試,確實生效,但是重啟之后就恢復了,要想永久生效就要修改/etc/security/limits.conf文件
#<type> can have the two values: # - "soft" for enforcing the soft limits # - "hard" for enforcing hard limits # #<item> can be one of the following: # - core - limits the core file size (KB) # - data - max data size (KB) # - fsize - maximum filesize (KB) # - memlock - max locked-in-memory address space (KB) # - nofile - max number of open files # - rss - max resident set size (KB) # - stack - max stack size (KB) # - cpu - max CPU time (MIN) # - nproc - max number of processes # - as - address space limit (KB) # - maxlogins - max number of logins for this user # - maxsyslogins - max number of logins on the system # - priority - the priority to run user process with # - locks - max number of file locks the user can hold # - sigpending - max number of pending signals # - msgqueue - max memory used by POSIX message queues (bytes) # - nice - max nice priority allowed to raise to values: [-20, 19] # - rtprio - max realtime priority # - chroot - change root to directory (Debian-specific) # #<domain> <type> <item> <value> ##* soft core 0 #root hard core 100000 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #ftp - chroot /ftp #@student - maxlogins 4 root soft nofile 120000 root hard nofile 120000 * soft nofile 120000 * hard nofile 120000 # End of file在文件里面添加
1. soft nofile 1200002. hard nofile 120000這里的soft 個數一定要小于等于hard個數,重啟后生效。
另外,如果想看一個正在運行的進程的資源限制,可以到/proc/進程id/ 下的limits文件里面查看。
2. 端口號限制
對于服務器來講一般只需要開放一個端口號, 但是某些應用,需要多個端口號例如nginx 反向代理。這里如果正常情況下Nginx只能轉發30000多個連接,因為默認情況下系統開放的端口號是3萬多 - 65535之間。Nginx 反向代理轉發時需要隨機端口去轉發,默認只能是3萬多。
對于端口號可以通過sysctl -a 查看。
root:~# sysctl -a | grep local fs.nfs.nsm_local_state = 0 net.ipv4.conf.all.accept_local = 0 net.ipv4.conf.all.route_localnet = 0 net.ipv4.conf.default.accept_local = 0 net.ipv4.conf.default.route_localnet = 0 net.ipv4.conf.docker0.accept_local = 0 net.ipv4.conf.docker0.route_localnet = 0 net.ipv4.conf.eth0.accept_local = 0 net.ipv4.conf.eth0.route_localnet = 0 net.ipv4.conf.lo.accept_local = 0 net.ipv4.conf.lo.route_localnet = 0 net.ipv4.conf.vethc30f7f2.accept_local = 0 net.ipv4.conf.vethc30f7f2.route_localnet = 0 net.ipv4.ip_local_port_range = 32768 61000 net.ipv4.ip_local_reserved_ports = net.ipv4.ip_nonlocal_bind = 0這里還不到3萬 。對于這個限制可以修改/etc/sysctl.conf 來實現 添加 net.ipv4.ip_local_port_range = 1024 65535.
修改后運行sysctl -p 或重啟即可生效。 但是即使這樣也只能轉發6萬多個鏈接。 如果要增加轉發數量,這里可以在后端server 多加虛擬IP 添加網卡別名
ifconfig eth0:1 192.168.1.11 netmask 255.255.255.0 up
這樣后端server就添加了一個虛擬IP, 然后再修改Nginx配置文件,用負載均衡這種方式進行轉發。或者也可以把虛擬IP加在Nginx這邊,把IP映射到統一的域名即可。有關內核參數的詳細解釋請看:http://www.cnblogs.com/tolimit/p/5065761.html
?
轉載于:https://www.cnblogs.com/MaAce/p/7755710.html
總結
以上是生活随笔為你收集整理的提高服务器并发量,有关系统配置的常规方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript自定义浏览器滚动条兼
- 下一篇: 由作用域安全的构造函数想到的