您好,登錄后才能下訂單哦!
本文依照nginx官方站點文檔介紹常用的nginx各種常用配置,未經過校對,如有錯誤還望海涵。
Nginx最基本的配置語法
配置項名 配置項值1 [配置項值2 ....]; 配置項名位于行首,配置項值與配置項名之間用空格隔開,多個配置項值之間也用空格隔開,每行配置結尾必須加上分號。 #配置項名 配置項值1 [配置項值2 ....]; #可以注釋掉本行
Nginx配置分為各個配置塊。主配置塊負責全局配置,各個子塊都會繼承全局配置。各個子塊也各有不同的配置項。
main block:主配置(全局配置) event{ ... }事件驅動相關配置塊 http{ ... }http/https 協議相關的配置塊 mail{ ... }郵件服務器相關的配置塊 stream{ ... }流服務器相關的配置塊
主配置按功能分為四類:
正常運行必備的配置
優化性能相關的配置
用于調試及定位問題的相關的配置
事件驅動相關的配置
一、正常運行必備的配置
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進程使用的用戶或者組的憑證,省略組名表示組名與用戶名相同。
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的文件路徑。
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所在目錄)
Syntax: load_module file; Default: — Context: main This directive appeared in version 1.9.11.
Loads a dynamic module.
加載動態模塊。此指令只在ngnix 1.9.11 版本后生效
二、性能優化相關的配置
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核心數量并以此為該項的設定值)。
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;
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。
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進程能打開文件的最大值,可以在不重啟主進程的情況下增加限制。
三、調試、定位問題
Syntax: daemon on | off; Default: daemon on; Context: main
Determines whether nginx should become a daemon. Mainly used during development.
決定nginx是否成為守護進程,主要用于開發期間。
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開發者使用。
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 { ... }
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值。
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默認會使用最有效的方法。
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進程資源將被浪費。
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進程重新接受新連接的超時時間。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。