Several parameters to be optimized for nginx Configuration

Source: Internet
Author: User

Worker_processes 8

Generally, CPU (I/O)-intensive configurations have the same number of cores, and network (I/O)-intensive configurations have the same number of cores (I set it to 2 times)

 

Worker_cpu_affinity (this has never been used)

This option is only applicable to Linux. You can use this option to bind worker processes and CPUs (2.4 kernel machines are unavailable)

Worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

Allocate CPU to each process. In the previous example, eight processes are allocated to eight CPUs. Of course, you can write multiple processes or allocate one process to multiple CPUs.

 

Worker_rlimit_nofile 102400;

The maximum number of file descriptors opened by each nginx process must be consistent with the number of files opened by a single process in the system. In Linux 2.6, the number of opened files is 65535,

The theoretical value should be that the maximum number of files opened (ulimit-N) is the same as the number of nginx processes, but the nginx allocation request is not so uniform, so it is best to be consistent with the value of ulimit-n,

Worker_rlimit_nofile should be set to 65535.

 

Use epoll

Nginx uses the latest epoll (Linux 2.6 kernel) and kqueue (FreeBSD) network I/O models, while Apache uses the traditional select model.

The Select network I/O model adopted by Apache is very inefficient in handling a large number of connections and reads and writes.

In high-concurrency servers, polling I/O is the most time-consuming operation. Currently, squid and memcached, which can withstand high-concurrency access in Linux, use the epoll network I/O model.

 

Worker_connections 65535;
Maximum number of concurrent connections allowed by each worker (maxclient = work_processes * worker_connections)

 

Keepalive_timeout 75

Keepalive timeout time:

The parameters can differ from each other. Line keep-alive: timeout = Time understands Mozilla and Konqueror. MSIE itself shuts keep-alive connection approximately after 60 seconds.

 

Client_header_buffer_size 4 K
Large_client_header_buffers 4 32 K

The buffer size of the client request header, which can be set based on the size of your system page. Generally, the size of the header of a request does not exceed 1 K, but generally the system page size is greater than 1 K, set the page size here.

You can use the getconf pagesize command to obtain the page size.

Nginx uses the buffer client_header_buffer_size by default to read the header value. If the request header is too large, it uses large_client_header_buffers to read

If the setting is too small,

When the HTTP header is too large (usually the cookie is too large), the error 400 nginx 400 bad request will be reported.

When the request line is too large, an HTTP 414 error (URI Too long) will be reported)

For details, see http://www.jianshu.com/p/d028a42490b7.

 

Open_file_cache Max 102400 inactive = 20 s;

Field used: HTTP, server, location

This command specifies whether the cache is enabled, Which is disabled by default. If enabled, the following information is recorded:

Open file descriptor

Size and modification time

Existing directory information

Error message during file search -- the file cannot be correctly read without it. For details, refer to open_file_cache_errors Command Options:

Max-specifies the maximum number of cached files. It is recommended that the number of files be the same as the number of files opened. inactive refers to the time when the files are not requested and the cache is deleted. If the cache overflows, the files that have been used for the longest time (LRU) will be removed.

Example: open_file_cache max = 1000 inactive = 20 s; open_file_cache_valid 30 s; open_file_cache_min_uses 2; open_file_cache_errors on;

 

Open_file_cache_errors
Syntax: open_file_cache_errors on | off default value: open_file_cache_errors off field used: HTTP, server, location this command specifies whether to search a file is a cache error record.

 

Open_file_cache_min_uses

Syntax: open_file_cache_min_uses number default value: open_file_cache_min_uses 1 field used: HTTP, server, location

The inactive parameter in the open_file_cache command specifies the minimum number of times files are used. If this number is exceeded, the file descriptor is always opened in the cache. For example, if a file is not enabled once in the inactive time

Is removed.


Open_file_cache_valid

Syntax: open_file_cache_valid time default value: open_file_cache_valid 60 field used: HTTP, server, location this command specifies how long to check the effective information of cached items in open_file_cache.


Enable Gzip
Gzip on;
Gzip_min_length 1 K;
Gzip_buffers 4 16 K;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/X-JavaScript text/CSS

Application/XML;
Gzip_vary on;

 

Cache static files:

Location ~ * ^. + \. (SwF | GIF | PNG | JPG | JS | CSS) $ {
Root/usr/local/ku6/KTV/show.ku6.com /;
Expires 1 m;
}

 

Optimize Linux Kernel Parameters

Net. ipv4.tcp _ max_tw_buckets = 6000timewait. The default value is 180000.
 
Net. ipv4.ip _ local_port_range = 1024 65000 port range that allows the system to open.
 
Net. ipv4.tcp _ tw_recycle = 1 enable timewait fast recovery.
 
Net. ipv4.tcp _ tw_reuse = 1 Enable reuse. Allow time-Wait sockets to be re-used for a New TCP connection.
 
Net. ipv4.tcp _ syncookies = 1 enable syn cookies. When SYN waits for queue overflow, cookies are enabled for processing.
 
Net. core. somaxconn = 262144the backlog of the listen function in the Web application will give us the net of Kernel Parameters by default. core. somaxconn is limited to 128, while ngx_listen_backlog defined by nginx is 511 by default, so it is necessary to adjust this value.

Net. Core. netdev_max_backlog = 262144 the maximum number of packets that can be sent to the queue when each network interface receives packets faster than the kernel processes these packets.
 
Net. ipv4.tcp _ max_orphans = 262144 the maximum number of TCP sockets in the system is not associated with any user file handle. If this number is exceeded, the orphan connection is immediately reset and a warning is printed. This limit is only used to prevent simple DoS attacks. You cannot rely too much on it or artificially reduce the value. You should also increase this value (if the memory is increased ).
 
Net. ipv4.tcp _ max_syn_backlog = 262144 records the maximum number of connection requests that have not received confirmation from the client. For systems with 1024 MB of memory, the default value is 128, while for systems with small memory, the value is.
 
Net. ipv4.tcp _ timestamps = 0 Timestamp can avoid serial number winding. A 1 Gbit/s link must have a previously used serial number. The timestamp allows the kernel to accept such "abnormal" packets. Disable it here.
 
Net. ipv4.tcp _ synack_retries = 1 in order to enable the peer connection, the kernel needs to send a SYN with an ACK that responds to the previous syn. That is, the second handshake in the three-way handshake. This setting determines the number of SYN + ACK packets sent before the kernel disconnects.
 
Net. ipv4.tcp _ syn_retries = 1 Number of SYN packets sent before the kernel disconnects the connection.

Net. ipv4.tcp _ fin_timeout = 1 if the socket is disabled by the local end, this parameter determines the time it remains in the fin-wait-2 state. The peer can make an error and never close the connection, or even become an unexpected machine. The default value is 60 seconds. 2.2 The kernel value is usually 180 seconds. You can follow this setting, but remember that even if your machine is a lightweight web server, there is also a risk of memory overflow due to a large number of dead sockets. The risk of fin-wait-2 is smaller than that of fin-wait-1, because it can only eat K of memory at most, however, they have a longer lifetime.
 
Net. ipv4.tcp _ keepalive_time = 30 the frequency of keepalive messages sent by TCP when keepalive is in use. The default value is 2 hours.

VI/etc/sysctl. conf

# Add

A complete kernel Optimization Configuration
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
Net. ipv4.tcp _ tw_recycle = 1
Net. ipv4.tcp _ tw_reuse = 1
Net. ipv4.tcp _ mem = 94500000 915000000 927000000
Net. ipv4.tcp _ fin_timeout = 1
Net. ipv4.tcp _ keepalive_time = 30
Net. ipv4.ip _ local_port_range = 1024 65000

Several parameters to be optimized for nginx Configuration

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.