Nginx的一些騷操作
- 前言
- worker_processes
- worker_cpu_affinity
- multi_accept
- worker_rlimit_nofile
- worker_connections
- backlog
- 總結
前言
相關引數
- worker_processes
- worker_cpu_affinity
- multi_accept
- worker_rlimit_nofile
- worker_connections
- backlog
worker_processes
讓nginx開啟多核CPU的配置,可以將行程系結到一組CPU上,依據服務器CPU核數設定,(不是CPU數量哦)
user nginx;
worker_processes 8; # 服務器8核 可以設定8核,依據核心數量設定,注意不是CPU數量
worker_cpu_affinity
可以配合worker_processes 使用,將行程系結到指定的CPU核心,CPU核心的指定可以通過CPU的位掩碼來表示,
如果是四核的服務器,那么第一個CPU核心就是 0001 第二個CPU核心為 0010,依次類推
服務器會出現個別CPU很閑,壓力很小,這個時候我們就可以將nginx系結到閑置的CPU身上
通過使用Top命令,按1鍵查看每個CPU內核的使用情況
示例:
top - 11:17:46 up 20 days, 20:54, 5 users, load average: 0.28, 0.22, 0.32
Tasks: 354 total, 2 running, 352 sleeping, 0 stopped, 0 zombie
%Cpu0 : 5.0 us, 1.3 sy, 0.0 ni, 93.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 3.7 us, 0.7 sy, 0.0 ni, 95.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 2.7 us, 1.7 sy, 0.0 ni, 95.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu4 : 1.0 us, 1.0 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 : 1.0 us, 1.0 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu6 : 1.0 us, 1.3 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 : 1.7 us, 1.7 sy, 0.0 ni, 96.3 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
%Cpu8 : 1.3 us, 0.7 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu9 : 4.0 us, 1.0 sy, 0.0 ni, 95.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu10 : 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu11 : 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu12 : 0.7 us, 0.7 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.3 st
%Cpu13 : 0.7 us, 0.7 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.3 st
%Cpu14 : 0.7 us, 0.3 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu15 : 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16264568 total, 800660 free, 8007112 used, 7456796 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 7169460 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15159 root 20 0 2285980 111220 29940 S 3.7 0.7 1310:31 kubelet
21002 polkitd 20 0 9625676 87996 5200 S 2.7 0.5 73:08.99 beam.smp
4357 root 20 0 2034248 105228 17996 S 2.3 0.6 1966:23 dockerd
25879 root 20 0 2740444 43716 17000 S 1.3 0.3 680:10.67 calico-node
我們可以這樣設定
user nginx;
worker_processes 2; # 使用兩核CPU
worker_cpu_affinity 0000000000000100 0000001000000000; #系結到第三個和第十個CPU核心
也可以這樣設定
worker_processes auto; # 允許將作業行程自動系結到可用CPU核心
worker_cpu_affinity auto;
還能這樣玩
worker_processes auto; # 允許將作業行程自動系結到可用CPU核心
worker_cpu_affinity auto 001 100; # 限制可以自動系結的CPU核心
multi_accept
默認關閉, 關閉狀態下一個作業行程只能接收一個新連接,該引數配置在nginx的events模塊中,
此外還有個引數需要注意 accept_mutex 默認為off,該引數為off狀態時,在某一時刻,如果只有一個請求,也會導致多個睡眠的行程被喚醒,
一般這樣設定
events {
accept_mutex on; # 當只有單個請求時,不會導致多個行程被喚醒
multi_accept on; # 允許接受多個新網路連接請求
use epoll;
}
如果nginx使用kqueue連接方法,那么這條指令會被忽略,因為這個方法會報告在等待被接受的新連接的數量
worker_rlimit_nofile
如果不修改改配置和系統默認limit引數 則可能會遇到open too many files錯誤
查看系統默認最大檔案描述符
ulimit -n
[root@192 ~]# ulimit -n
1024
默認為1024
引數和worker_processes與worker_connections 也有一點關系
系統的最大檔案描述符>= worker_connections*worker_process
嘗試修改系統最大檔案描述符
第一個方式
編輯檔案 vi /etc/security/limits.conf
加入以下內容
* soft nofile 65536
* hard nofile 65536
修改組態檔,需要重啟才能生效
這樣我們可以在環境變數中在配置一次,讓他立即生效
編輯檔案 vi /etc/profile
新增 ulimit -n 65535
示例
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
ulimit -n 65535
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
新增完成后重繪環境變數
. /etc/profile 或者 source /etc/profile
再次查看
[root@192 ~]# ulimit -n
65535
[root@192 ~]#
nginx配置
worker_rlimit_nofile 35535; #根據環境適當修改
worker_connections
單個行程允許的最大客戶端連接數,位于events模塊中
nginx能處理的最大并發數量
該值不能超過 worker_rlimit_nofile 否者會出現worker_connections exceed open file resource limit: xxx
網上相關資訊一般是以 worker_rlimit_nofile >= worker_connections*worker_process 來配置該引數,具體可依斬訓境資訊調控
示例
events {
worker_connections 2048;
accept_mutex on;
multi_accept on;
}
backlog
該引數配置在listen 后面,為底層listen函式的一個引數,默認值為511
詳細內容參照官方檔案,也可以看看這篇文章http://www.04007.cn/article/323.html
我們了解兩個linux內核引數
net.core.somaxconn
net.ipv4.tcp_max_syn_backlog
這兩個引數默認值為128
tcp_max_syn_backlog是指定所能接受SYN同步包的最大客戶端數量,半連接上限,為SYN_REVD狀態的連接數,
somaxconn是Linux中的一個kernel引數,指的是服務端所能accept即處理資料的最大客戶端數量,即完成連接上限,
好像 tcp_max_syn_backlog>=somaxconn
tcp_max_syn_backlog 就是我能接收多少請求
somaxconn 我能完成多少請求
理論上貌似是這樣的,但是這兩個屬性的配置沒有直接關系,
請以你的系統環境情況來分配 somaxconn 和 tcp_max_syn_backlog,
不要被忽悠了,詳細內容見官方檔案,
net.core.somaxconn 影響到了nginx快取佇列的最大的連接數量,
net.core.netdev_max_backlog 被切換到CPU處理前被網卡快取的速率包,根據網卡檔案加大值可以提高性能
對于高負載的服務器來說,128肯定是不夠用的,
具體設定為多少可以根據自己系統業務情況來定奪,
修改默認值
編輯檔案
vi /etc/sysctl.conf
加入以下內容
示例
[root@192 ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.tcp_max_syn_backlog=8096
net.core.somaxconn=32768
讓配置生效
[root@192 ~]# sysctl -p
net.ipv4.tcp_max_syn_backlog = 8096
net.core.somaxconn = 32768
[root@192 ~]#
nginx 默認的backlog引數為511,他影響nginx握手成功的佇列大小,和系統的somaxconn 對應上了,
修改nginx默認backlog大小
示例
server {
listen 8080 default backlog=1024; #多域名情況 單個埠 只能配置一個
client_max_body_size 2048M;
proxy_cache_lock on;
····
}
你想抗住一定量的流量,首先系統要能支持,在想著nginx優化,
總結
給甲方做一個H5系統的時候,前端是vue直接扔Nginx里面的,那邊直接說Nginx能抗住五萬,你們需要能抗2-3w,我抗尼瑪,就給了四臺服務器,雖然配置也還不錯,
Nginx默認的引數設定是扛不住太高的流量的,除此之外還要先從系統層面入手優化,首先確定你的服務器能頂住預估的流量,在來優化你的應用,中間件,另外脫離業務場景談優化啥的,我也感覺扯淡,
你沒有遇到那些問題,就想著提前給優化掉,那你很牛逼啊,我反正就是劃水,劃水,劃水,
另外附上一些系統引數調配 僅供參考
修改/etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv4.neigh.default.gc_interval=60
net.ipv4.neigh.default.gc_stale_time=120
kernel.perf_event_paranoid=-1
#sysctls for k8s node config
net.ipv4.tcp_slow_start_after_idle=0
net.core.rmem_max=16777216
fs.inotify.max_user_watches=524288
kernel.softlockup_all_cpu_backtrace=1
kernel.softlockup_panic=0
kernel.watchdog_thresh=30
fs.file-max=2097152
fs.inotify.max_user_instances=8192
fs.inotify.max_queued_events=16384
vm.max_map_count=262144
fs.may_detach_mounts=1
net.core.netdev_max_backlog=16384
net.ipv4.tcp_wmem=4096 12582912 16777216
net.core.wmem_max=16777216
net.core.somaxconn=32768
net.ipv4.ip_forward=1
net.ipv4.tcp_max_syn_backlog=8096
net.ipv4.tcp_rmem=4096 12582912 16777216
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
kernel.yama.ptrace_scope=0
vm.swappiness=0
# 可以控制core檔案的檔案名中是否添加pid作為擴展,
kernel.core_uses_pid=1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.all.accept_source_route=0
# Promote secondary addresses when the primary address is removed
net.ipv4.conf.default.promote_secondaries=1
net.ipv4.conf.all.promote_secondaries=1
# Enable hard and soft link protection
fs.protected_hardlinks=1
fs.protected_symlinks=1
# 源路由驗證
# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets=5000
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_synack_retries=2
kernel.sysrq=1
我也是白嫖過來的,白嫖香~~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/169874.html
標籤:其他
上一篇:2 linux筆記 檔案目錄管理
