高性能反向代理服務(wù)器--Nginx
本文簡(jiǎn)述:rewrite使用蛮艰、瀏覽器本地緩存配置及動(dòng)靜分離
個(gè)人學(xué)習(xí)筆記腋腮,如有不足,歡迎指正壤蚜。 2018-5-22
一即寡、內(nèi)容
1、Rewrite的使用
2袜刷、緩存配置及Gzip配置
二聪富、筆記
1.1)rewrite的使用
rewrite通過(guò)ngx_http_rewrite_module模塊支持url重寫(xiě)、支持if判斷著蟹,但不支持else.
rewrite功能是:使用nginx提供的全局變量或子集設(shè)置的變量善涨,結(jié)合正則表達(dá)式和標(biāo)志位實(shí)現(xiàn)url重寫(xiě)以及重定向。
rewrite只能是放在server{},location{},if{}中 草则,并且只能對(duì)域名后邊的除去傳遞的參數(shù)外的字符串起作用。
1.2)常用指令
if 空格 (條件) {設(shè)定條件進(jìn)行重寫(xiě)}
條件語(yǔ)法
“=” 判斷相等蟹漓,用于字符比較炕横。
"~"用正則來(lái)匹配 (表示區(qū)分大小寫(xiě)),“~*” 不區(qū)分大小寫(xiě)
-
"-f -d -e"來(lái)判斷是否為文件葡粒、目錄份殿、是否存在
關(guān)于if (條件)中的條件膜钓,具體還有那些功能大家可以參見(jiàn)官方文檔。
以下是這段文字是Module ngx_http_rewrite_module 中的內(nèi)容卿嘲。
A condition may be any of the following:
a variable name; false if the value of a variable is an empty string or “0”;
Before version 1.0.1, any string starting with “0” was considered a false value.
comparison of a variable with a string using the “=” and “!=” operators;
matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the $1..$9 variables. Negative operators “!~” and “!~” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
checking of a file existence with the “-f” and “!-f” operators;
checking of a directory existence with the “-d” and “!-d” operators;
checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;
checking for an executable file with the “-x” and “!-x” operators.
return指令
語(yǔ)法:return code;
停止出來(lái)并返回指定狀態(tài)碼給客戶(hù)端颂斜。
if($request_uri ~*\.sh){
return 403
}
set指令
set variable value;
定義一個(gè)變量并復(fù)制,值可以是文本拾枣、變量或者文本變量混合體沃疮。
rewrite指令
語(yǔ)法:rewrite regex replacement [flag]{last / break/ redirect 返回臨時(shí)302/ permant 返回永久302}
last
:停止處理后續(xù)的rewrite指令集、然后對(duì)當(dāng)前重寫(xiě)的url在rewrite指令集上重新查找梅肤。
break
:停止出來(lái)后續(xù)的rewrite指令集司蔬,并不會(huì)重新查找。
示例
-
攔截以
/
開(kāi)頭的url ,重定向到百度網(wǎng)頁(yè)姨蝴。location / { # root html; # index index.html; rewrite ^/ http://www.baidu.com; #重定向 }
-
正則匹配url中請(qǐng)求的地址俊啼,假設(shè)我們請(qǐng)求的地址是192.168.0.85/images/www/wcl.png 會(huì)重寫(xiě)到
/mic?file=wcl.png
,于是變匹配到location /mic
;通過(guò)try_files
獲取存在的文件進(jìn)行返回左医。如果存在這個(gè)文件 便顯示授帕,如果不存在,接著向下匹配location = /image404.html
最終返回404錯(cuò)誤浮梢。location / { rewrite '^/images/([a-z]{3})/(.*)\.(png)$' /mic?file=$2.$3 last;##注意 此處last可以不寫(xiě) 也可以寫(xiě)break 作用不同 set $image_file $2; set $image_file $3; } location /mic { root html; try_files /$arg_file /image404.html; } location = /image404.html { return 404 "image not found exception"; }
注意:上述代碼中我添加注釋的部分:如果是last跛十,最后運(yùn)行結(jié)果和不加last效果一樣,顯示結(jié)果__404image not found exception __ ;如果是break,最終顯示的結(jié)果是:
404 Not Found
rewrite匹配規(guī)則
似乎我們看到rewrite
與location
功能相似黔寇,都實(shí)現(xiàn)了跳轉(zhuǎn)偶器。在這里我們要重要強(qiáng)調(diào)rewrite
與location
本質(zhì)區(qū)別。
rewrite
是在同一域名內(nèi)缝裤,更改獲取資源的路徑屏轰。
location
是對(duì)一類(lèi)路徑做控制訪問(wèn)或反向代理,可以proxy_pass
到其他機(jī)器憋飞。
在實(shí)際應(yīng)用中rewrite
也會(huì)寫(xiě)在location
中霎苗,執(zhí)行順序是:
執(zhí)行server塊的rewrite指令。
執(zhí)行l(wèi)ocation匹配
執(zhí)行選定的location中的rewrite指令
如果其中某步URI被重寫(xiě)榛做,則重新循環(huán)執(zhí)行1-3唁盏,直到找到真實(shí)存在的文件;循環(huán)超過(guò)10次检眯,則返回500 Internal Server Error錯(cuò)誤
2.1)瀏覽器本地緩存配置及動(dòng)靜分離
語(yǔ)法:expires 60s|m|h|d
示例:
在這里厘擂,我在html目錄下創(chuàng)建了一個(gè)images文件夾,該文件夾下有一張圖片锰瘸。
修改index.html, 增加
<img src=”圖片”/>
修改nginx.conf配置刽严。配置兩個(gè)location實(shí)現(xiàn)動(dòng)靜分離,并且在靜態(tài)文件中增加
expires
的緩存期限 為5min避凝。location /{ root html; index index.html index.htm; } location ~ \.(png|js|jpg|gif|css)$ { root html/images; expires 5m; ##靜態(tài)資源緩存 緩存在瀏覽器中 5min中之內(nèi)不去去找服務(wù)器找這個(gè)文件 }
好了 運(yùn)行nginx,在地址欄輸入地址請(qǐng)求 舞萄。f12
- 第一次訪問(wèn).png
第二次訪問(wèn)
- 這里寫(xiě)圖片描述
其實(shí)我們可以f12查看這張圖片的在此請(qǐng)求的時(shí)間眨补,而不是從瀏覽器緩存中讀取:
- expires.png
2.2)Gzip壓縮策略
瀏覽器請(qǐng)求-->告訴服務(wù)器當(dāng)前瀏覽器可以致辭壓縮類(lèi)型-->服務(wù)端會(huì)把內(nèi)容根據(jù)瀏覽器所所支持的壓縮策略去進(jìn)行壓縮返回
-->瀏覽器拿到數(shù)據(jù)后解碼倒脓;
常見(jiàn)的壓縮方式:gzip
撑螺、deflate
、sdch
在這里我看一下我的chrome的解壓方式崎弃。
示例:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65s;
server {
listen 80;
server_name localhost 192.*;
gzip off; #開(kāi)啟
gzip_buffers 4 16k; # 以16k為單位申請(qǐng)4倍的壓縮使用內(nèi)存
gzip_comp_level 4; # 壓縮級(jí)別 級(jí)別越高 最大到9 越高越容易失真 同時(shí) 壓縮涉及到運(yùn)算 影響cpu性能
gzip_min_length 500;# 壓縮時(shí) 這個(gè)文件達(dá)到最小的長(zhǎng)度不被壓縮甘晤。
gzip_types text/css text/xml application/javascript; #針對(duì)于那些類(lèi)型的文件進(jìn)行壓縮 mime.types
## 瀏覽器緩存
location /{
root html;
index index.html index.htm;
}
location ~ \.(png|png|js|jpg|gif|css)$ {
root html/images;
expires 5m; ##靜態(tài)資源緩存 緩存在瀏覽器中 5min中之內(nèi)不去去找服務(wù)器找這個(gè)文件
}
}
}
注意:
1、圖片吊履、mp3這樣的二進(jìn)制文件安皱,沒(méi)有必要進(jìn)行壓縮處理。因?yàn)檫@類(lèi)文件壓縮比很小艇炎,壓縮過(guò)程會(huì)耗費(fèi)CPU資源
2酌伊、太小的文件沒(méi)有必要壓縮,因?yàn)閴嚎s以后會(huì)增加一些頭信息缀踪,反而會(huì)當(dāng)導(dǎo)致文件變大
3居砖、Nginx默認(rèn)只對(duì)text/html進(jìn)行壓縮,如果要對(duì)html之外的內(nèi)容進(jìn)行壓縮傳輸驴娃,需要我們手動(dòng)配置奏候。配置類(lèi)型可以參見(jiàn)mime.types