什么是 nginx?
nginx 最初是作為一個 web 服務器創建的,用于解決 c10k 的問題。作為一個 web 服務器,它可以以驚人的速度為您的數據服務。但 nginx 不僅僅是一個 web 服務器,你還可以將其用作反向代理,與較慢的上游服務器(如:unicorn 或 puma)輕松集成。你可以適當地分配流量(負載均衡器)、流媒體、動態調整圖像大小、緩存內容等等。基本的 nginx 體系結構由 master 進程和其 worker 進程組成。master 讀取配置文件,并維護 worker 進程,而 worker 則會對請求進行實際處理。
基本命令
要啟動 nginx,只需輸入:
[sudo]?nginx
當你的 nginx 實例運行時,你可以通過發送相應的信號來管理它:
[sudo]?nginx?-s?signal
可用的信號:
-
stop – 快速關閉
-
quit – 優雅關閉 (等待 worker 線程完成處理)
-
reload – 重載配置文件
-
reopen – 重新打開日志文件
命令和上下文
nginx 的配置文件,默認的位置包括:
-
/etc/nginx/nginx.conf,
-
/usr/local/etc/nginx/nginx.conf,或
-
/usr/local/nginx/conf/nginx.conf
配置文件的由下面的部分構成:
-
指令 – 可選項,包含名稱和參數,以分號結尾
gzip?on;
-
上下文 – 分塊,你可以聲明指令 – 類似于編程語言中的作用域
worker_processes?2;?#?全局上下文指令http?{??????????????#?http?上下文???gzip?on;????????#?http?上下文中的指令?server?{??????????#?server?上下文???listen?80;??????#?server?上下文中的指令?} }
指令類型
當使用相同的指令在不同的繼承模型中進行操作時,必須小心謹慎。有三種類型的指令,每種都有自己的繼承模型。
普通指令
在每個上下文僅有唯一值。而且,它只能在當前上下文中定義一次。在子級上下文中覆蓋父級的值只在當前子級上下文中有效。
gzip?on; gzip?off;?#?非法,不能在同一個上下文中指定同一普通指令2次server?{ ?location?/downloads?{ ???gzip?off; ?} ?location?/assets?{ ???#?gzip?is?on?here?} }
數組指令
在同一上下文中添加多條指令,將添加多個值,而不是完全覆蓋。在子級上下文中定義指令將覆蓋給父級上下文中的值。
error_log?/var/log/nginx/error.log; error_log?/var/log/nginx/error_notive.log?notice; error_log?/var/log/nginx/error_debug.log?debug; server?{ ?location?/downloads?{ ???#?下面的配置會覆蓋父級上下文中的指令???error_log?/var/log/nginx/error_downloads.log; ?} }
行動指令
行動是改變事情的指令。根據模塊的需要,它繼承的行為可能會有所不同。例如 rewrite 指令,只要是匹配的都會執行:
server?{ ?rewrite?^?/foobar; ?location?/foobar?{ ???rewrite?^?/foo; ???rewrite?^?/bar; ?} }
如果用戶想嘗試獲取 /sample:
-
server的rewrite將會執行,從 /sample rewrite 到 /foobar
-
location /foobar 會被匹配
-
location的第一個rewrite執行,從/foobar rewrite到/foo
-
location的第二個rewrite執行,從/foo rewrite到/bar
return 指令提供的是不同的行為:
server?{ ?location?/?{ ???return?200; ???return?404; ?} }
在上述的情況下,立即返回200。
處理請求
在 Nginx 內部,你可以指定多個虛擬服務器,每個虛擬服務器用 server{} 上下文描述。
server?{ ?listen??????*:80?default_server; ?server_name?netguru.co; ?return?200?"Hello?from?netguru.co"; } server?{ ?listen??????*:80; ?server_name?foo.co; ?return?200?"Hello?from?foo.co"; } server?{ ?listen??????*:81; ?server_name?bar.co; ?return?200?"Hello?from?bar.co"; }
這將告訴 Nginx 如何處理到來的請求。在檢查給定的 IP 端口組合時,Nginx 會先測試哪個虛擬主機有設置 listen 指令。
然后,server_name 指令的值將檢測 Host 頭(存儲著主機域名)。
Nginx 將會按照下列順序選擇虛擬主機:
-
匹配sever_name指令的IP-端口主機
-
擁有default_server標記的IP-端口主機
-
首先定義的IP-端口主機
-
如果沒有匹配,拒絕連接。
例如下面的例子:
Request?to?foo.co:80?????=>?"Hello?from?foo.co"Request?to?www.foo.co:80?=>?"Hello?from?netguru.co"Request?to?bar.co:80?????=>?"Hello?from?netguru.co"Request?to?bar.co:81?????=>?"Hello?from?bar.co"Request?to?foo.co:81?????=>?"Hello?from?bar.co"
server_name 指令
server_name指令接受多個值。它還處理通配符匹配和正則表達式。
server_name?netguru.co?www.netguru.co;?#?exact?matchserver_name?*.netguru.co;??????????????#?wildcard?matchingserver_name?netguru.*;?????????????????#?wildcard?matchingserver_name??~^[0-9]*.netguru.co$;???#?regexp?matching
當有歧義時,nginx 將使用下面的命令:
-
確切的名字
-
最長的通配符名稱以星號開始,例如“* .example.org”。
-
最長的通配符名稱以星號結尾,例如“mail.**”
-
首先匹配正則表達式(按照配置文件中的順序)
Nginx將存儲三個哈希表,用于存儲具體名稱、以星號開頭的通配符和以星號結尾的通配符。如果結果不在任何表中,則將按順序進行正則表達式測試。
值得謹記的是
server_name?.netguru.co;
是一個來自下面的縮寫
server_name??netguru.co??www.netguru.co??*.netguru.co;
有一點不同,.netguru.co 存儲在第二張表,這意味著它比顯式聲明的慢一點。
listen 指令
在很多情況下,能夠找到 listen 指令,接受IP:端口值
listen?127.0.0.1:80; listen?127.0.0.1;????#?by?default?port?:80?is?usedlisten?*:81; listen?81;???????????#?by?default?all?ips?are?usedlisten?[::]:80;??????#?IPv6?addresseslisten?[::1];????????#?IPv6?addresses
然而,還可以指定 UNIX-domain 套接字。
listen?unix:/var/run/nginx.sock;
你甚至可以使用主機名
listen?localhost:80; listen?netguru.co:80;
但請慎用,由于主機可能無法啟動 nginx,導致無法綁定在特定的 TCP Socket。
最后,如果指令不存在,則使用 *:80。
最小化配置
有了這些知識 – 我們應該能夠創建并理解運行 nginx 所需的最低配置。
#?/etc/nginx/nginx.confevents?{}???????????????????#?events?context?needs?to?be?defined?to?consider?config?validhttp?{ server?{ ???listen?80; ???server_name??netguru.co??www.netguru.co??*.netguru.co; ???return?200?"Hello"; ?} }
root, location, 和 try_files 指令
root 指令
root 指令設置請求的根目錄,允許 nginx 將傳入請求映射到文件系統。
server?{ ?listen?80; ?server_name?netguru.co; ?root?/var/www/netguru.co; }
根據給定的請求,指定 nginx 服務器允許的內容
netguru.co:80/index.html?????#?returns?/var/www/netguru.co/index.htmlnetguru.co:80/foo/index.html?#?returns?/var/www/netguru.co/foo/index.html
location 指令
location指令根據請求的 URI 來設置配置。location [modifier] path
location?/foo/?{ ?#?...}
如果沒有指定修飾符,則路徑被視為前綴,其后可以跟隨任何東西。
以上例子將匹配
/foo /fooo /foo123 /foo/bar/index.html ...
此外,在給定的上下文中可以使用多個 location 指令。
server?{ ?listen?80; ?server_name?netguru.co; ?root?/var/www/netguru.co; ?location?/?{ ???return?200?"root"; ?} ?location?/foo/?{ ???return?200?"foo"; ?} } netguru.co:80???/???????#?=>?"root"netguru.co:80???/foo????#?=>?"foo"netguru.co:80???/foo123?#?=>?"foo"netguru.co:80???/bar????#?=>?"root"
Nginx還有一些修飾符可以用于連接location。因為每個修飾符都有自己的優先級,所以它們會影響 location 模塊在使用時的行為。
=???????????-?Exact?match ^~??????????-?Preferential?match ~?&&?~*?????-?Regex?match no?modifier?-?Prefix?match
Nginx 會先檢查精確匹配。如果找不到,我們會找優先級最高的。如果之前的匹配嘗試失敗,正則表達式會按照出現的順序逐個進行測試。至少,最后一個前綴匹配將被使用。
location?/match?{ ?return?200?'Prefix?match:?matches?everything?that?starting?with?/match'; } location?~*?/match[0-9]?{ ?return?200?'Case?insensitive?regex?match'; } location?~?/MATCH[0-9]?{ ?return?200?'Case?sensitive?regex?match'; } location?^~?/match0?{ ?return?200?'Preferential?match'; } location?=?/match?{ ?return?200?'Exact?match'; } /match/????#?=>?'Exact?match'/match0????#?=>?'Preferential?match'/match2????#?=>?'Case?insensitive?regex?match'/MATCH1????#?=>?'Case?sensitive?regex?match'/match-abc?#?=>?'Prefix?match:?matches?everything?that?starting?with?/match'
try_files 指令
嘗試不同的路徑,找到一個路徑就返回。
try_files?$uri?index.html?=404;
所以對于 /foo.html 請求,它將嘗試按以下順序返回文件:
-
$uri ( /foo.html )
-
index.html
-
如果什么都沒找到則返回 404
有趣的是,如果我們在服務器上下文中定義 try_files,然后定義匹配的所有請求的 location —— try_files 將不會執行。
這是因為在服務器上下文中定義的 try_files 是它的 pseudo-location,這是最不可能的位置。因此,location/的定義將比pseudo-location更為明確。
server?{ ?try_files?$uri?/index.html?=404; ?location?/?{ ?} }
因此,你應該避免在 server 上下文中出現 try_files:
server?{ ?location?/?{ ???try_files?$uri?/index.html?=404; ?} }