Nginx performance optimization Guide

Source: Internet
Author: User
Tags epoll sendfile keep alive

Nginx performance optimization Guide

Nginx is a world-renowned high-performance Web server and Load balancer. This Nginx performance optimization guide aims to help you quickly optimize the performance of Nginx servers and deliver website content to users.

Configure Worker

The simplest optimization is to modify Worker and configure the number of connections.

Worker Process

If you run Nginx and databases on the same server, and the website traffic is small, you can set worker_processes 1 in the nginx. conf configuration file;

If your website traffic is large or Nginx runs as a single server, you can allocate Worker and worker_processes auto to each CPU core;

If you do not want to configure the CPU cores manually, run the following command to view the number of CPU cores and change the number of worker_processes.

  1. Grep ^ processor/proc/cpuinfo | wc-l
Number of Worker connections

Worker_connections allows you to configure the maximum number of connections of each Worker process at the same time. By default, the number of Worker connections is limited to 512, but in fact the system can process more connections.

The number of Worker connections can be determined by checking the system's core limits. You only need to use the following command:

  1. Ulimit-n

We can also set the use epoll mechanism. This option is an I/O event notification mechanism that ensures maximum I/O utilization.

Finally, we can use multi_accept to allow Worker to accept all new connections at the same time.

After the above event mechanism is configured, the/etc/nginx. conf configuration file is similar to the following:

  1. Events {
  2. Worker_connections 66536;
  3. Use epoll;
  4. Multi_accept on;
  5. }
Optimizing Keep Alive over HTTP and TCP

Keep Alive can achieve less reconnection in the client browser:

  • Keepalive_timeout and keepalive_requests control keep alive settings
  • Sendfile user optimized static files
  • Tcp_nodelay allows Nginx to send multiple buffers as separate data packets
  • Tcp_nopush is used to activate the TCP_CORK option of the TCP stack.

After configuration, the/etc/nginx. conf configuration file is similar to the following:

  1. Keepalive_timeout 65;
  2. Keepalive_requests 100000;
  3. Sendfile on;
  4. Tcp_nopush on;
  5. Tcp_nodelay on;
Buffer size

Buffer-related configuration is used to adjust the Buffer size. If the Buffer configuration is too low, Nginx will need to write a temporary file, it will be sent to too many disk I/O.

  • Client_body_buffer_size is used to process the buffer size of the client. Most client buffers use the form submitted in POST mode. Therefore, setting it to 128 K is a good choice.
  • Client_max_body_size is used to limit the maximum buffer size. If the request size exceeds the configured value, a 413 error is returned to the client (the browser generally does not display the 413 error ).
  • Client_header_buffer_size is used to process the client header size. The default value is 1 K.
  • Large_client_header_buffers displays the maximum value and size of the header of a large client. 4 header and 4 k buffer should be sufficient.
  • Output_buffers sets the number and size of buffer responses read from the disk. If possible, client data transmission will be delayed until Nginx collects the appropriate data size.

After configuration, the/etc/nginx. conf configuration file is similar to the following:

  1. Client_body_buffer_size 128 k;
  2. Client_max_body_size 10 m;
  3. Client_header_buffer_size 1 k;
  4. Large_client_header_buffers 44 k;
  5. Output_buffers 132 k;
  6. Post pone_output 1460;
Connection queue

The/etc/sysctl. conf configuration file option can be used to set the Linux connection queue and buckets size. Changing the net. core. somaxconn and net. ipv4.tcp _ max_tw_buckets sizes can change the Nginx wait time.

After configuration, the/etc/sysctl. conf configuration file is similar to the following:

  1. Net. core. somaxconn = 65536
  2. Net. ipv4.tcp _ max_tw_buckets = 1440000
Timeout Configuration

Timeout configuration can also significantly improve Nginx performance:

  • Client_body_timeout is used to set the time-out time of the sender.
  • Sent_timeout is used to set the timeout response time of the client.
  • Client_header_timeout is used to configure the sending time after the client header request

After configuration, the/etc/nginx. conf configuration file is similar to the following:

  1. Client_header_timeout 3 m;
  2. Client_body_timeout 3 m;
  3. Send_timeout 3 m;
Gzip Compression

For plain text content, Nginx can use gzip compression. You can use the following parameters to specify the type to be compressed.

After configuration, the/etc/nginx. conf configuration file is similar to the following:

  1. Gzip on;
  2. Gzip_min_length 1000;
  3. Gzip_types: text/html application/x-javascript text/css application/javascript text/plain text/xml application/json application/vnd. ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/xml font/eot font/opentype font/otf image/svg + xml image/vnd. microsoft. icon;
  4. Gzip_disable "MSIE [1-6] \.";
Static File Cache

If your website has a large number of static resources (such as CSS/JS/images), Nginx can configure static file caching. We can use the following configuration to set the cache file type and Cache Time:

  1. Location ~ *. (Woff | eot | ttf | svg | mp4 | webm | jpg | jpeg | png | gif | ico | css | js) $ {
  2. Expires 365d;
  3. }
File System Optimization

The following configuration improves the system's memory management capability. We need to modify the/etc/sysctl. conf configuration file.

When Nginx is used as a proxy server, a temporary port is used whenever it is connected to the upstream server. Run the following command to limit the local IPv4 port on the server to a fixed interval:

  1. Net. ipv4.ip _ local_port_range 102465000

When tcp fin times out, the port must be inactive before it can be used for another connection. The default timeout value is 60 seconds. We can reduce it to 30 seconds or even 15 seconds:

  1. Net. ipv4.tcp _ fin_timeout 15

The zooming option of the TCP window can set the size of the received window to 65535 bytes. The TCP option can be activated by using the following command:

  1. Net. ipv4.tcp _ window_scaling = 1

The net. ipv4.tcp _ max_syn_backlog parameter can be used to configure the size of data packets that can be accumulated by the kernel. If the data packet size exceeds the value, it is discarded. We can set it to the following reference value:

  1. Net. ipv4.tcp _ max_syn_backlog = 3240000

We can configure to close the connection when the client loses response.

  1. Reset_timedout_connection on;

Of course, the administrator can select conditional logs. The following configuration example does not record logs with HTTP Response codes 2XX and 3XX:

Add the/etc/nginx. conf configuration file

  1. Map $ status $ loggable {
  2. ~ ^ [23] 0;
  3. Default1;
  4. }

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.