在前面《Nginx服務器開箱體驗》 一文中咱們從開箱到體驗,感覺了一下Nginx服務器的魅力。Nginx是輕量級的高性能Web服務器,提供了諸如HTTP代理和反向代理、負載均衡、緩存等一系列重要特性,於是在實踐之中使用普遍,筆者也在學習和實踐之中。html
在本文中,咱們繼續延續前文,從前文給出的一份示例配置清單開始,詳解一下Nginx服務器的各類配置指令的做用和用法。前端
注: 本文首發於 My 公衆號 CodeSheep ,可 長按 或 掃描 下面的 當心心 來訂閱 ↓ ↓ ↓
看到了下文中的包含了 「小豬佩琪色」 的配圖了嗎,嘿嘿,咱們開始吧!
從圖中能夠看出主要包含如下幾大部份內容:nginx
該部分配置主要影響Nginx全局,一般包括下面幾個部分:web
該部分配置主要影響Nginx服務器與用戶的網絡鏈接,主要包括:正則表達式
筆者按照文章:《Nginx服務器開箱體驗》 中的實驗,給出了一份簡要的清單配置舉例:編程
配置代碼以下:c#
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進程狀況:
ps -aux | grep nginx
很明顯,理解 worker_processes
這個指令的含義就很容易了
Nginx進程是做爲系統守護進程在運行,須要在某文件中保存當前運行程序的主進程號,Nginx支持該保存文件路徑的自定義
指令格式:pid 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;
cat mime.types
來查看mime.types文件內容,咱們發現其就是一個types結構,裏面包含了各類瀏覽器可以識別的MIME類型以及對應類型的文件後綴名字,以下所示:
指令格式:
access_log path [format];
log_format
定義的格式指令格式:
sendfile on | off; sendfile_max_chunk size;
指令格式:keepalive_timeout timeout [header_timeout];
指令格式:keepalive_requests number;
指令格式:
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前面的方括號中的內容是可選項,解釋以下:
指令格式:root path;
固然,還能夠經過alias指令來更改location接收到的URI請求路徑,指令爲:
alias path; # path爲修改後的根路徑
指令格式:index file ......
做者更多的SpringBt實踐文章在此:
若是有興趣,也能夠抽點時間看看做者一些關於容器化、微服務化方面的文章: