溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Nginx常用配置詳解(一)——主配置塊篇

發(fā)布時(shí)間:2020-06-07 18:02:27 來源:網(wǎng)絡(luò) 閱讀:1254 作者:司徒剩堂 欄目:建站服務(wù)器

Nginx常用配置詳解(一)

本文依照nginx官方站點(diǎn)文檔介紹常用的nginx各種常用配置,未經(jīng)過校對(duì),如有錯(cuò)誤還望海涵。

Nginx配置通用語法

Nginx最基本的配置語法

配置項(xiàng)名 配置項(xiàng)值1 [配置項(xiàng)值2 ....];
配置項(xiàng)名位于行首,配置項(xiàng)值與配置項(xiàng)名之間用空格隔開,多個(gè)配置項(xiàng)值之間也用空格隔開,每行配置結(jié)尾必須加上分號(hào)。

#配置項(xiàng)名 配置項(xiàng)值1 [配置項(xiàng)值2 ....];
#可以注釋掉本行

Nginx配置分為各個(gè)配置塊。主配置塊負(fù)責(zé)全局配置,各個(gè)子塊都會(huì)繼承全局配置。各個(gè)子塊也各有不同的配置項(xiàng)。

main block:主配置(全局配置)
    event{
        ...
    }事件驅(qū)動(dòng)相關(guān)配置塊
    http{
        ...
    }http/https 協(xié)議相關(guān)的配置塊
    mail{
        ...
    }郵件服務(wù)器相關(guān)的配置塊
    stream{
        ...
    }流服務(wù)器相關(guān)的配置塊

主配置塊配置

主配置按功能分為四類:

  1. 正常運(yùn)行必備的配置

  2. 優(yōu)化性能相關(guān)的配置

  3. 用于調(diào)試及定位問題的相關(guān)的配置

  4. 事件驅(qū)動(dòng)相關(guān)的配置

一、正常運(yùn)行必備的配置

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進(jìn)程使用的用戶或者組的憑證,省略組名表示組名與用戶名相同。

pid

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

Defines a file that will store the process ID of the main process.
指定存儲(chǔ)nginx matser進(jìn)程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可以是絕對(duì)路徑也可以是相對(duì)路徑,相對(duì)路徑為相對(duì)Nginx配置文件的路徑,即Nginx.conf所在目錄)

load_module

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

Loads a dynamic module.
加載動(dòng)態(tài)模塊。此指令只在ngnix 1.9.11 版本后生效

二、性能優(yōu)化相關(guān)的配置

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進(jìn)程數(shù)量。該設(shè)定會(huì)直接影響性能,最佳值取決于多種因素包括但不限于CPU核心、存書數(shù)據(jù)的硬盤數(shù)量,加載模式。較好的選擇是設(shè)定該值值等于可用的CPU數(shù)量(auto自動(dòng)檢測(cè)CPU核心數(shù)量并以此為該項(xiàng)的設(shè)定值)。

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.
將設(shè)定的CPU核心與worker進(jìn)程綁定,每個(gè)CPU設(shè)定用位掩碼分別綁定給每一個(gè)worker進(jìn)程。默認(rèn)情況下worker進(jìn)程不綁定在任何一個(gè)CPU上。(每一位CPUmask代表一個(gè)CPU核心)
例如:
主機(jī)有四個(gè)核心,建立四個(gè)worker進(jìn)程分別綁定在每個(gè)CPU上

 worker_processes4;
 worker_cpu_affinity 0001 0010 0100 1000;

主機(jī)有四個(gè)核心,建立兩個(gè)worker進(jìn)程,第一個(gè)進(jìn)程綁定在CPU0/CPU2上,第二個(gè)進(jìn)程綁定在CPU1/CPU3上

 worker_processes2;
 worker_cpu_affinity 0101 1010;

使用自動(dòng)自動(dòng)綁定

 worker_processes auto;
 worker_cpu_affinity auto;

自動(dòng)綁定并限制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進(jìn)程的優(yōu)先級(jí),相當(dāng)于nice指令:負(fù)數(shù)的優(yōu)先級(jí)更高,取值范圍從-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進(jìn)程能打開文件的最大值,可以在不重啟主進(jìn)程的情況下增加限制。

三、調(diào)試、定位問題

daemon

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

Determines whether nginx should become a daemon. Mainly used during development.
決定nginx是否成為守護(hù)進(jìn)程,主要用于開發(fā)期間。

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進(jìn)程。此指令打算給nginx開發(fā)者使用。

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.
配置日志,幾個(gè)日志可以被指定為同一級(jí)別。如果主配置文件級(jí)別中配置文件路徑?jīng)]有明確指明,則使用默認(rèn)配置。

第一個(gè)字段定義日志存儲(chǔ)文件位置。特殊值stderr選擇標(biāo)準(zhǔn)錯(cuò)誤文件。針對(duì)syslog的文件可以在前面用syslog:指明。針對(duì)cyclic memory buffer可以在前面用memory:指明,并且要指明緩沖大小,此項(xiàng)指令通常用于調(diào)試。

第二字段判定日志級(jí)別,在debug, info, notice, warn, error, crit, alert, emerg之中選擇一項(xiàng)。這些日志級(jí)別從左到右依次從輕微到嚴(yán)重。確定日志級(jí)別后,會(huì)記錄該級(jí)別和該級(jí)別以上的級(jí)別的所有日志。例如:設(shè)定error級(jí)別會(huì)記錄error, crit, alert, emerg四個(gè)基本,如果該條目省略,則默認(rèn)級(jí)別為error。

四、事件驅(qū)動(dòng)相關(guān)配置

事件驅(qū)動(dòng)相關(guān)的配置配置與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.
設(shè)定worker進(jìn)程同步連接最大值。

這項(xiàng)設(shè)定需要注意,這個(gè)數(shù)字包括了所有連接(例如:代理連接服務(wù)器等),不僅僅是客戶端的連接。
另一個(gè)值得注意的問題是實(shí)際的同步連接數(shù)值要小于之前在 worker_rlimit_nofile中設(shè)定的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.
指明使用的連接進(jìn)程方法。通常不需要明確的指明,因?yàn)镹GINX默認(rèn)會(huì)使用最有效的方法。

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進(jìn)程在接受新連接時(shí)采取輪流進(jìn)行的模式。如果不這么設(shè)定,新連接將不會(huì)通知給各worker進(jìn)程。在新連接較少的情況下,部分worker進(jìn)程資源將被浪費(fèi)。

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進(jìn)程正在接受新連接時(shí),worker進(jìn)程重新接受新連接的超時(shí)時(shí)間。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI