目錄
-
ngx_http_rewrite_module模塊
-
ngx_http_gzip_module模塊
-
ngx_http_fastcgi_module模塊
-
ngx_http_ssl_module模塊
-
ngx_http_referer_module模塊
ngx_http_rewrite_module模塊
將用戶某一次請求的URI當中的字符串是不是能夠被我們給出的regex所描述的模式所匹配,如果能,則替換為新的URL
1、rewrite
語法格式:rewrite regex replacement [flag]
Default: —
Context: server, location, if將用戶請求的URI基于regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI;
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自上而下逐個檢查;被某條件規則替換完成后,會重新一輪的替換檢查,因此,隱含有循環機制;[flag]所表示的標志位用于控制此循環機制;
如果replacement是以http://或https://的絕對路徑開頭,則替換結果會直接以重定向返回給客戶端;[flag]:
last:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環;break:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它配置;結束循環;
redirect:重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;不能以http://或https://開頭;
permanent:重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;
2、return
語法:return code [text];
return code URL;
return URL;
Default: —
Context: server, location, if停止處理并將指定的代碼返回給客戶端
3、rewrite_log
語法:rewrite_log on | off;
Default: rewrite_log off;
Context: http, server, location, if是否開啟重寫日志
4、if
語法:if (condition) { … }
Default: —
Context: server, location引入一個新的配置上下文 ;條件滿足時,執行配置塊中的配置指令;server, location;
condition:
比較操作符:
==
!=
~:模式匹配,區分字符大小寫;
~*:模式匹配,不區分字符大小寫;
!~:模式不匹配,區分字符大小寫;
!~*:模式不匹配,不區分字符大小寫;文件及目錄存在性判斷:
-e, !-e
-f, !-f
-d, !-d
-x, !-x
5、set
語法:set $variable value;
Default: —
Context: server, location, if用戶自定義變量
6、break
語法:Syntax: break;
Default: —
Context: server, location, if停止處理當前ngx_http_rewrite_module指令集。
簡單示例
ngx_http_gzip_module模塊
ngx_http_gzip_module模塊是一個過濾器,壓縮響應使用“gzip”方法。這往往有助于減少一半或更多的傳輸數據的大小。
1、gzip
語法:gzip on | off;
Default: gzip off;
Context: http, server, location, if in location是否開啟gzip功能
2、gzip_comp_level
語法:gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location指定壓縮比,1-9
3、gzip_disable
語法:gzip_disable regex …;
Default: —
Context: http, server, location對被指定的模式所匹配到的瀏覽器禁用gzip功能
4、gzip_min_length
語法:gzip_min_length length;
Default: gzip_min_length 20;
Context: http, server, location啟用壓縮功能的響應報文最小長度
5、gzip_buffers
語法:gzip_buffers number size;
Default: gzip_buffers 32 4k|16 8k;
Context: http, server, location支持實現壓縮功能時為其配置的緩沖區數量及每個緩存區的大??;
6、gzip_proxied
語法:gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any …;
Default: gzip_proxied off;
Context: http, server, locationnginx作為代理服務器接收到從被代理服務器發送的響應報文后,在何種條件下啟用壓縮功能的;
off:對代理的請求不啟用
no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值為此三者中任何一個,則啟用壓縮功能;
7、gzip_types
語法:gzip_types mime-type …;
Default: gzip_types text/html;
Context: http, server, location壓縮過濾器,僅對此處設定的MIME類型的內容啟用壓縮功能;
8、gzip_vary
語法:gzip_vary on | off;
Default: gzip_vary off;
Context: http, server, location如果啟用了gzip功能是否在響應報文首部添加Vary: Accept-Encoding項
配置示例
ngx_http_fastcgi_module模塊
ngx_http_fastcgi_module模塊允許通過請求FastCGI服務器,將動態頁面發送至FastCGI服務器解釋執行
1、fastcgi_pass
語法:fastcgi_pass address;
Default: —
Context: location, if in locationfastcgi服務器IP地址
2、fastcgi_index
語法:fastcgi_index name;
Default: —
Context: http, server, locationfastcgi默認的主頁資源;
3、fastcgi_param
語法:fastcgi_param parameter value [if_not_empty];
Default: —
Context: http, server, location設置一個參數,將此參數傳遞給FastCGI服務器。該值可以包含文本、變量和它們的組合。
配置php-fpm示例
1.安裝php-fpm和mysql
[root@centos7 ~]# yum -y install php-fpm mariadb-server
2.Nginx配置文件
3.測試訪問
4、fastcgi_cache_path
語法: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];
Context: http定義fastcgi的緩存;緩存位置為磁盤上的文件系統,由path所指定路徑來定義;
levels=levels:緩存目錄的層級數量,以及每一級的目錄數量;levels=ONE:TWO:THREE
leves=1:2:2
keys_zone=name:size
k/v映射的內存空間的名稱及大小
inactive=time
非活動時長
max_size=size
磁盤上用于緩存數據的緩存空間上限
5、fastcgi_cache
語法:fastcgi_cache zone | off;
Default: fastcgi_cache off;
Context: http, server, location調用指定的緩存空間來緩存數據;http, server, location
6、fastcgi_cache_key
語法:fastcgi_cache_key string;
Default: —
Context: http, server, location定義用作緩存項的key的字符串;
7、fastcgi_cache_methods
語法:fastcgi_cache_methods GET | HEAD | POST …;
Default: fastcgi_cache_methods GET HEAD;
Context: http, server, location為哪些請求方法使用緩存;
8、fastcgi_cache_min_uses
語法:astcgi_cache_min_uses number;
Default: fastcgi_cache_min_uses 1;
Context: http, server, location緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項;
9、fastcgi_cache_valid
語法:fastcgi_cache_valid [code …] time;
Default: —
Context: http, server, location不同的響應碼各自的緩存時長;
10、fastcgi_keep_conn
語法:fastcgi_keep_conn on | off;
Default: fastcgi_keep_conn off;
Context: http, server, location默認情況下,一個FastCGI服務器將發送響應后關閉連接正確。然而,當這個指令設置的值,Nginx會指示一個FastCGI服務器保持連接打開。
fastcgi_cache配置示例
1.Nginx配置文件
2.啟用緩存功能前做壓測
3.啟用緩存功能后做壓縮
4.生成緩存文件
ngx_http_ssl_module模塊
ngx_http_ssl_module模塊提供HTTPS功能支持
1、ssl
語法:ssl on | off;
Default: ssl off;
Context: http, server是否開啟ssl功能
2、ssl_certificate
語法:ssl_certificate file;
Default: —
Context: http, server當前虛擬主機使用PEM格式的證書文件;
3、ssl_certificate_key
語法:ssl_certificate_key file;
Default: —
Context: http, server當前虛擬主機上與其證書匹配的私鑰文件;
4、ssl_protocols
語法:ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
Default: ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Context: http, server支持ssl協議版本,默認為后三個;
5、ssl_session_cache
語法:ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
Default: ssl_session_cache none;
Context: http, serverbuiltin[:size]:使用OpenSSL內建的緩存,此緩存為每worker進程私有;
[shared:name:size]:在各worker之間使用一個共享的緩存;
6、ssl_session_timeout
語法:ssl_session_timeout time;
Default: ssl_session_timeout 5m;
Context: http, server客戶端一側的連接可以復用ssl session cache中緩存 的ssl參數的有效時長;
ssl配置示例
1.CA配置
2.nginx端生成簽署請求
3.CA簽證
4.Nginx配置文件
5.測試
ngx_http_referer_module模塊
ngx_http_referer_module模塊是用來阻止訪問一個在referer頭域值無效請求的網站。
1、valid_referers
語法:valid_referers none | blocked | server_names | string …;
Default: —
Context: server, location定義referer首部的合法可用值;
none:請求報文首部沒有referer首部;
blocked:請求報文的referer首部沒有值;
server_names:參數,其可以有值作為主機名或主機名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭,例如 ~.*.magedu.com;
配置示例
valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
if($invalid_referer) {
return 403;
}
原創文章,作者:zhai796898,如若轉載,請注明出處:http://www.www58058.com/57665