Nginx常用配置详解(一)

本文依照nginx官方站点文档介绍常用的nginx各种常用配置,未经过校对,如有错误还望海涵。

Nginx配置通用语法

Nginx最基本的配置语法

配置项名 配置项值1 [配置项值2 ....];
配置项名位于行首,配置项值与配置项名之间用空格隔开,多个配置项值之间也用空格隔开,每行配置结尾必须加上分号。

#配置项名 配置项值1 [配置项值2 ....];
#可以注释掉本行

Nginx配置分为各个配置块。主配置块负责全局配置,各个子块都会继承全局配置。各个子块也各有不同的配置项。

main block:主配置(全局配置)
    event{
        ...
    }事件驱动相关配置块
    http{
        ...
    }http/https 协议相关的配置块
    mail{
        ...
    }邮件服务器相关的配置块
    stream{
        ...
    }流服务器相关的配置块

主配置块配置

主配置按功能分为四类:

  1. 正常运行必备的配置

  2. 优化性能相关的配置

  3. 用于调试及定位问题的相关的配置

  4. 事件驱动相关的配置

一、正常运行必备的配置

user

Syntax: user user [group];
Default: user nobody nobody;
Context: main

Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.
定义worker进程使用的用户或者组的凭证,省略组名表示组名与用户名相同。

pid

Syntax: pid file;
Default: pid nginx.pid;
Context: main

Defines a file that will store the process ID of the main process.
指定存储nginx matser进程ID的文件路径。

include

Syntax: include file | mask;
Default: —
Context: any

Includes another file, or files matching the specified mask, into configuration. Included files should consist of syntactically correct directives and blocks.
配置文件可嵌入其他配置文件,include指明嵌入的文件位置可以是明确的文件名,也可以是含有通配符的文件名。(include可以是绝对路径也可以是相对路径,相对路径为相对Nginx配置文件的路径,即Nginx.conf所在目录)

load_module

Syntax: load_module file;
Default: —
Context: main
This directive appeared in version 1.9.11.

Loads a dynamic module.
加载动态模块。此指令只在ngnix 1.9.11 版本后生效

二、性能优化相关的配置

worker_processes

Syntax: worker_processes number | auto;
Default: worker_processes 1;
Context: main

Defines the number of worker processes.

The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it).
定义worker进程数量。该设定会直接影响性能,最佳值取决于多种因素包括但不限于CPU核心、存书数据的硬盘数量,加载模式。较好的选择是设定该值值等于可用的CPU数量(auto自动检测CPU核心数量并以此为该项的设定值)。

worker_cpu_affinity

Syntax: worker_cpu_affinity cpumask ...;
        worker_cpu_affinity auto [cpumask];
Default: —
Context: main

Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.
将设定的CPU核心与worker进程绑定,每个CPU设定用位掩码分别绑定给每一个worker进程。默认情况下worker进程不绑定在任何一个CPU上。(每一位CPUmask代表一个CPU核心)
例如:
主机有四个核心,建立四个worker进程分别绑定在每个CPU上

 worker_processes4;
 worker_cpu_affinity 0001 0010 0100 1000;

主机有四个核心,建立两个worker进程,第一个进程绑定在CPU0/CPU2上,第二个进程绑定在CPU1/CPU3上

 worker_processes2;
 worker_cpu_affinity 0101 1010;

使用自动自动绑定

 worker_processes auto;
 worker_cpu_affinity auto;

自动绑定并限制CPU使用

 worker_cpu_affinity auto 01010101;

worker_priority

Syntax: worker_priority number;
Default: worker_priority 0;
Context: main

Defines the scheduling priority for worker processes like it is done by the nice command: a negative number means higher priority. Allowed range normally varies from -20 to 20.
定义worker进程的优先级,相当于nice指令:负数的优先级更高,取值范围从-20到20。

worker_rlimit_nofile

Syntax: worker_rlimit_nofile number;
Default: —
Context: main

Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.
修改worker进程能打开文件的最大值,可以在不重启主进程的情况下增加限制。

三、调试、定位问题

daemon

Syntax: daemon on | off;
Default: daemon on;
Context: main

Determines whether nginx should become a daemon. Mainly used during development.
决定nginx是否成为守护进程,主要用于开发期间。

master_process

Syntax: master_process on | off;
Default: master_process on;
Context: main

Determines whether worker processes are started. This directive is intended for nginx developers.
决定是否启用worker进程。此指令打算给nginx开发者使用。

error_log

Syntax: error_log file [level];
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location

Configures logging. Several logs can be specified on the same level (1.5.2). If on the main configuration level writing a log to a file is not explicitly defined, the default file will be used.

The first parameter defines a file that will store the log. The special value stderr selects the standard error file. Logging to syslog can be configured by specifying the “syslog:” prefix. Logging to a cyclic memory buffer can be configured by specifying the “memory:” prefix and buffer size, and is generally used for debugging (1.7.11).

The second parameter determines the level of logging, and can be one of the following: debug, info, notice, warn, error, crit, alert, or emerg. Log levels above are listed in the order of increasing severity. Setting a certain log level will cause all messages of the specified and more severe log levels to be logged. For example, the default level error will cause error, crit, alert, and emerg messages to be logged. If this parameter is omitted then error is used.
配置日志,几个日志可以被指定为同一级别。如果主配置文件级别中配置文件路径没有明确指明,则使用默认配置。

第一个字段定义日志存储文件位置。特殊值stderr选择标准错误文件。针对syslog的文件可以在前面用syslog:指明。针对cyclic memory buffer可以在前面用memory:指明,并且要指明缓冲大小,此项指令通常用于调试。

第二字段判定日志级别,在debug, info, notice, warn, error, crit, alert, emerg之中选择一项。这些日志级别从左到右依次从轻微到严重。确定日志级别后,会记录该级别和该级别以上的级别的所有日志。例如:设定error级别会记录error, crit, alert, emerg四个基本,如果该条目省略,则默认级别为error

四、事件驱动相关配置

事件驱动相关的配置配置与events配置块中

events {
    ...
}

worker_connections

Syntax: worker_connections number;
Default: worker_connections 512;
Context: events

Sets the maximum number of simultaneous connections that can be opened by a worker process.

It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.
设定worker进程同步连接最大值。

这项设定需要注意,这个数字包括了所有连接(例如:代理连接服务器等),不仅仅是客户端的连接。
另一个值得注意的问题是实际的同步连接数值要小于之前在 worker_rlimit_nofile中设定的open file值。

use

Syntax: use method;
Default: —
Context: events

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.
指明使用的连接进程方法。通常不需要明确的指明,因为NGINX默认会使用最有效的方法。

accept_mutex

Syntax: accept_mutex on | off;
Default: accept_mutex off;
Context: events

If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.
如果accept_mutex启用,worker进程在接受新连接时采取轮流进行的模式。如果不这么设定,新连接将不会通知给各worker进程。在新连接较少的情况下,部分worker进程资源将被浪费。

accept_mutex_delay
Syntax: accept_mutex_delay time;
Default: accept_mutex_delay 500ms;
Context: events

If accept_mutex is enabled, specifies the maximum time during which a worker process will try to restart accepting new connections if another worker process is currently accepting new connections.
accept_mutex启用的情况下,指明在其他worker进程正在接受新连接时,worker进程重新接受新连接的超时时间。