Home Backend Development PHP Tutorial Introduction to places where nginx can be optimized

Introduction to places where nginx can be optimized

Oct 25, 2018 pm 05:06 PM
nginx

This article brings you an introduction to the places where nginx can be optimized. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

worker_processes 8;
Copy after login

nginxThe number of processes is recommended to be specified according to the number of cpu, which is usually a multiple of it.

 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
Copy after login

Assign cpu to each process. In the above example, 8 The process is assigned to 8cpu. Of course, you can write multiple, or assign one process to multiple cpu.

 worker_rlimit_nofile 102400;
Copy after login

This command refers to the maximum number of file descriptors opened by a nginx process. The theoretical value should be the maximum number of open files ( ulimit n) is divided by the number of nginx processes, but nginx allocates requests It's not that uniform, so it's better to keep it consistent with the value of ulimit n.

 use epoll;
Copy after login

Using the I/O model of epoll, needless to say this .

 worker_connections 102400;
Copy after login

The maximum number of connections allowed by each process. Theoretically, the maximum number of connections for each nginx server isworker_processes*worker_connections.

 keepalive_timeout 60;
Copy after login

keepaliveTimeout.

client_header_buffer_size 4k;
Copy after login

The buffer size of the client request header. This can be set according to the paging size of your system. Generally, the header size of a request will not exceed 1k , However, since the general system paging is larger than 1k , so the paging size is set here. The paging size can be obtained with the command getconf PAGESIZE.

 open_file_cache max=102400 inactive=20s;
Copy after login

This will specify the cache for open files. It is not enabled by default. maxSpecifies the number of caches. It is recommended to be the same as the number of open files. Sincerely, inactive refers to how long the file has not been requested before the cache is deleted.

 open_file_cache_valid 30s;
Copy after login

This refers to how often to check cached valid information.

 open_file_cache_min_uses 1;
Copy after login

open_file_cache#inactiveThe minimum number of times the file is used within the parameter time in the directive, if this number is exceeded, The file descriptor is always

open in the cache. If a file is not used once in inactive time, it will was removed.

Optimization of kernel parameters

net.ipv4.tcp_max_tw_buckets = 6000

number of timewait , the default is 180000.

net.ipv4.ip_local_port_range = 1024 65000

The port range allowed to be opened by the system.

net.ipv4.tcp_tw_recycle = 1

EnabletimewaitFast recycling.

net.ipv4.tcp_tw_reuse = 1

Enable reuse. Allow TIMEWAIT sockets to be reused for new TCP connections.

net.ipv4.tcp_syncookies = 1

EnableSYN Cookies, when # appears ##SYNEnable cookies to handle when waiting for the queue to overflow.

net.core.somaxconn = 262144

webIn applicationlisten The function’s backlogdefaults to the net.core.somaxconn that will limit our kernel parameters to 128, and nginxdefinitionNGX_LISTEN_BACKLOGdefaults to 511, so it is necessary to adjust this value. net.core.netdev_max_backlog = 262144

When each network interface receives packets faster than the kernel can process those packets, The maximum number of packets allowed to be sent to the queue.

net.ipv4.tcp_max_orphans = 262144

The maximum number of

TCP The socket is not associated with any user file handle. If this number is exceeded, the orphan connection will be reset immediately and a warning message will be printed. This limit is only to prevent simple DoS attacks. You cannot rely too much on it or artificially reduce this value. You should increase this value( If you increase the memory). net.ipv4.tcp_max_syn_backlog = 262144

The maximum value of recorded connection requests that have not yet received client confirmation information. For systems with

128M memory, the default value is 1024, and for systems with small memory it is 128. net.ipv4.tcp_timestamps = 0

Time stamps can avoid sequence number wrapping. A

1Gbps link will definitely encounter a previously used sequence number. The timestamp allows the kernel to accept such "Exception" data packets. It needs to be turned off here. net.ipv4.tcp_synack_retries = 1

In order to open the connection to the peer, the kernel needs to send a

SYNAnd comes with a ACK that responds to the previous SYN. This is the second handshake in the so-called three-way handshake. This setting determines the number of SYN ACK packets sent before the kernel abandons the connection. net.ipv4.tcp_syn_retries = 1

在内核放弃建立连接之前发送SYN包的数量。

net.ipv4.tcp_fin_timeout = 1

如果套接字由本端要求关闭,这个参数决定了它保持在FIN­WAIT­2状态的时间。对端可以出错并永远不关 闭连接,甚至意外当机。缺省值是60秒。2.2 内核的通常值是180秒,你可以按这个设置,但要记住的是, 即使你的机器是一个轻载的WEB服务器,也有因为大量的死套接字而内存溢出的风险,FIN­ WAIT­2的危 险性比FIN­WAIT­1要小,因为它最多只能吃掉1.5K内存,但是它们的生存期长些。

net.ipv4.tcp_keepalive_time = 30

keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。

一个完整的内核优化配置

 net.ipv4.ip_forward = 0
 net.ipv4.conf.default.rp_filter = 1
 net.ipv4.conf.default.accept_source_route = 0
Copy after login
 kernel.sysrq = 0
 kernel.core_uses_pid = 1
 net.ipv4.tcp_syncookies = 1
 kernel.msgmnb = 65536
 kernel.msgmax = 65536
 kernel.shmmax = 68719476736
 kernel.shmall = 4294967296
 net.ipv4.tcp_max_tw_buckets = 6000
 net.ipv4.tcp_sack = 1
 net.ipv4.tcp_window_scaling = 1
 net.ipv4.tcp_rmem = 4096    87380    4194304
 net.ipv4.tcp_wmem = 4096    16384    4194304
 net.core.wmem_default = 8388608
 net.core.rmem_default = 8388608
 net.core.rmem_max = 16777216
 net.core.wmem_max = 16777216
Copy after login

 net.core.netdev_max_backlog = 262144
 net.core.somaxconn = 262144
 net.ipv4.tcp_max_orphans = 3276800
 net.ipv4.tcp_max_syn_backlog = 262144
 net.ipv4.tcp_timestamps = 0
 net.ipv4.tcp_synack_retries = 1
 net.ipv4.tcp_syn_retries = 1
Copy after login

The above is the detailed content of Introduction to places where nginx can be optimized. 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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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 start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

See all articles