2017-04-14 176 views
0

Nginx充當廣告服務器的反向代理,每分鐘接收20k個請求。在100ms內發生的響應從廣告服務器的nginx的Nginx和sysctl配置 - 性能設置

運行在虛擬機與配置 128GB RAM 4個vCPU 100GB HDD

上述考慮,什麼是Nginx的良好環境,也sysctl.conf的

回答

2

請記住,內核調整是複雜的,需要大量的評估,直到你得到正確的結果。如果有人發現一個錯誤,請讓我知道,以便我可以調整我自己的配置:-)

另外,如果此服務器只運行Nginx,您的內存對於請求數量是相當高的,您可以檢查您有多少在高峯時段使用並相應調整。

檢查一個重要的事情是文件描述符的數量,在你的情況下,我將它設置爲65.000,以應付每秒20.000+個請求。原因是在正常情況下,您只需要大約4.000個文件描述符,因爲您有4.000個同時打開的連接(20.000 * 2 * 0.1)。但是,如果發生後端問題,加載廣告可能需要1秒或更長時間。在這種情況下simultanious打開的連接的數量會更高:

20.000 * 2 * 1.5 = 60.000. 

所以將它設置爲65K將在我看來是一個保存值。

您可以通過檢查文件描述符的數量:

cat /proc/sys/fs/file-max 

如果這是65000下面你就需要設置到/etc/sysctl.conf:

fs.file-max = 65000 

也爲Nginx的,您需要添加以下文件中:/etc/systemd/system/nginx.service.d/override.conf

[Service] 
LimitNOFILE=65000 

在nginx.conf文件:

worker_rlimit_nofile 65000; 

添加的情況下,你將需要申請更改:

sudo systemctl -p 
sudo systemctl daemon-reload 
sudo systemctl restart nginx 

這些設置後,以下設置將讓你開始:

vm.swappiness = 0 # The kernel will swap only to avoid an out of memory condition 
vm.min_free_kbytes = 327680 # The kernel will start swapping when memory is below this limit (300MB) 

vm.vfs_cache_pressure = 125 # reclaim memory which is used for caching of VFS caches quickly 
vm.dirty_ratio = 15 # Write pages to disk when 15% of memory is dirty 
vm.dirty_background_ratio = 10 # System can start writing pages to disk when 15% of memory is dirty 

另外,我在我的sysctl配置中使用以下安全設置,並配合上面的可調參數。隨意使用它們,for credits

# Avoid a smurf attack 
net.ipv4.icmp_echo_ignore_broadcasts = 1 
# Turn on protection for bad icmp error messages 
net.ipv4.icmp_ignore_bogus_error_responses = 1 
# Turn on syncookies for SYN flood attack protection 
net.ipv4.tcp_syncookies = 1 
# Turn on and log spoofed, source routed, and redirect packets 
net.ipv4.conf.all.log_martians = 1 
net.ipv4.conf.default.log_martians = 1 
# No source routed packets here 
net.ipv4.conf.all.accept_source_route = 0 
net.ipv4.conf.default.accept_source_route = 0 
# Turn on reverse path filtering 
net.ipv4.conf.all.rp_filter = 1 
net.ipv4.conf.default.rp_filter = 1 
# Make sure no one can alter the routing tables 
net.ipv4.conf.all.accept_redirects = 0 
net.ipv4.conf.default.accept_redirects = 0 
net.ipv4.conf.all.secure_redirects = 0 
net.ipv4.conf.default.secure_redirects = 0 
# Don't act as a router 
net.ipv4.ip_forward = 0 
net.ipv4.conf.all.send_redirects = 0 
net.ipv4.conf.default.send_redirects = 0 
# Turn on execshild 
kernel.exec-shield = 1 
kernel.randomize_va_space = 1 

當你進行代理請求,我將以下內容添加到您的sysctl.conf的文件,以確保您沒有運行出端口,它是可選的,但如果你正在運行到問題是東西要記住:

net.ipv4.ip_local_port_range=1024 65000 

正如我一般評價的默認設置,並相應地調整我沒有提供IPv4和IPv4的。tcp_選項。你可以在下面找到一個例子,但請不要複製和粘貼,你需要做一些閱讀,然後再開始調整這些變量。

# Increase TCP max buffer size setable using setsockopt() 
net.ipv4.tcp_rmem = 4096 87380 8388608 
net.ipv4.tcp_wmem = 4096 87380 8388608 
# Increase Linux auto tuning TCP buffer limits 
# min, default, and max number of bytes to use 
# set max to at least 4MB, or higher if you use very high BDP paths 
# Tcp Windows etc 
net.core.rmem_max = 8388608 
net.core.wmem_max = 8388608 
net.core.netdev_max_backlog = 5000 
net.ipv4.tcp_window_scaling = 1 

以上參數是不是你應該考慮,有很多,你可以調整,爲example多個參數的一切:

  • 組工作進程的數量爲4(每個CPU核心一個) 。
  • 調整積壓隊列。
  • 如果您不需要acccess日誌,我只需將其關閉以刪除磁盤I/O。
  • 可選:如果CPU使用率升高,請降低或禁用gzip壓縮。