Nginx 和 sysctl 配置 - 性能设置
Nginx and sysctl configuration - Performance setting
Nginx 充当广告服务器的反向代理,每分钟接收 20k 个请求。响应发生在从广告服务器到 nginx 的 100 毫秒内
运行 在配置为的虚拟机上
128GB 内存
4个虚拟CPU
100GB 硬盘
综上所述,Nginx设置什么好sysctl.conf
请记住,内核调优很复杂,需要进行大量评估才能获得正确的结果。如果有人发现错误,请告诉我,以便我调整自己的配置:-)
此外,如果此服务器只有运行 Nginx,您的内存对于请求量来说是相当高的,您可以检查一下您在高峰时段使用了多少并相应地进行调整。
要检查的一个重要事项是文件描述符的数量,在您的情况下,我会将其设置为 65.000 以应对每秒 20.000 多个请求。原因是在正常情况下,您只需要大约 4.000 个文件描述符,因为您有 4.000 个同时打开的连接 (20.000 * 2 * 0.1)。但是,如果后端出现问题,加载广告可能需要 1 秒或更长时间。在那种情况下,同时打开的连接数会更高:
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 sysctl -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 核心一个)。
- 调整积压队列。
- 如果您不需要访问日志,我会简单地关闭它以删除磁盘 I/O。
- 可选:如果您的 CPU 使用率越来越高,请降低或禁用 gzip 压缩。
Nginx 充当广告服务器的反向代理,每分钟接收 20k 个请求。响应发生在从广告服务器到 nginx 的 100 毫秒内
运行 在配置为的虚拟机上 128GB 内存 4个虚拟CPU 100GB 硬盘
综上所述,Nginx设置什么好sysctl.conf
请记住,内核调优很复杂,需要进行大量评估才能获得正确的结果。如果有人发现错误,请告诉我,以便我调整自己的配置:-)
此外,如果此服务器只有运行 Nginx,您的内存对于请求量来说是相当高的,您可以检查一下您在高峰时段使用了多少并相应地进行调整。
要检查的一个重要事项是文件描述符的数量,在您的情况下,我会将其设置为 65.000 以应对每秒 20.000 多个请求。原因是在正常情况下,您只需要大约 4.000 个文件描述符,因为您有 4.000 个同时打开的连接 (20.000 * 2 * 0.1)。但是,如果后端出现问题,加载广告可能需要 1 秒或更长时间。在那种情况下,同时打开的连接数会更高:
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 sysctl -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 核心一个)。
- 调整积压队列。
- 如果您不需要访问日志,我会简单地关闭它以删除磁盘 I/O。
- 可选:如果您的 CPU 使用率越来越高,请降低或禁用 gzip 压缩。