search
HomeBackend DevelopmentPHP TutorialIntroduction to places where nginx can be optimized

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;

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;

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;

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;

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

 worker_connections 102400;

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;

keepaliveTimeout.

client_header_buffer_size 4k;

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;

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;

This refers to how often to check cached valid information.

 open_file_cache_min_uses 1;

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
 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

 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

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
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),