Home Operation and Maintenance Nginx How to optimize the Linux kernel of high-concurrency nginx server

How to optimize the Linux kernel of high-concurrency nginx server

May 15, 2023 pm 12:10 PM
linux nginx

The code is as follows:

# controls the use of tcp syncookies

#表示开启重用。允许将time-wait sockets重新用于新的tcp连接,默认为0,表示关闭;
net.ipv4.tcp_syncookies = 1

#一个布尔类型的标志,控制着当有很多的连接请求时内核的行为。启用的话,如果服务超载,内核将主动地发送rst包。
net.ipv4.tcp_abort_on_overflow = 1

#表示系统同时保持time_wait的最大数量,如果超过这个数字,time_wait将立刻被清除并打印警告信息。
#默认为180000,改为6000。对于apache、nginx等服务器,此项参数可以控制time_wait的最大数量,服务器被大量的time_wait拖死
net.ipv4.tcp_max_tw_buckets = 6000

#有选择的应答
net.ipv4.tcp_sack = 1

#该文件表示设置tcp/ip会话的滑动窗口大小是否可变。参数值为布尔值,为1时表示可变,为0时表示不可变。tcp/ip通常使用的窗口最大可达到65535 字节,对于高速网络.
#该值可能太小,这时候如果启用了该功能,可以使tcp/ip滑动窗口大小增大数个数量级,从而提高数据传输的能力。
net.ipv4.tcp_window_scaling = 1

#tcp接收缓冲区
net.ipv4.tcp_rmem = 4096    87380  4194304

#tcp发送缓冲区
net.ipv4.tcp_wmem = 4096    66384  4194304

# # out of socket memory
net.ipv4.tcp_mem = 94500000 915000000 927000000

#该文件表示每个套接字所允许的最大缓冲区的大小。
net.core.optmem_max = 81920

#该文件指定了发送套接字缓冲区大小的缺省值(以字节为单位)。
net.core.wmem_default = 8388608

#指定了发送套接字缓冲区大小的最大值(以字节为单位)。
net.core.wmem_max = 16777216

#指定了接收套接字缓冲区大小的缺省值(以字节为单位)。
net.core.rmem_default = 8388608

#指定了接收套接字缓冲区大小的最大值(以字节为单位)。
net.core.rmem_max = 16777216

#表示syn队列的长度,默认为1024,加大队列长度为10200000,可以容纳更多等待连接的网络连接数。

net.ipv4.tcp_max_syn_backlog = 1020000

#每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
net.core.netdev_max_backlog = 862144

#web 应用中listen 函数的backlog 默认会给我们内核参数的net.core.somaxconn 限制到128,而nginx 定义的ngx_listen_backlog 默认为511,所以有必要调整这个值。
net.core.somaxconn = 262144

#系统中最多有多少个tcp 套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。


#这个限制仅仅是为了防止简单的dos 攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个
net.ipv4.tcp_max_orphans = 327680

#时间戳可以避免序列号的卷绕。一个1gbps 的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包。这里需要将其关掉。
net.ipv4.tcp_timestamps = 0

#为了打开对端的连接,内核需要发送一个syn 并附带一个回应前面一个syn 的ack。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送syn+ack 包的数量。
net.ipv4.tcp_synack_retries = 1

#在内核放弃建立连接之前发送syn 包的数量。www.jb51.net
net.ipv4.tcp_syn_retries = 1

#表示开启tcp连接中time-wait sockets的快速回收,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1

#表示开启重用。允许将time-wait sockets重新用于新的tcp连接,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1

#修改系統默认的 timeout 时间。
net.ipv4.tcp_fin_timeout = 15

#表示当keepalive起用的时候,tcp发送keepalive消息的频度。缺省是2小时,建议改为20分钟。
net.ipv4.tcp_keepalive_time = 30

#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为10000到65000。(注意:这里不要将最低值设的太低,否则可能会占用掉正常的端口!)
net.ipv4.ip_local_port_range = 1024  65000

#以下可能需要加载ip_conntrack模块 modprobe ip_conntrack ,有文档说防火墙开启情况下此模块失效

#縮短established的超時時間
net.netfilter.nf_conntrack_tcp_timeout_established = 180

#conntrack_max 允许的最大跟踪连接条目,是在内核内存中netfilter可以同时处理的“任务”(连接跟踪条目)
net.netfilter.nf_conntrack_max = 1048576
net.nf_conntrack_max = 1048576
Copy after login

The above is the detailed content of How to optimize the Linux kernel of high-concurrency nginx server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? Apr 01, 2025 pm 03:15 PM

Running multiple PHP versions simultaneously in the same system is a common requirement, especially when different projects depend on different versions of PHP. How to be on the same...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? Apr 01, 2025 pm 10:54 PM

Solution to Redirecting 404 Errors after Simulation Login When using Selenium for Simulation Login, we often encounter some difficult problems. �...

See all articles