Nginx core configuration in-depth understanding

Source: Internet
Author: User
Tags epoll sendfile
Original link: http://blog.csdn.net/xyang81/article/details/51814787

Nginx configuration is organized in modules, each module contains one or more instructions, the configuration file is the smallest configuration unit, all configuration items are directives. such as the include, Default_type, sendfile directives in the HTTP core module, are all part of the HTTP module. The instructions in all Nginx modules are described in the official documentation: http://nginx.org/en/docs/dirindex.html

注意:以下配置中的“上下文”表示指令可以配置在哪些模块中。main:顶层配置,约束服务器的行为

1. Server-level core configuration

instruction Context Grammar Default Value function Description
User Main User nobody nobyd; Nobody Which user rights to run worker threads
Daemon Main Daemon Yes; Yes Nginx is running as daemon
Worker_processes Main Worker_processes number; 1 Configures the number of worker processes. Traditional Web servers (such as Apache) are synchronous blocking models, a request-in (line) mode, when the number of incoming (line) to a certain extent, more CPU time wasted in the thread and process switching, the performance dropped sharply, so the load rate is not high. Nginx is an event-based, non-blocking multiplexing (Epoll or kquene) model in which a process can respond to a large number of requests in a short period of time. It is recommended that you set this value to the number of <=CPU cores, which is generally higher than the number of CPU cores, and may have a negative impact on process switching overhead.
Worker_connections Events Worker_connections number; 1024 The key configuration value for the concurrency response capacity, which represents the maximum number of simultaneous connections allowed per process. maxconnection = work_connections * worker_processes; usually a browser will open two connections at the same time, if it is a reverse proxy, nginx to the server after the number of connections to occupy 2 connections, so, do a static server, General maxconnection = work_connections * WORKER_PROCESSES/2; When doing anti-proxy Server maxconnection = work_connections * WORKER_PROCESSES/4;
Use Events Use Epoll; Choose the most efficient connection processing method based on different platforms Specifies the method that handles the connection request. Linux kernel 2.6 By default using the Epoll method, other platforms please refer to: http://nginx.org/en/docs/events.html Note: To achieve the best network responsiveness under ultra-high load, it is also necessary to optimize network-related Linux kernel parameters
Worker_cpu_affinity Main Worker_cpu_affinity cpumask ...; No Binds the worker process to a specific CPU, reducing the overhead of the CPU switching between processes. Use a binary bit bit to indicate which CPU core the process is bound to. Setup methods such as 8 kernel 4 processes: worker_cpu_affinity 00000001 00000010 00000100 10000000
Worker_rlimit_nofile Main Worker_rlimit_core size; Limited by the number of Linux kernel file descriptors Sets the maximum number of file descriptors that can be opened by Nginx. Because Linux has a limit on the number of file descriptions that each process can open, the default is typically 1024, which can be modified by the Ulimit-n filecnt or/etc/securit/limits.conf configuration to limit the number of file handles that Linux can open by default. Recommended values are: System maximum/number of processes. However, the inter-process workload is not evenly distributed, so it can be set at a larger number. Recommended values are: 655350
Error_log Main, http, mail, stream, server, location Error_log log file path log level; Error_log Logs/error.log error; Configure the path and log level of the error log file. Log levels are available in debug, info, notice, warn, error, crit, alert, and Emerg. Nginx log using syslog output, so the output of the log format is regular, the system operators can be based on the log rules for error-checking or statistical analysis. For more information, please refer to the official documentation: Http://nginx.org/en/docs/ngx_core_module.html#error_log
Pid Main PID daemon socket file path; PID Logs/nginx.pid Configure Nginx Daemon ID to store file path (not worker process)

This is the top-level configuration of Nginx, which manages server-level behavior. For more configuration Please refer to the official documentation: Http://nginx.org/en/docs/ngx_core_module.html#working_directory

2. HTTP Module Core Configuration

Nginx as an HTTP reverse proxy server, usually the most exposed to the HTTP request should be related to the configuration, and the HTTP module all the configuration is placed in the http { ... } configuration.

instruction Context Grammar function Description
Types HTTP, server, location Types {MIME type file suffix;}; Configures the types of files that can be processed. such as: text/html html htm shtml;
Include Any Include file path; Copy the contents of the external file into the nginx.conf file as a configuration. Such as: include Mime.type; Copy the Mime.type configuration file from the current directory to the Nginx configuration file. The file path can be a relative path or an absolute path. The file name can be represented by a * wildcard character.
Default_type HTTP, server, location Default_type MIME type; The mapping of file names to suffixes. Configures the default MIME type, using the type specified by Default_type when the requested file type is not found in the types Directive. The default is the Text/plain type.
Access_log HTTP, server, location, if on location, limit_except Access_log path [format [buffer=size] [gzip[=level] [flush=time] [if=condition]];
Access_log off;
Turn off or turn on access logging. The default configuration is: Access_log Logs/access.log combined; Represents the log format defined by combined, written to the Logs/access.log file, combined is the HTTP module default format. If one of the parameters of buffer and gzip is defined, the log is written to the cache by default, and when the cache is full, the logs in the cache are compressed by gzip and written to the file, gzip compression must be enabled to add the Gzip module when the Nginx is installed. The cache size defaults to 64K. The 1~9 compression level of gzip can be configured, the higher the compression efficiency, the smaller the log file occupies, but the higher the system performance is required. The default value is 1. Http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
log_format http log_format format name log format; defines the format of the HTTP access log, which allows access to the inline variables of the HTTP module in the log format. If it changes, it will be the log output. Example: r e m o T e a d d R , request, and more variables, see: Http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
Sendfile HTTP, server, location, if in location Sendfile on | Off Enable kernel copy mode. As a static server, you can increase the maximum IO access speed. Traditional file read and write using read and write mode, the process is: HDD >> kernel buffer >> user buffer>> kernel socket buffer >> protocol stack, The process of reading and writing with sendfile files is: HDD >> kernel buffer (fast copy to Kernelsocket buffer) >> protocol stack, Obviously sendfile this system call reduces the number of transitions and copies of data between kernel-to-user mode, directly copying the data from the kernel cache to the protocol stack, which improves efficiency. This article describes the comparative details: http://xiaorui.cc/?p=1673
Tcp_nodelay HTTP, server, location Off|on;
Tcp_nopush HTTP, server, location Off|on; The two parameters, Tcp_nodelay and Tcp_nopush, are used together to start both configurations and send the data after the packet reaches a certain size. This reduces the number of network traffic, reduces the probability of blocking, but also affects the timeliness of response. Compare large data communication scenarios that are appropriate for file downloads.
Keepalive_timeout HTTP, server, location Keepalive_time 65; The timeout of the client-to-server connection has been exceeded, and the server has been disconnected for more than the specified time. The default is 75 seconds. Reducing the alive time per connection can increase the number of responsive connections to some extent, so this value can generally be appropriately lowered
Gzip HTTP, server, location, if in location gzip on | Off Turn on content compression to effectively reduce client access and network bandwidth
Gzip_min_length HTTP, server, location Gzip_min_length length; The unit is k, which defaults to 20k. When the content exceeds the minimum length, the compression is turned on because too short content compression is poor and the compression process wastes system resources. This compression length is returned to the client as the HTTP response header content-length field. Recommended value: 1000
Gzip_comp_level HTTP, server, location Gzip_comp_level 1~9; The compression level, the default value is 1. Range is 1~9, the higher the compression level, the higher the compression rate, but the higher the system performance requirements. Recommended Value: 4
Gzip_types HTTP, server, location Gzip_types Mime-type ...; Compressed content type, default is text/html;. Only compressed HTML text, generally we will compress JS, CSS, JSON and so on, you can match these common text data. such as: Text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/ JavaScript
Open_file_cache HTTP, server, location Open_file_cache off; Open_file_cache max=n [Inactive=time]; The default value is off; Sets the maximum number of caches, and the lifetime of the cache file that is not used. Recommended value: max=655350 (consistent with worker_rlimit_nofile parameters) inactive=20s;
Open_file_
Cache_min_uses
HTTP, server, location Open_file_cache_min_uses number; The default is 1, and the minimum number of times a file is in the validity period. Recommended Value: 2
Open_file
_cache_valid
HTTP, server, location Open_file_cache_valid time; The default is 60s, which verifies the cache validity interval. Indicates that every 60s checks the cached files, which files are not used more than 2 times within 20s, and are removed from the cache. The LRU algorithm is used.
Server server {...} http The core configuration of the HTTP server, the virtual host for configuring the HTTP server, can be configured with multiple
Listen Listen ip[: Port] Server Configure the IP address and port on which the virtual host listens, by default listening for native IP addresses and 80 or 8000 ports. If only IP is not set, port 80 is used by default. If only the port is set, no IP is set by default using native IP. For detailed configuration, please refer to: Http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
server_name server_name domain_name ...; Server To configure the domain name of the virtual host, you can specify multiple, separated by spaces. Default is empty
CharSet HTTP, server, location, if in location CharSet CharSet | Off Set the request encoding, and the URL parameter garbled problem.
Location Server, Location Location [= | ~ | ~* | ^~] URI {...}
Location @name {...}
An important configuration item in an HTTP request that configures the matching rules for the client Pull server URL address. Multiple matching rules can be configured

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the Nginx core configuration in-depth understanding, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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