Key Points of Nginx optimization as a Web Server

Source: Internet
Author: User

Key Points of Nginx optimization as a Web Server

Nginx can be used as either a Web Server or a reverse Proxy. Here we will first discuss the general optimization points of Web Server.

Key points of common optimization

Nginx uses a fixed number of workers, and each worker processes incoming requests. The best practice is to configure a worker for each CPU kernel.

How many CPUs does your system have?

$ Grep ^ processor/proc/cpuinfo | wc-l

For a quad-core processor, the configuration file is similar:

# One worker per CPU-core.

Worker_processes 4;
Events {
Worker_connections 8096;
Multi_accept on;
Use epoll;
}
Worker_rlimit_nofile 40000;
Http {
Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
Keepalive_timeout 15;
# Your content here ..
}

Here we have improved the worker_connections setting and defined how many connections each worker process can process.

The maximum number of connections to the server is:

Worker_processes * worker_connections (= 32384 in this example)

Multi_accept is enabled here. This configuration item enables nginx to receive as many requests as possible, reducing the client connection initialization time.

Finally, the epoll event model is used in this example, which is also the best practice suggestion.
 
Compression

Many users enable the gzip compression module to make the returned client content shorter and faster.

However, compression consumes user server resources by monitoring CPU usage (open-source Hyperic can be used). If it is too high, you can disable compression.

Generally, only large files are compressed to avoid compressing files with poor compression effects, such as examples and executable files.

You can refer to the following Configuration:

Gzip on;
Gzip_vary on;
Gzip_min_length 10240;
Gzip_proxied expired no-cache no-store private auth;
Gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
Gzip_disable "MSIE [1-6] \.";

The above configuration only compresses text files larger than 10 KB.
 
Client Cache

If the client (usually a browser) deems that the latest content to be downloaded has been saved, no request will be sent to the nginx server.

This requires some cache settings. The simplest way is to set a fixed length of time for all images, js and other static content:

Location ~ * \. (Jpg | jpeg | gif | png | css | js | ico | xml) $ {
Access_log off;
Log_not_found off;
Expires 30d;
}
 

Here, we also disable media file logs and set the expiration time of some file suffixes to 30 days.
 
File handle Cache

If you need to process a large number of static files, you need to keep these file handles open to avoid opening them again later.

The following example can be placed in both the server section of nginx configuration and the master http block. :

Open_file_cache max = 2000 inactive = 20 s;
Open_file_cache_valid 60 s;
Open_file_cache_min_uses 5;
Open_file_cache_errors off;
 

Here, the server can cache up to 2000 opened file handles, and close the file handles that are not requested within 20 seconds. The validity period of the handle is 60 seconds, in addition, the cache is cached only when the number of visits exceeds 5. In this way, only frequently accessed files are cached to reduce access to the file system.
 
Optimize PHP

Many websites use PHP, such as drupal and wordpress.

Since nginx does not have its own mod_php, the recommended approach is to use the PHP-FPM and the request needs to be forwarded, for example:

# Execute all. php files via php-fpm
Location ~ . Php $ {
# Connect to a unix domain-socket:
Fastcgi_pass unix:/var/run/php5-fpm.sock;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Fastcgi_param SCRIPT_NAME $ fastcgi_script_name;
Fastcgi_buffer_size 128 k;
Fastcgi_buffers 256 16 k;
Fastcgi_busy_buffers_size 256 k;
Fastcgi_temp_file_write_size 256 k;
# This file is present on Debian systems ..
Include fastcgi_params;
}
 

Note that Unix domain-socket is used to connect to FPM, so you need to modify/etc/php5/fpm/pool. d/www. conf as follows:

Listen =/var/run/php5-fpm.sock

This ensures that FPM listens to a domain socket instead of the default ("listen = 127.0.0.1: 9000 ").

The default PHP-FPM starts some dedicated worker, each running one PHP instance. If the memory is sufficient, you can increase the number of workers to improve the concurrency throughput.

Edit the/etc/php5/fpm/pool. d/www. conf file and change the number, for example:

; Set a fixed number of php workers
Pm = static
; Start up 12 php processes
Pm. max_children = 12

This value needs to be adjusted according to the actual environment. The adjustment is based on the data collected using the monitoring tool.

Finally, you can configure the PHP-FPM to automatically restart. If a problem occurs, for example, the following configuration means that ten sub-processes will die in one minute, and the restart will allow the process to lose control for ten seconds.

Below is the global configuration in the/etc/php5/fpm/php-fpm.conf:

Emergency_restart_threshold 10
Emergency_restart_interval 1 m
Process_control_timeout 10 s
 
 
Inspection

We recommend that you deploy a monitoring tool to test the effectiveness of configuration optimization. The monitoring content should include:

Nginx: the open-source version provides only the following six monitoring metrics:

Connections, Accepts, Handled, Requests, Writing, Waiting

From the Operating System Perspective: it should include the CPU usage, memory usage, overall CPU usage, swap zone usage and other indicators of the Nginx process.

If it is running on a virtual machine, you should also pay attention to the ST (Steal Time) indicators of the operating system to determine whether there are oversold, overload, and other phenomena;

We recommend a free open-source tool, Hyperic.

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.