從圖中能夠看出主要包含如下幾大部份內容:html
該部分配置主要影響Nginx全局,一般包括下面幾個部分:前端
該部分配置主要影響Nginx服務器與用戶的網絡鏈接,主要包括:nginx
筆者按照文章:《Nginx服務器開箱體驗》 中的實驗,給出了一份簡要的清單配置舉例:web
配置代碼以下:正則表達式
user nobody nobody; worker_processes 3; error_log logs/error.log; pid logs/nginx.pid; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 8088; server_name codesheep; access_log /codesheep/webserver/server1/log/access.log; error_page 404 /404.html; location /server1/location1 { root /codesheep/webserver; index index.server2-location1.htm; } location /server1/location2 { root /codesheep/webserver; index index.server2-location2.htm; } } server { listen 8089; server_name 192.168.31.177; access_log /codesheep/webserver/server2/log/access.log; error_page 404 /404.html; location /server2/location1 { root /codesheep/webserver; index index.server2-location1.htm; } location /srv2/loc2 { alias /codesheep/webserver/server2/location2/; index index.server2-location2.htm; } location = /404.html { root /codesheep/webserver/; index 404.html; } } }
接下來就來詳細剖析如下配置文件中各個指令的含義⬇️瀏覽器
指令格式:user user [group];
服務器
若是user指令不配置或者配置爲 user nobody nobody
,則默認全部用戶均可以啓動Nginx進程網絡
Nginx服務器實現併發處理服務的關鍵,指令格式:worker_processes number | auto;
併發
按照上文中的配置清單的實驗,咱們給worker_processes配置的數目是:3,啓動Nginx服務器後,咱們能夠後臺看一下主機上的Nginx進程狀況:app
ps -aux | grep nginx
很明顯,理解 worker_processes
這個指令的含義就很容易了
Nginx進程是做爲系統守護進程在運行,須要在某文件中保存當前運行程序的主進程號,Nginx支持該保存文件路徑的自定義
指令格式:pid file;
file:指定存放路徑和文件名稱
若是不指定默認置於路徑 logs/nginx.pid
指定格式:error_log file | stderr;
指令格式:include file;
指令格式:accept_mutex on | off;
說到該指令,首先得闡述一下什麼是所謂的 「驚羣問題」,能夠參考 WIKI百科的解釋。就Nginx的場景來解釋的話大體的意思就是:當一個新網絡鏈接來到時,多個worker進程會被同時喚醒,但僅僅只有一個進程能夠真正得到鏈接並處理之。若是每次喚醒的進程數目過多的話,實際上是會影響一部分性能的。
因此在這裏,若是accept_mutex on,那麼多個worker將是以串行方式來處理,其中有一個worker會被喚醒;反之若accept_mutex off,那麼全部的worker都會被喚醒,不過只有一個worker能獲取新鏈接,其它的worker會從新進入休眠狀態
這個值的開關與否實際上是要和具體場景掛鉤的。
指令格式:multi_accept on | off;
指令格式:use model;
指令格式:worker_connections number;
指令格式:
include mime.types; default_type mime-type;
MIME-Type指的是網絡資源的媒體類型,也即前端請求的資源類型
include指令將mime.types文件包含進來
cat mime.types
來查看mime.types文件內容,咱們發現其就是一個types結構,裏面包含了各類瀏覽器可以識別的MIME類型以及對應類型的文件後綴名字,以下所示:
指令格式:
access_log path [format];
path:自定義服務日誌的路徑 + 名稱
format:可選項,自定義服務日誌的字符串格式。其也可使用 log_format
定義的格式
指令格式:
sendfile on | off;
sendfile_max_chunk size;
指令格式:keepalive_timeout timeout [header_timeout];
timeout 表示server端對鏈接的保持時間,默認75秒
header_timeout 爲可選項,表示在應答報文頭部的 Keep-Alive 域設置超時時間:「Keep-Alive : timeout = header_timeout」
指令格式:keepalive_requests number;
指令格式:
第一種:配置監聽的IP地址:listen IP[:PORT];
第二種:配置監聽的端口:listen PORT;
實際舉例:
listen 192.168.31.177:8080; # 監聽具體IP和具體端口上的鏈接 listen 192.168.31.177; # 監聽IP上全部端口上的鏈接 listen 8080; # 監聽具體端口上的全部IP的鏈接
指令格式:server_name name1 name2 ...
實際舉例:
server_name ~^www\d+\.myserver\.com$
此時表示該虛擬主機能夠接收相似域名 www1.myserver.com 等的請求而拒絕 www.myserver.com 的域名請求,因此說用正則表達式能夠實現更精準的控制
至於基於IP的虛擬主機配置比較簡單,再也不太贅述:
指令格式:server_name IP地址
指令格式爲:location [ = | ~ | ~* | ^~ ] uri {...}
uri前面的方括號中的內容是可選項,解釋以下:
「=」:用於標準uri前,要求請求字符串與uri嚴格匹配,一旦匹配成功則中止
「~」:用於正則uri前,而且區分大小寫
「~*」:用於正則uri前,但不區分大小寫
「^~」:用於標準uri前,要求Nginx找到標識uri和請求字符串匹配度最高的location後,當即使用此location處理請求,而再也不使用location塊中的正則uri和請求字符串作匹配
指令格式:root path;
固然,還能夠經過alias指令來更改location接收到的URI請求路徑,指令爲:
alias path; # path爲修改後的根路徑
指令格式:index file ......