基于IP地址的訪問控制模塊
ngx_http_access_module模塊可以限制某些客戶端IP地址的訪問,針對某些ip的客戶端訪問進行允許或者禁止。
語法:
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
示例:
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
在上面的例子中,僅允許網段 10.1.1.0/16 和 192.168.1.0/24中除 192.168.1.1之外的ip訪問.匹配規則被依次進行檢驗直到第一個匹配的規則為止,后續匹配規則無效。建議把同類型的規則范圍小的放前面,范圍大的放后面,不同類型的訪問頻繁的放在上面。
基于用戶的訪問控制模塊
語法:
auth_basic string | off; (Default: auth_basic off;) //指定用戶登錄提示語,默認關閉
auth_basic_user_file file; (Default: —) //指定驗證文件路徑
Context: http, server, location, limit_except
示例:
location / {
auth_basic "closed site"; //添加用戶登錄提示語"close site"
auth_basic_user_file /etc/conf/htpasswd; //指定驗證文件為/etc/conf/htpasswd
}
輸出nginx的基本狀態模塊
ngx_http_stub_status_module模塊提供對基本狀態信息的訪問。
語法:
Syntax: stub_status;
Default: —
Context: server, location
示例:
location /basic_status {
stub_status;
}
結果:
This configuration creates a simple web page with basic status data which may look like as follows:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections: 活動狀態的連接數;
accepts:已經接受的客戶端請求的總數;
handled:已經處理完成的客戶端請求的總數;
requests:客戶端發來的總的請求數;
Reading:處于讀取客戶端請求報文首部的連接的連接數;
Writing:處于向客戶端發送響應報文過程中的連接數;
Waiting:處于等待客戶端發出請求的空閑連接數;
現在使用nginx服務器地址加上basic_status就可以查看nginx的基本狀態信息。
nginx記錄日志模塊
ngx_http_log_module模塊中指定的格式寫入請求的日志。
語法:
log_format //日志格式
Syntax: log_format name [escape=default|json] string ...;
Default:
log_format combined "...";
Context: http
string可以使用nginx核心模塊及其它模塊內嵌的變量;
access_log //訪問日志
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
open_log_file_cache //日志緩存
Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Default: open_log_file_cache off; //默認不緩存,禁用
Context: http, server, location
緩存各日志文件相關的元數據信息;
max:緩存的最大文件描述符數量;
min_uses:在inactive指定的時長內訪問大于等于此值方可被當作活動項;
inactive:非活動時長;
valid:驗正緩存中各緩存項是否為活動項的時間間隔;
off:禁用;
nginx壓縮模塊
該ngx_http_gzip_module模塊是使用“gzip”方法壓縮響應的過濾器。這通常有助于將傳輸的數據的大小減少一半甚至更多。
gzip on | off; //gzip開關(on為啟用,off為禁用)
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location
gzip comp_level (level); //指定gzip的壓縮等級(低1-9高,壓縮等級越高,壓縮率越高)
Syntax: gzip_comp_level level;
Default:
gzip_comp_level 1;
Context: http, server, location
gzip_buffers number size; //支持實現壓縮功能時為其配置的緩沖區數量及每個緩存區的大小;
Syntax: gzip_buffers number size;
Default:
gzip_buffers 32 4k|16 8k;
Context: http, server, location
gzip_disable regex ...; //禁止使用與任何指定的正則表達式匹配的“User-Agent”頭字段的請求的響應。
Syntax: gzip_disable regex ...;
Default: —
Context: http, server, location
gzip_min_length; //啟用壓縮功能的響應報文大小閾值;
Syntax: gzip_min_length length;
Default:
gzip_min_length 20;
Context: http, server, location
gzip_proxied; //nginx作為代理服務器接收到從被代理服務器發送的響應報文后,在何種條件下啟用壓縮功能的;
Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
Default:
gzip_proxied off;
Context: http, server, location
選項參數:
off:對代理的請求不啟用壓縮
no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值為此三者中任何一個,則啟用壓縮功能;
no_last_modified:如果響應頭不包括“Last-Modified”字段,則啟用壓縮;
no_etag:如果響應頭不包括“ETag”字段,則啟用壓縮;
auth:如果請求頭包含“授權”字段,則啟用壓縮;
any:啟用所有代理請求的壓縮。
gzip_types mime-type ...; //壓縮過濾器,僅對此處設定的MIME類型的內容啟用壓縮功能;
Syntax: gzip_types mime-type ...;
Default:
gzip_types text/html;
Context: http, server, location
Nginx-SSL模塊
語法:
ssl on | off; //啟用或者禁用虛擬主機的ssl功能。
Syntax: ssl on | off;
Default:
ssl off;
Context: http, server
ssl_certificate file; //指定當前虛擬主機使用PEM格式的證書文件;
ssl_certificate_key file; //指定當前虛擬主機上與其證書匹配的私鑰文件;
Syntax: ssl_certificate file;
Default: —
Context: http, server
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; //支持ssl協議版本
Syntax: ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];
Default: ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Context: http, server
ssl_session_cache off | none | [builtin[:size]][shared:name:size];//ssl會話緩存
Syntax: ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
Default: ssl_session_cache none;
Context: http, server
builtin[:size]:使用OpenSSL內建的緩存,此緩存為每worker進程私有;
[shared:name:size]:在各worker之間使用一個共享的緩存;
ssl_session_timeout time; //客戶端一側的連接可以復用ssl session cache中緩存的ssl參數的有效時長;
Syntax: ssl_session_timeout time;
Default: ssl_session_timeout 5m;
Context: http, server
示例:
server {
listen 443 ssl;
server_name www.magedu.com;
root /vhosts/ssl/htdocs;
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:sslcache:20m;
}
nginx-uri重寫模塊
將用戶請求的URI基于regex所描述的模式進行檢查,而后完成替換;
1、rewrite //將用戶請求的URI基于regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI;
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自下而下逐個檢查;被某條件規則替換完成后,會重新一輪的替換檢查,因此,隱含有循環機制;[flag]所表示的標志位用于控制此循環機制;
如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給客戶端;
301:永久重定向;
[flag]:
last:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環;
break:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它配置;結束循環;
redirect:重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;不能以http://或https://開頭;
permanent:重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;
2、return //停止處理并返回指定code給客戶端。非標準代碼444則關閉連接而不發送響應頭。
Syntax: return code [text]; return code URL; return URL;
Default: —
Context: server, location, if
3、 rewrite_log on | off; //是否開啟重寫日志,默認關閉;
4、 if (condition) { ... } //引入一個新的配置上下文 ;條件滿足時,執行配置塊中的配置指令;server, location;
Syntax: if (condition) { ... }
Default: —
Context: server, location
condition:
比較操作符
==: 等于
!=: 不等于
~: 模式匹配,區分字符大小寫;
~*: 模式匹配,不區分字符大小寫;
!~: 模式不匹配,區分字符大小寫;
!~*: 模式不匹配,不區分字符大小寫;
文件及目錄存在性判斷
用“ -f”和“ !-f”操作符檢查文件存在;
使用“ -d”和“ !-d”操作符檢查目錄存在;
使用“ -e”和“ !-e”操作符檢查文件,目錄或符號鏈接存在;
使用“ -x”和“ !-x”運算符檢查可執行文件。
5、set $variable value; //用戶自定義變量;
Nginx引用控制模塊
該ngx_http_referer_module模塊用于在“引用者”標題字段中阻止對具有無效值的請求的站點訪問。
valid_referers none //定義referer首部的合法可用值
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
none:請求報文首部沒有referer首部;
blocked:請求報文的referer首部沒有值;
server_names:參數,其可以有值作為主機名或主機名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭,例如 ~.*.magedu.com;
配置示例:
valid_referers none blocked server_names
*.example.com example.* www.example.org/galleries/
~\.google\.;
if ($invalid_referer) {
return 403;
}
Nginx-http-proxy模塊
ngx_http_proxy_module模塊允許將請求傳遞給另一個服務器。
1、proxy_pass
設置代理服務器的協議和地址以及應映射位置的可選URI。作為協議,可以指定“ http”或“ https”。地址可以指定為域名或IP地址,也可以指定為可選端口。
注意:proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端主機;
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
proxy_pass http:// localhost:8000 / uri /;
server {
...
server_name HOSTNAME;
location /uri/ {
proxy http://hos[:port];
}
...
}
http://HOSTNAME/uri --> http://host/uri
proxy_pass后面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri;
server {
...
server_name HOSTNAME;
location /uri/ {
proxy http://host/new_uri/;
}
...
}
http://HOSTNAME/uri/ --> http://host/new_uri/
如果location定義其uri時使用了正則表達式的模式,或在if語句或limt_execept中使用proxy_pass指令,則proxy_pass之后必須不能使用uri; 用戶請求時傳遞的uri將直接附加代理到的服務的之后;
server {
...
server_name HOSTNAME;
location ~|~* /uri/ {
proxy http://host;
}
...
}
http://HOSTNAME/uri/ --> http://host/uri/;
2、proxy_set_header field value; //設定發往后端主機的請求報文的請求首部的值;
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close;
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3、proxy_cache_path //定義可用于proxy功能的緩存;
Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default: —
Context: http
4、proxy_cache zone | off; //指明要調用的緩存,或關閉緩存機制;
Syntax: proxy_cache zone | off;
Default:
proxy_cache off;
Context: http, server, location
5、 proxy_cache_key string; //緩存中用于“鍵”的內容;
Syntax: proxy_cache_key string;
Default:
proxy_cache_key $scheme$proxy_host$request_uri;
Context: http, server, location
6、proxy_cache_valid [code ...] time; //定義對特定響應碼的響應內容的緩存時長;
定義在http{...}中;
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
定義在需要調用緩存功能的配置段,例如server{...};
proxy_cache pxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
7、proxy_cache_use_stale
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...;
Default:
proxy_cache_use_stale off;
Context: http, server, location
確定在與代理的服務器進行通信時,哪些情況下可能會使用過時緩存的響應。指令的參數與proxy_next_upstream指令的參數相匹配 。
error如果無法選擇處理請求的代理服務器,則 該參數還允許使用陳舊的緩存響應。
此外,updating如果當前正在更新,該參數允許使用陳舊的緩存響應。這樣可以最大限度地減少更新緩存數據時對代理服務器的訪問次數。
8、proxy_cache_methods GET | HEAD | POST ...; //緩存指定的請求方法
Syntax: proxy_cache_methods GET | HEAD | POST ...;
Default:
proxy_cache_methods GET HEAD;
Context: http, server, location
如果客戶端請求方法在此指令中列出,則響應將被緩存。“ GET”和“ HEAD”方法總是添加到列表中,盡管建議明確指定它們。另請參閱proxy_no_cache指令。
9、proxy_hide_header field;
默認情況下,nginx不會將代理服務器的響應中的頭字段“Date”,“Server”,“X-Pad”和“X-Accel -...”傳遞給客戶端。該proxy_hide_header指令設置不會傳遞的其他字段。如果恰恰相反,需要允許字段的傳遞,那么可以使用proxy_pass_header指令。
10、proxy_connect_timeout time; //定義與代理服務器建立連接的超時時間
Syntax: proxy_connect_timeout time;
Default:
proxy_connect_timeout 60s;
Context: http, server, location
定義與代理服務器建立連接的超時時間。應該注意的是,這個超時通常不能超過75秒。
11、proxy_read_timeout time; //定義從代理服務器讀取響應的超時
Syntax: proxy_read_timeout time;
Default:
proxy_read_timeout 60s;
Context: http, server, location
定義從代理服務器讀取響應的超時。超時僅在兩個連續讀操作之間設置,而不是傳輸整個響應。如果代理的服務器在此時間內沒有傳輸任何內容,則連接被關閉。
12、proxy_send_timeout time; //設置代理服務器發送請求的超時時間。
Syntax: proxy_send_timeout time;
Default:
proxy_send_timeout 60s;
Context: http, server, location
設置將代理服務器發送請求的超時。超時僅在兩個連續的寫入操作之間設置,而不是傳輸整個請求。如果代理服務器在此時間內沒有收到任何內容,則連接將關閉。
Nginx-http頭部信息模塊
ngx_http_headers_module模塊允許向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值;
1、add_header name value [always]; //添加自定義首部;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
2、expires [modified] time; //用于定義Expire或Cache-Control首部的值;
Syntax: expires [modified] time;
expires epoch | max | off;
Default:
expires off;
Context: http, server, location, if in location
只要響應代碼等于200,201,204,206,301,302,303,304,307或308,則啟用或禁用添加或修改“過期”和“緩存控制”響應頭字段。參數可以是積極或消極的時間。
“Expires”字段中的時間計算為當前時間和time指令中指定的總和。如果使用modified參數(0.7.0,0.6.32),則將該時間計算為文件修改時間與time指令中指定的時間之和。
另外,可以使用“ @”前綴指定一天的時間:
expires@ 15h30m;
該epoch參數對應于絕對時間“ Thu, 01 Jan 1970 00:00:01 GMT”。“緩存控制”字段的內容取決于指定時間的符號:
時間為負 - “緩存控制:無緩存”。
時間為正或零 - “Cache-Control:max-age = t”,其中t是指令指定的時間,以秒為單位。
該max參數設置“過期”值“ Thu, 31 Dec 2037 23:55:55 GMT”,和“緩存控制”到10年。
該off參數禁止添加或修改“Expires”和“Cache-Control”響應頭域。
ngx_http_fastcgi_module模塊
允許對fastcgi服務器的請求通過
The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.
1、fastcgi_pass address; //指定fastcgi的服務器地址,地址可以是IP、域名和端口
Context:location, if in location;
2、fastcgi_index name; //fastcgi默認的主頁資源;
3、fastcgi_param parameter value [if_not_empty]; //設置一個參數,該參數應該傳遞給FastCGI服務器。該值可以包含文本、變量和它們的組合,參數值應該非空。
通過/pm_status和/ping來獲取fpm server狀態信息;
location ~* ^/(pm_status|ping)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
4、fastcgi_cache_path //定義fastcgi的緩存;緩存位置為磁盤上的文件系統,由path所指定路徑來定義;
Syntax: fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default: —
Context: http
設置緩存的路徑和其他參數。緩存數據存儲在文件中。緩存中的密鑰和文件名都是將MD5功能應用于代理URL的結果。該levels參數定義緩存的層次級別:從1到3,每個級別接受值1或2.例如,在以下配置中
fastcgi_cache_path / data / nginx / cache levels = 1:2 keys_zone = one:10m;
緩存中的文件名將如下所示:
/ data / nginx / cache / c / 29 / b7f54b2df7773722d382f4809d650 29c
緩存的響應首先寫入臨時文件,然后重新命名該文件。
(1)levels=levels:緩存目錄的層級數量,以及每一級的目錄數量;
leves=1:2:2(levels=一級:二級:三級) //一級1個字符,二級2個字符,三級2個字符
(2)keys_zone=name:size // k/v映射的內存空間的名稱及大小
(3)inactive=time //非活動時長
(4)max_size=size //磁盤上用于緩存數據的緩存空間上限
5、fastcgi_cache zone | off; //調用指定的緩存空間來緩存數據;http, server, location
6、fastcgi_cache_key string; //定義用作緩存項的key的字符串;
7、fastcgi_cache_methods GET | HEAD | POST ...; //為哪些請求方法使用緩存;
8、fastcgi_cache_min_uses number; //緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項;
9、fastcgi_cache_valid [code ...] time; //不同的響應碼各自的緩存時長;
示例:
http {
...
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
...
server {
...
location ~* \.php$ {
...
fastcgi_cache fcgi;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
...
}
...
}
...
}
10、fastcgi_keep_conn on | off;
Syntax: fastcgi_keep_conn on | off;
Default:
fastcgi_keep_conn off;
Context: http, server, location
默認情況下,FastCGI服務器將在發送響應后立即關閉連接。但是,當該指令設置為該值時on,nginx將指示FastCGI服務器保持連接處于打開狀態。這是必要的,特別是與FastCGI服務器的Keepalive連接功能。
ngx_http_upstream_module模塊
該ngx_http_upstream_module模塊用于定義可由proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass和 memcached_pa??ss指令引用的服務器組,默認調度方式為wrr。
(1) upstream name { ... } //定義后端服務器組;引入一個新的上下文;只能用于http{}上下文中;
(2) server address [parameters];
定義服務器地址和相關的參數;
地址格式:IP[:PORT]、HOSTNAME[:PORT]、unix:/PATH/TO/SOME_SOCK_FILE
參數:
weight=number //權重,默認為1;
max_fails=number //失敗嘗試的最大次數;
fail_timeout=time //設置服務器為不可用狀態的超時時長;
backup //把服務器標記為“備用”狀態;
down //手動標記其為不可用;
(3) least_conn; //最少連接調度算法; 當server擁有不同的權重時為wlc;當所有后端主機的連接數相同時,則使用wrr進行調度;
(4) ip_hash; //源地址hash算法;能夠將來自同一個源IP地址的請求始終發往同一個upstream server;
(5) hash key [consistent]; //基于指定的key的hash表實現請求調度,此處的key可以文本、變量或二者的組合;
consistent:參數,指定使用一致性hash算法;
示例:
hash $request_uri consistent //對請求的uri進行一致性hash
hash $remote_addr //對遠程主機(客戶端主機)進行hash
hash $cookie_name //對cookie名進行hash
(6) keepalive connections; //可使用長連接的連接數量;每worker與后端服務保持的最大空閑長連接數量;
四層代理和負載均衡模塊
模擬反代基于tcp或udp的服務連接,即工作于傳輸層的反代或調度器;
listen address:port //監聽的端口
Syntax: listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default: —
Context: server
默認為tcp協議;udp: 監聽udp協議的端口;
Nginx代理模塊
ngx_stream_proxy_module
ngx_stream_proxy_module模塊(1.9.0)允許通過TCP、UDP(1.9.13)和unix域套接字來代理數據流。
(1) proxy_pass address; //設置一個代理服務器的地址。該地址可以指定為一個域名或IP地址,一個端口或一個unix域的套接字路徑。
(2) proxy_timeout timeout; //代理服務響應超時時間,默認為10m;在客戶端或代理服務器連接上設置兩個連續讀或寫操作之間的超時。如果在這個時間內沒有傳輸數據,那么連接就關閉了。
(3) proxy_connect_timeout time; //定義與代理服務器建立連接的超時時間。設置nginx與被代理的服務器嘗試建立連接的超時時長;默認為60s;
示例:
stream {
upstream sshsrvs {
server 192.168.10.130:22;
server 192.168.10.131:22;
hash $remote_addr consistent;
}
server {
listen 172.16.100.6:22202;
proxy_pass sshsrvs;
proxy_timeout 60s;
proxy_connect_timeout 10s;
}
}