本文依照nginx官方站點文檔介紹常用的nginx各種常用配置,未經(jīng)過校對,如有錯誤還望海涵。
創(chuàng)新互聯(lián)公司服務(wù)項目包括伊州網(wǎng)站建設(shè)、伊州網(wǎng)站制作、伊州網(wǎng)頁制作以及伊州網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,伊州網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到伊州省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
Nginx最基本的配置語法
配置項名 配置項值1 [配置項值2 ....]; 配置項名位于行首,配置項值與配置項名之間用空格隔開,多個配置項值之間也用空格隔開,每行配置結(jié)尾必須加上分號。 #配置項名 配置項值1 [配置項值2 ....]; #可以注釋掉本行
Nginx配置分為各個配置塊。主配置塊負(fù)責(zé)全局配置,各個子塊都會繼承全局配置。各個子塊也各有不同的配置項。
main block:主配置(全局配置) event{ ... }事件驅(qū)動相關(guān)配置塊 http{ ... }http/https 協(xié)議相關(guān)的配置塊 mail{ ... }郵件服務(wù)器相關(guān)的配置塊 stream{ ... }流服務(wù)器相關(guān)的配置塊
主配置按功能分為四類:
正常運行必備的配置
優(yōu)化性能相關(guān)的配置
用于調(diào)試及定位問題的相關(guān)的配置
事件驅(qū)動相關(guān)的配置
一、正常運行必備的配置
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)程使用的用戶或者組的憑證,省略組名表示組名與用戶名相同。
Syntax: pid file; Default: pid nginx.pid; Context: main
Defines a file that will store the process ID of the main process.
指定存儲nginx matser進(jìn)程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.
加載動態(tài)模塊。此指令只在ngnix 1.9.11 版本后生效
二、性能優(yōu)化相關(guān)的配置
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è)定會直接影響性能,最佳值取決于多種因素包括但不限于CPU核心、存書數(shù)據(jù)的硬盤數(shù)量,加載模式。較好的選擇是設(shè)定該值值等于可用的CPU數(shù)量(auto自動檢測CPU核心數(shù)量并以此為該項的設(shè)定值)。
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)程綁定,每個CPU設(shè)定用位掩碼分別綁定給每一個worker進(jìn)程。默認(rèn)情況下worker進(jìn)程不綁定在任何一個CPU上。(每一位CPUmask代表一個CPU核心)
例如:
主機(jī)有四個核心,建立四個worker進(jìn)程分別綁定在每個CPU上
worker_processes4; worker_cpu_affinity 0001 0010 0100 1000;
主機(jī)有四個核心,建立兩個worker進(jìn)程,第一個進(jìn)程綁定在CPU0/CPU2上,第二個進(jìn)程綁定在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進(jìn)程的優(yōu)先級,相當(dāng)于nice指令:負(fù)數(shù)的優(yōu)先級更高,取值范圍從-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進(jìn)程能打開文件的最大值,可以在不重啟主進(jìn)程的情況下增加限制。
三、調(diào)試、定位問題
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ā)期間。
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ā)者使用。
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.
配置日志,幾個日志可以被指定為同一級別。如果主配置文件級別中配置文件路徑?jīng)]有明確指明,則使用默認(rèn)配置。
第一個字段定義日志存儲文件位置。特殊值stderr選擇標(biāo)準(zhǔn)錯誤文件。針對syslog的文件可以在前面用syslog:
指明。針對cyclic memory buffer可以在前面用memory:
指明,并且要指明緩沖大小,此項指令通常用于調(diào)試。
第二字段判定日志級別,在debug, info, notice, warn, error, crit, alert, emerg之中選擇一項。這些日志級別從左到右依次從輕微到嚴(yán)重。確定日志級別后,會記錄該級別和該級別以上的級別的所有日志。例如:設(shè)定error級別會記錄error, crit, alert, emerg四個基本,如果該條目省略,則默認(rèn)級別為error。
四、事件驅(qū)動相關(guān)配置
事件驅(qū)動相關(guān)的配置配置與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.
設(shè)定worker進(jìn)程同步連接最大值。
這項設(shè)定需要注意,這個數(shù)字包括了所有連接(例如:代理連接服務(wù)器等),不僅僅是客戶端的連接。
另一個值得注意的問題是實際的同步連接數(shù)值要小于之前在 worker_rlimit_nofile中設(shè)定的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.
指明使用的連接進(jìn)程方法。通常不需要明確的指明,因為NGINX默認(rèn)會使用最有效的方法。
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)程在接受新連接時采取輪流進(jìn)行的模式。如果不這么設(shè)定,新連接將不會通知給各worker進(jìn)程。在新連接較少的情況下,部分worker進(jìn)程資源將被浪費。
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)程正在接受新連接時,worker進(jìn)程重新接受新連接的超時時間。
分享名稱:Nginx常用配置詳解(一)——主配置塊篇
瀏覽路徑:http://chinadenli.net/article20/gojejo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、電子商務(wù)、微信小程序、網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)