Nginx battle preparation-Optimization Guide

Source: Internet
Author: User
Most Nginx installation guides tell you the following basic knowledge to install through apt-get and modify the configurations here or there. well, you already have a Web server! In addition, in most cases, a regular nginx installation can work well for your website.

Most Nginx installation guides tell you the following basic knowledge-install through apt-get and modify the configurations here or there. well, you already have a Web server! Moreover, in most cases, a regular nginx installation can work well for your website. However, if you really want to squeeze out nginx performance, you must go deeper. In this guide, I will explain the Nginx settings that can be fine-tuned to optimize the performance when processing a large number of clients. Note that this is not a comprehensive fine-tuning guide. This is a simple preview-an overview of performance settings that can be tuned to improve performance. Your situation may be different.

Basic (optimized) configuration

The only file we will modify isNginx. confContains all the settings for different Nginx modules. You should be able/Etc/nginxFind nginx. conf in the directory. First, we will talk about some global settings. then, we will talk about the settings that allow you to have good performance when accessing a large number of clients, and why they will improve the performance. There is a complete configuration file at the end of this article.

High-level configuration

In the nginx. conf file, Nginx has a few advanced configurations on the module section.

 
 
  1. user www-data; 
  2. pid /var/run/nginx.pid; 
  3.  
  4. worker_processes auto; 
  5.  
  6. worker_rlimit_nofile 100000; 

UserAndPidYou should follow the default settings-we will not change the content, because the change is no different.

Worker_processesDefines the number of worder processes when nginx provides external web services. The optimal value depends on many factors, including (but not limited to) the number of CPU cores, the number of hard disks for storing data, and the load mode. If you are unsure, setting it to the number of available CPU cores is a good start (setting it to "auto" will try to detect it automatically ).

Worker_rlimit_nofileChange the maximum number of files opened by a worker process. If this parameter is not set, the value is an operating system restriction. After setting, your operating system and Nginx can process more files than "ulimit-a", so set this value to high, in this way, nginx will not have the "too open files" problem.

Events Module

The events module contains all the settings for handling connections in nginx.

 
 
  1. events { 
  2.     worker_connections 2048; 
  3.     multi_accept on; 
  4.     use epoll; 

Worker_connectionsSets the maximum number of connections that can be opened by a worker process at the same time. If the worker_rlimit_nofile mentioned above is set, we can set this value to a high value.

Remember, the maximum number of customers is also limited by the number of available socket connections (~ 64 K), so it is no good to set an unrealistic height.

Multi_acceptTell nginx to accept as many connections as possible after receiving a new connection notification.

UseSets the polling method used to reuse client threads. If you use Linux 2.6 +, you should use epoll. If you use * BSD, you should use kqueue. Want to know more about event polling? Take a look at Wikipedia (note that if you want to know everything, you may need the neckbeard and operating system course basics)

(It is worth noting that if you do not know which polling method should be used for Nginx, it will select the one that best suits your operating system)

HTTP Module

The HTTP module controls all core features of nginx http processing. Because there are only a few configurations, we only extract a small part of the configuration. All these settings should be in the http module, and you will not even pay special attention to this setting.

 
 
  1. http { 
  2.  
  3.     server_tokens off; 
  4.  
  5.     sendfile on; 
  6.  
  7.     tcp_nopush on; 
  8.     tcp_nodelay on; 
  9.  
  10.     ... 

Server_tokensIt does not speed up nginx execution, but it can disable the nginx version number on the error page, which is good for security.

SendfileSendfile () can be used. Sendfile () can copy data (or any two file descriptors) between the disk and the TCP socket ). Pre-sendfile is used to request a data buffer in the user space before data transmission. Then, use read () to copy data from the file to the buffer zone, and write () to write the buffer zone data to the network. Sendfile () immediately reads data from the disk to the OS cache. Because this copy is completed in the kernel, sendfile () is more effective than combining read () and write () and turning on and off the discard buffer (more about sendfile)

Tcp_nopushTells nginx to send all header files in one packet, instead of sending them one by one.

Tcp_nodelayTell nginx not to cache data, but to send data for a period of time-when you need to send data in time, you should set this attribute for the application, so that when sending a small piece of data information, you cannot get the return value immediately.

 
 
  1. access_log off; 
  2. error_log /var/log/nginx/error.log crit; 

Access_logSet whether nginx will store access logs. Disabling this option can make disk read IO operations faster (aka, YOLO)

Error_logTell nginx that only serious errors can be recorded

 
 
  1. keepalive_timeout 10; 
  2.  
  3. client_header_timeout 10; 
  4. client_body_timeout 10; 
  5.  
  6. reset_timedout_connection on; 
  7. send_timeout 10; 

Keepalive_timeoutAllocate the keep-alive link timeout time to the client. The server will close the link after the timeout. We set it to a lower level so that ngnix can continue to work for a longer time.

Client_header_timeoutAndClient_body_timeoutSet the timeout time for the request header and request body. We can also lower this setting.

Reset_timeout_connection tells nginx to close the client connection that does not respond. This will release the memory space occupied by the client.

Send_timeout specifies the response timeout of the client. This setting is not used for the entire forwarder, but between two client read operations. If the client does not read any data during this period, nginx closes the connection.

 
 
  1. limit_conn_zone $binary_remote_addr zone=addr:5m; 
  2. limit_conn addr 100; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.