Home > Backend Development > PHP Tutorial > In-depth understanding of Nginx core configuration

In-depth understanding of Nginx core configuration

WBOY
Release: 2016-07-28 08:27:13
Original
980 people have browsed it

Original link: http://blog.csdn.net/xyang81/article/details/51814787

Nginx configuration is organized in units of modules. Each module contains one or more instructions, and the instructions are in the configuration file The smallest configuration unit, all configuration items are instructions. For example, the include, default_type, and sendfile instructions in the http core module all belong to the http module. For instructions in all nginx modules, see the official documentation: http://nginx.org/en/docs/dirindex.html

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

1. Server-level core configuration

command Context Syntax Default Value Function description
user main user nobody nobyd; nobody With which user permissions to run the worker thread
daemon main daemon yes ; yes Whether nginx runs as a daemon?
worker_processes main worker_processes number; 1 Configure the number of worker processes. Traditional web servers (such as Apache) are all synchronous blocking models, one request is entered (thread) mode. When the number of incoming (threads) increases to a certain extent, more CPU time is wasted in thread and process switching, performance Dropped sharply, so the load rate is not high. Nginx is an event-based non-blocking multiplexing (epoll or kquene) model, and a process can respond to a large number of requests in a short period of time. It is recommended to set this value to
worker_connections events worker_connections number; 1024 The key configuration value of concurrent response capability, indicating the maximum number of simultaneous connections allowed per process. maxConnection = work_connections * worker_processes; Generally, a browser will open two connections at the same time. If it is a reverse proxy, the number of connections to the server after nginx arrives will also occupy 2 connections. Therefore, when making a static server, generally maxConnection = work_connections * worker_processes / 2; When working as an anti-proxy server, maxConnection = work_connections * worker_processes / 4;
use events use epoll; According to different platforms, select the most efficient connection processing method Specify the connection request processing method method. Linux kernel 2.6 and above use the epoll method by default. For other platforms, please refer to: http://nginx.org/en/docs/events.html Note: To achieve the best network response capability under ultra-high load, it is necessary to optimize the network Related linux kernel parameters
worker_cpu_affinity main worker_cpu_affinity cpumask…; None Bind the worker process to a specific CPU, reducing the CPU overhead of switching between processes. Use binary bits to indicate which CPU core the process is bound to. For example, the setting method when using 8 cores and 4 processes: worker_cpu_affinity 00000001 00000010 00000100 10000000
worker_rlimit_nofile main worker_rlimit_core size; is limited by the number of Linux kernel file descriptors Set the maximum number of file descriptors that nginx can open . Because Linux has a limit on the number of file descriptions that can be opened by each process, the default is generally 1024. You can modify the limit on the number of file handles that Linux can open by default through ulimit -n FILECNT or /etc/securit/limits.conf configuration. . The recommended value is: maximum number of systems/number of processes. However, the workload among processes is not evenly distributed, so it can be set larger. 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 of the error log file and log level. Log levels include debug, info, notice, warn, error, crit, alert and emerg. nginx logs are output using syslog, so the output log format is regular, and system operation and maintenance personnel can conduct error checking or statistical analysis according to the log rules. For more instructions, 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 the nginx daemon ID storage file path (not the worker process)

The above 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 is an HTTP reverse proxy server that is most commonly contacted. It should be the relevant configuration for http requests. All configurations related to the http module are placed in the http { ... } configuration.

Command Context Syntax Function description
types http, server, location types { mime type file suffix; }; Configure the file types that can be processed. For example: text/html html htm shtml;
include any include file path; Copy the contents of the external file to the nginx.conf file as configuration. For example: include mime.type; Copy the mime.type configuration file in the current directory to the nginx configuration file. File paths can be relative or absolute. File names can use * to represent wildcard characters.
default_type http, server, location default_type mime type; File name to suffix mapping relationship. Configure the default mime type. When the requested file type is not found in the types directive, the type specified by default_type is used. The default is text/plain type.
access_log http, server, location, if in location, limit_except access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Turn off or enable access logs. The default configuration is: access_log logs/access.log combined; means that the log format defined by combined is written to the logs/access.log file. combined is the default format of the http module. If one of the buffer and gzip parameters is defined, the log will be written to the cache by default. When the cache is full, the log in the cache will be compressed by gzip and written to the file. If gzip compression is enabled, you must ensure that gzip is added when nginx is installed. module. The cache size defaults to 64K. You can configure gzip compression levels from 1 to 9. The higher the level, the greater the compression efficiency and the smaller the space occupied by the log file, 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; Define the format of http access log, which can be accessed in log format The built-in variables of the http module, if they exist, will be output as logs. Such as: remotea ddr , request, etc. For more variables, please refer to: 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. Being a static server can increase the maximum IO access speed. Traditional file reading and writing uses read and write methods. The process is: hard disk>> kernel buffer>> user buffer>> kernel socket buffer>> protocol stack. The process of reading and writing files using sendfile is: hard disk >> kernel buffer (quickly copy to kernelsocket buffer) >> Protocol stack, it is obvious that the sendfile system call reduces the number of switches and data copies between the kernel and user mode, and directly copies the data from the kernel cache to the protocol The stack improves a lot of efficiency. This article introduces it in more detail: http://xiaorui.cc/?p=1673
tcp_nodelay http, server, location off|on;
tcp_nopush http, server, The two parameters location off|on; tcp_nodelay and tcp_nopush are used together. When these two configurations are enabled, the data will be sent after the data packet reaches a certain size. This will reduce the number of network communications and reduce the probability of blocking, but it will also affect the timeliness of response. It is more suitable for big data communication scenarios such as file downloading.
keepalive_timeout http, server, location keepalive_time 65; The timeout for the client to establish a connection to the server. The server will disconnect after the specified time. The default is 75 seconds. Reducing the alive time of each connection can increase the number of responsive connections to a certain extent, so generally this value can be appropriately reduced
gzip http, server, location, if in location gzip on | off; Turning on content compression can effectively reduce client access traffic and network bandwidth
gzip_min_length http, server, location gzip_min_length length; The unit is k, and the default is 20k. Turn on compression only when the content exceeds the minimum length, because content that is too short will not be compressed well, and the compression process will also waste system resources. This compressed length will be returned to the client as the Content-Length field of the http response header. Recommended value: 1000
gzip_comp_level http, server, location gzip_comp_level 1~9; compression level, the default value is 1. The range is from 1 to 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 html text is compressed. Generally, we will compress js, css, json and the like. These common text data can be combined. 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; Set the maximum number of caches and the unused lifetime of cached files. Recommended value: max=655350 (consistent with worker_rlimit_nofile parameter) inactive=20s;
open_file_
cache_min_uses
http, server, location open_file_cache_min_uses number; default is 1, the minimum number of file uses within the validity period. Recommended value: 2
open_file
_cache_valid
http, server, location open_file_cache_valid time; default is 60s, verification cache validity interval. It means to check the cached files every 60s. If any files have not been used more than 2 times within 20s, they will be deleted from the cache. Use lru algorithm.
server server { … } http The core configuration of the HTTP server, used to configure the virtual host of the HTTP server, multiple
listen listen ip[:port] server Configure the IP address and port that the virtual host listens to. By default, it listens to the local IP address and port 80 or 8000. If only the IP is set but no port is set, port 80 is used by default. If only the port is set and the IP is not set, the local IP is used by default. 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 Configure the domain name of the virtual host, you can specify multiple , separated by spaces. The default is empty
charset http, server, location, if in location charset charset | off; Set the request encoding, which is related to the problem of garbled url parameters.
location server, location location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
An important configuration item in the http request, used to configure the client The matching rule for the end request server URL address. Multiple matching rules can be configured
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced an in-depth understanding of Nginx core configuration, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template