能夠說nginx
的使用基本體如今了配置文件的指令上,而每一個模塊中的指令又對應了不少的不一樣的功能,因此本文旨在瞭解了以前的nginx的初始nginx的基本思想和功能的前提下,進一步的瞭解一些比較經常使用的模塊和其中的指令;php
主要參考nginx的官方文檔,nginx的指令集合字母排序,nginx的變量字母排序html
主模塊的指令主要包括如下這些,基本都是簡單指令,不過每一個指令的執行環境context是不同的,有的能夠在http、location、server等中均可以使用,主模塊指令如下是主模塊中的指令列表:linux
1.2 daemon 1.3 debug_points 1.4 error_log 1.5 include 1.6 master_process 1.7 pid 1.8 ssl_engine 1.9 timer_resolution 1.10 user 1.11 worker_cpu_affinity 1.12 worker_priority 1.13 worker_processes 1.14 worker_rlimit_core 1.15 worker_rlimit_nofile 1.16 worker_rlimit_sigpending 1.17 working_directory
daemon指令,守護進程QA,生產環境中不要使用"daemon"和"master_process"指令,這些選項僅用於開發調試[主要方便開發過程當中的進程清除]。nginx
語法: daemon on | off
缺省值: on
使用環境context: main主模塊
debug_points指令,主要也是在開發過程來調試nginx正則表達式
語法: debug_points [stop | abort]
缺省值: none
error_log 指令用於記錄nginx的某些級別的異常錯誤日誌express
語法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/error.log
include 指令,主要是來引入其餘配置文件,避免單一配置文件過大和複雜apache
語法: include file | *
缺省值: none
你能夠在任意地方使用include指令實現配置文件的包含,相似於apache中的include方法,可減小主配置文件的篇幅。include 指令還支持像下面配置同樣的全局包含的方法,例如包含一個目錄下全部以".conf"結尾的文件:segmentfault
include vhosts/*.conf;
pid 指令,進程id存儲文件緩存
語法: pid file
缺省值: compile-time option Example:
pid /var/log/nginx.pid;
user
這個指令名錶明的是執行worker processes
的本機用戶,默認是nobody
,那麼若是須要讀寫一些roort
或者其餘用戶全部權的文件時,若是當前配置文件填寫的user
這個指令名對應的用戶又不具備r+w+x
的權限時,就會出現一些權限問題;服務器
語法: user user [group]
缺省值: nobody nobody
指定Nginx Worker進程運行用戶,默認是nobody賬號。
worker_processes
這個指令名是指配置worker_processes
的數量,nginx
啓動時會有一個主進程和若干的工做進程,這個指令就是來規定工做進程的數量的,對應的是一個數值,
nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.
使用: worker_processes number | auto;
默認:worker_processes 1;
使用環境context: main 主模塊
若是nginx處理的是cpu密集型(比較耗費cpu的)的操做,建議將此值設置爲cpu個數或cpu的核數。
一個cpu配置多於一個worker數,對nginx而言沒有任何益處。
須要注意的是,經過event
模塊中的worker_connections
和主模塊的worker_proceses
能夠計算出maxclients
,也就同時最大鏈接數:
max_clients = worker_processes * worker_connections
worker_rlimit_nofile指令,對於上述的的最大鏈接數有限制做用
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.
這其中使用比較多的就是include
指令。
事件模塊主要是設置Nginx處理鏈接請求;事件模塊指令指令列表以下:
2.1 accept_mutex 2.2 accept_mutex_delay 2.3 debug_connection 2.4 devpoll_changes 2.5 devpoll_events 2.6 kqueue_changes 2.7 kqueue_events 2.8 epoll_events 2.9 multi_accept 2.10 rtsig_signo 2.11 rtsig_overflow_events 2.12 rtsig_overflow_test 2.13 rtsig_overflow_threshold 2.14 use 2.15 worker_connections
accept_mutex指令,nginx 使用鏈接互斥鎖進行順序的accept()系統調用.
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.
這個指令之前默認是開的,Prior to version 1.11.3, the default value was on.
worker_connections
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.
其餘的之類須要參考官方文檔來看: 主模塊和事件模塊的簡單指令詳細介紹:http://nginx.org/en/docs/ngx_core_module.html
http相關模塊能夠看做是nginx的核心模塊,是其功能的最核心部分,因此http相關的模塊有將近幾十個,其中使用的最多的是http核心模塊;
http模塊的主要組成部分是server和一些公用提取出來指令【能夠在http、server、server甚至是if內使用的指令,在server塊級指令集中設置的話須要書寫屢次,因此能夠做爲公共提取到http塊指令中】,而server的重要部分是location、listen、server_name等;
大部分配置也能夠寫在server、http塊級中,可是爲了不重複書寫,能夠考慮提取到外層;部分指令以下:
types 響應內容的文件類型,Maps file name extensions to MIME types of responses;MIME (Multipurpose Internet Mail Extensions) 是描述消息內容類型的因特網標準。MIME 消息能包含文本、圖像、音頻、視頻以及其餘應用程序專用的數據。
Syntax: types { ... }
Default: types {
text/html html;
image/gif gif;
image/jpeg jpg;
}
Context: http, server, location
一個完整的映射表在conf/mime.types文件中,因此默認的寫法是include mime.types;
;默認類型還可使用:To make a particular location emit the 「application/octet-stream」 MIME type for all requests, the following configuration can be used:
location /download/ { types { } default_type application/octet-stream; }
keepalive_timeout 客戶端keeplive的連接無任何操做的保持最久時間
Syntax: keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the 「Keep-Alive: timeout=time」 response header field. Two parameters may differ.
error_page 爲一些特定的錯誤返回定義好的一些URI路徑, 詳細介紹error_page
Syntax: error_page code ... [=[response]] uri;
Default: —
Context: http, server, location, if in location
如下example:
error_page 404 /404.html; error_page 500 502 503 504 /50x.html; error_page 404 =200 /empty.gif; error_page 404 = /404.php; location / { error_page 404 = @fallback; } location @fallback { proxy_pass http://backend; } error_page 403 http://example.com/forbidden.html; error_page 404 =301 http://example.com/notfound.html;
listen 詳細listen,用於監聽服務器的某個端口的全部請求;
Syntax:listen
address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen
port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen
unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default: listen :80 | :8000;
Context: server
一些例子:
listen 127.0.0.1:8000; listen 127.0.0.1; listen 8000; listen *:8000; listen localhost:8000; //IPv6 addresses (0.7.36) are specified in square brackets: listen [::]:8000; listen [::1];
若是沒有這個listen
指令,那麼須要根據啓動用戶是不是超級管理員用戶,是的話將會啓動監聽80端口,不然使用8000端口;目前已經支持hhtp2 https
的方式。
server_name server_name詳情爲每一個服務設置上名稱,在http指令中能夠設置多個server,多個server能夠監聽同一個端口,那麼端口接收到的請求就須要根據名字匹配來進行相應的服務中進行處理,若是都沒有匹配上的話,就是第一個derver指令來處理。
Syntax: server_name name ...;
Default: server_name "";
Context: server
可使用全匹配、通配符、正則匹配,例子以下:
server { server_name example.com www.example.com; } //The first name becomes the primary server name. //Server names can include an asterisk (「*」) replacing the first or last part of a name: server { server_name example.com *.example.com www.example.*; } //Such names are called wildcard names. //The first two of the names mentioned above can be combined in one: server { server_name .example.com; } It is also possible to use regular expressions in server names, preceding the name with a tilde (「~」): server { server_name www.example.com ~^www\d+\.example\.com$; } Regular expressions can contain captures (0.7.40) that can later be used in other directives: server { server_name ~^(www\.)?(.+)$; location / { root /sites/$2; } } server { server_name _; location / { root /sites/default; } } Named captures in regular expressions create variables (0.8.25) that can later be used in other directives: server { server_name ~^(www\.)?(?<domain>.+)$; location / { root /sites/$domain; } } server { server_name _; location / { root /sites/default; } }
server_name_in_redirect 在由nginx發佈的絕對重定向中,啓用或禁用使用由server_name指令指定的主服務器名稱
Syntax: server_name_in_redirect on | off;
Default: server_name_in_redirect off;
Context: http, server, location
在開啓的時候,若是須要重定向,就可使用已經開啓過的server_name
來書寫,快速的定位;
server_name cwj.cc; server_name_in_redirect on; #charset koi8-r; #access_log logs/host.access.log main; location /test { proxy_pass http://cwj.cc:8080; //此服務名開啓了重定向,能夠直接使用 }
port_in_redirect 和上面的指令差很少,只不過換成了端口號,默認開啓
Syntax: port_in_redirect on | off;
Default: port_in_redirect on;
Context: http, server, location
location 將解析後的URI請求進行匹配,匹配到後根據配置完成相應的處理和轉發,該指令還能夠嵌套使用;
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
可使用= 、^~、 ~|~*、@
四種方法來配置;匹配順序以下序列號:
= 使用等號匹配符代表兩點:
例子以下:
location = /test{ return 'hello_world no1'; } // 須要徹底匹配,只有 127.0.0.1:port/test 能夠正確匹配 ;連127.0.0.1:port/test/也沒法匹配上
空字符徹底匹配,空字符匹配既能夠徹底匹配也能夠匹配URI的前半部分【prefix string】;區別是:只有徹底匹配的時候,匹配優先級纔會高於正則匹配,而prefix string的
例子以下:
location /test/test.jpg{ return '/test/test.jpg'; } // 須要徹底匹配優先級才高,只有 127.0.0.1:port//test/test.jpg 能夠算徹底匹配 ; //連127.0.0.1:port/test.jgp.test 也能夠匹配上,只是不算徹底匹配
^~ 正則前綴匹配,表示匹配以正則匹配的內容開頭的URI,
例子以下:
location ^~ /test{ return 'hello_world no3'; } // 匹配 127.0.0.1:port/test + 其餘後綴路徑等;
~|~*
例子以下:
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ { alias /data/w3/images/$1; } // 須要與正則表達式匹配,127.0.0.1:port/users/test.jpg 能夠正確匹配 ;還能夠在後續來使用這個匹配上的可捕獲的子表達式
空字符非精確
例子以下:
location = /test{ return 'hello_world no5'; } // 能夠徹底和部分匹配,只有 127.0.0.1:port/test 纔算是全匹配 ;127.0.0.1:port/test/也能夠匹配上,就是非徹底匹配,優先級最低
還有不少其餘的,此處不一一例舉。
alias 替換URI訪問的具體路徑
Syntax: alias path;
Default: —
Context: location
僅在location中使用,用來替換訪問路徑。下面的例子就是一個路徑的映射關係
location /i/ { alias /data/w3/images/; } //on request of 「/i/top.gif」, the file /data/w3/images/top.gif will be sent.
root 設置請求的根目錄
Syntax: root path;
Default: root html;
Context: http, server, location, if in location
使用後全部的請求的資源都將映射到設置的根目錄下面:
location /i/ { root /data/w3; } //The /data/w3/i/top.gif file will be sent in response to the 「/i/top.gif」 request.
etag 爲請求設置etag響應頭,和緩存先關的設置
Syntax: etag on | off;
Default: etag on;
Context: http, server, location
This directive appeared in version 1.3.3.
Enables or disables automatic generation of the 「ETag」 response header field for static resources.
if_modified_since 替換URI訪問的具體路徑
Syntax: if_modified_since off | exact | before;
Default: if_modified_since exact;
Context: http, server, location
This directive appeared in version 0.7.24.
還有不少其餘的,此處不一一例舉。
爲了不篇幅過長,部份內容移到下一篇文章中