【Nginx筆記】nginx設定檔詳解,nginx設定檔詳解
本文主要對nginx的配置做重點說明,關於nginx的其它基本概念,建議參考官網描述,這裡推薦Nginx Beginner's Guide這篇文檔,對初學者快速認識nginx很有協助。
顯然,發揮nginx強大優勢的前提是熟悉其設定檔並進行合理的配置,而學習nginx配置時,最重要的一點是建立如下概念:
The most important is that nginx is a reverse proxy first and HTTP server second, its first concern is not files but rather URLs, this changes the way we have to configure nginx.
也即,nginx的強大之處在於其高效能的反向 Proxy功能(接收請求並傳遞給下遊真正處理請求的servers;等待下遊響應並返回給請求發起方),其次,它也能像普通webserver那樣提供靜態檔案訪問功能。
我們以nginx源碼編譯安裝後的預設設定檔(conf/nginx.conf.default)為例,對重要的配置項進行說明。
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { 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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
1. Nginx設定檔區塊層級關係
要熟悉nginx設定檔,首先要清楚區塊劃分及區塊間的層級關係,關於這一點,UNDERSTANDING THE NGINX CONFIGURATION INHERITANCE MODEL一文對各區塊層級劃分、層級或配置項類型、配置項的繼承關係及嵌套區塊定義等方面均做了詳細且帶執行個體的說明,這裡不再贅述。
我們需要牢記其中3個區塊間的層級關係:http->server->location,它們是nginx配置過程中的關鍵區塊。
2. Server區塊(Virtual Host)
server block主要目的是建立virtual server,其最重要的設定項有3個:
1) listen
設定偵聽地址、連接埠號碼、UNIX-domain socket的路徑。可以只設定偵聽地址(ip或hostname),這種情況下預設偵聽80連接埠,也可以只指定偵聽連接埠號碼。此外,還可以設定是否是default server, connection是否ssl加密,proxy_protocol,等等。
詳細的設定文法和設定項可以參考這裡的官方文檔。
2) server_name
設定該server block的網域名稱,支援精確網域名稱、帶萬用字元的網域名稱和Regex這3種格式,具體規則見這裡。nginx接收到請求URL後,會根據各server block配置的server_name搜尋處理本次請求的server,第1個match成功(請求URL的header指定的網域名稱與server_name做匹配)的server block負責處理URL請求。從文檔可知,具體的搜尋順序優先順序如下(由高到低):
a. the exact name
b. the longest wildcard name starting with an asterisk, e.g. “*.example.com”
c. the longest wildcard name ending with an asterisk, e.g. “mail.*”
d. the first matching regular expression (in order of appearance in the configuration file)
實際上,配置server_name時,精確網域名稱、由*開頭的通配網域名稱、由*結尾的通配網域名稱被會分別儲存在與listen port關聯的3個hash表中。nginx根據server_name搜尋server block時,會按上述順序依次搜尋這3個hash表,只要有1個hash表的某項配置命中,則搜尋結束,此時,使用者請求URL會交給該成功匹配的server_name所屬的server區塊來處理。若3個hash表均搜尋失敗,則會依次搜尋Regex格式的server_name。由於reg exp match相對最慢,故其搜尋優先順序最低。
因此,在實際使用時,我們應該儘可能配置精確網域名稱。
此外,若需配置很多server名或某個server網域名稱非常長,則應該分別最佳化server_names_hash_max_size和server_names_hash_bucket_size這2個配置項,具體說明可見這篇文檔的Optimization部分。
3) location
詳見下一小節的說明
3. Location區塊
關於location block,需要清楚如下幾個重要規則:
1) 除命名location(named location)區塊外,其餘location區塊均作用在不帶query參數的URL上,且每次只會有1個location區塊起作用。這也是目錄配置通常置於最頂層區塊的原因,因為定義在location /的root目錄設定不能被用於location /images,而若將root目錄設定在server區塊,則所有的location區塊均可使用該設定(當然,在location區塊中可以改寫本區塊的root目錄設定),無論是消除重複代碼還是後續維護方面,均要方便得多。
2) nginx會根據location區塊的server_name來分配請求URL。nginx對URL和location區塊的match規則如下(官網文檔在這裡):
a. 首先尋找由前置詞字元串(prefix string)定義的location區塊,在前置詞字元串匹配成功的所有location區塊中,最長首碼匹配的那個block會由nginx記錄下來;
b. 接著nginx按在設定檔中定義的先後順序檢查由Regex定義的location區塊,當正則匹配成功某個區塊後,尋找結束,該區塊負責處理URL,整個match過程隨之結束;
c. 若沒有由正則指定的location區塊匹配成功,則返回第1步記錄的最長首碼匹配的區塊,整個match過程結束。
幾點特別說明:
a. 由Regex定義的location區塊具有較低的搜尋優先順序,但具有較高的匹配優先順序。這一點與nginx搜尋server_name的規則不同(server_name是按搜尋優先順序匹配,一旦匹配成功就立即結束搜尋),需引起特別注意。
b. location還可以用等號(=)做精確配置,它具有最高的匹配優先順序,只要命中精確匹配的location,nginx對location區塊的搜尋立即結束。
3) 配置的具體文法規則詳見官網文檔,這裡不贅述。
4. 反向 Proxy設定
在熟悉主要區塊(http/server/location)基本配置規則的前提下,配置nginx來實現反向 Proxy伺服器其實很簡單,在location區塊通過配置proxy_pass或fastcgi_pass即可實現。
此外,還可以配置哪個source address的request代理到哪個下遊服務(proxy_bind),是否對下遊回應標頭做buffer及buffer區大小(proxy_buffering/proxy_buffer_size),是否對響應結果做cache(proxy_cache),等等。
詳細的配置文法可分別參考文檔Module ngx_http_proxy_module和Module ngx_http_fastcgi_module。
【參考資料】
1. UNDERSTANDING THE NGINX CONFIGURATION INHERITANCE MODEL
2. NGINX CONFIGURATION PRIMER
3. Nginx Beginner’s Guide
======================= EOF ===================