Nginx配置文件location配置詳解
location介紹
官方文檔
location 指令是 nginx 中最關(guān)鍵的指令之一,location 指令的功能是用來匹配不同的 URI 請求,進(jìn)而對請求做不同的處理和響應(yīng),這其中較難理解的是多個 location 的匹配順序,本文會作為重點來解釋和說明。
開始之前先明確一些約定,我們輸入的網(wǎng)址叫做請求 URI,nginx 用請求 URI 與 location 中配置的 URI 做匹配。
nginx文件結(jié)構(gòu)
首先我們先簡單了解 nginx 的文件結(jié)構(gòu),nginx 的 HTTP 配置,由ngx_http_core_module模塊引入。nginx的HTTP配置主要包括四個區(qū)塊,結(jié)構(gòu)如下:
http {//協(xié)議級別
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream {//負(fù)載均衡配置
...
}
server {//服務(wù)器級別,每個server類似于httpd中的一個<VirtualHost>
listen 80;
server_name localhost;
location / {//請求級別,類似于httpd中的<Location>,用于定義URL與本地文件系統(tǒng)的映射關(guān)系
root html;
index index.html index.htm;
}
}
}
http{}段配置指令:
server {}:定義一個虛擬主機(jī),示例如下:
server {
listen 80;
server_name www.idfsoft.com;
root "/vhosts/web";
}
listen:指定監(jiān)聽的地址和端口
listen address[:port];
listen port;
server_name NAME […]; 后面可跟多個主機(jī),名稱可使用正則表達(dá)式或通配符
當(dāng)有多個server時,匹配順序如下:
- 先做精確匹配檢查
- 左側(cè)通配符匹配檢查,如*.idfsoft.com
- 右側(cè)通配符匹配檢查,如mail.*
- 正則表達(dá)式匹配檢查,如~ ^.*.idfsoft.com$
default_server
root path; 設(shè)置資源路徑映射,用于指明請求的URL所對應(yīng)的資源所在的文件系統(tǒng)上的起始路徑
alias path; 用于location配置段,定義路徑別名
index file; 默認(rèn)主頁面
index index.php index.html;
error_page code […] [=code] URI | @name 根據(jù)http響應(yīng)狀態(tài)碼來指明特用的錯誤頁面,例如 error_page 404 /404_customed.html
[=code]:以指定的響應(yīng)碼進(jìn)行響應(yīng),而不是默認(rèn)的原來的響應(yīng),默認(rèn)表示以新資源的響應(yīng)碼為其響應(yīng)碼,例如 error_page 404 =200 /404_customed.html
log_format 定義日志格式
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;
//注意:此處可用變量為nginx各模塊內(nèi)建變量
location區(qū)段,通過指定模式來與客戶端請求的URI相匹配
//功能:允許根據(jù)用戶請求的URI來匹配定義的各location,匹配到時,
//此請求將被相應(yīng)的location配置塊中的配置所處理,
//例如做訪問控制等功能
//語法:location [ 修飾符 ] pattern {......}
常用修飾符說明:
修飾符 | 功能 |
---|---|
= | 精確匹配 |
~ | 正則表達(dá)式模式匹配,區(qū)分大小寫 |
~* | 正則表達(dá)式模式匹配,不區(qū)分大小寫 |
^~ | 前綴匹配,類似于無修飾符的行為,也是以指定模塊開始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正則表達(dá)式 |
@ | 定義命名location區(qū)段,這些區(qū)段客戶端不能訪問,只可以由內(nèi)部產(chǎn)生的請求來訪問,如try_files或error_page等 |
沒有修飾符表示必須以指定模式開始,如:
server {
server_name www.idfsoft.com;
location /abc {
......
}
}
那么如下內(nèi)容就可正確匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
- http://www.idfsoft.com/abc/
=:表示必須與指定的模式精確匹配,如:
server {
server_name www.idfsoft.com;
location = /abc {
......
}
}
那么如下內(nèi)容就可正確匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下內(nèi)容則無法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abc/abcde
~:表示指定的正則表達(dá)式要區(qū)分大小寫,如:
server {
server_name www.idfsoft.com;
location ~ ^/abc$ {
......
}
}
那么如下內(nèi)容就可正確匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下內(nèi)容則無法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/ABC
- http://www.idfsoft.com/abcde
~*:表示指定的正則表達(dá)式不區(qū)分大小寫,如:
server {
server_name www.idfsoft.com;
location ~* ^/abc$ {
......
}
}
那么如下內(nèi)容就可正確匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
- http://www.idfsoft.com/ABC
如下內(nèi)容則無法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abcde
~:類似于無修飾符的行為,也是以指定模式開始,不同的是,如果模式匹配,則停止搜索其他模式
查找順序和優(yōu)先級:由高到底依次為
1.先精準(zhǔn)匹配 = ,精準(zhǔn)匹配成功則會立即停止其他類型匹配;
2.沒有精準(zhǔn)匹配成功時,進(jìn)行前綴匹配。先查找?guī)в?^~ 的前綴匹配,帶有 ^~ 的前綴匹配成功則立即停止其他類型匹配,普通前綴匹配(不帶參數(shù) ^~ )成功則會暫存,繼續(xù)查找正則匹配;
3.= 和 ^~ 均未匹配成功前提下,查找正則匹配 ~ 和 ~* 。當(dāng)同時有多個正則匹配時,按其在配置文件中出現(xiàn)的先后順序優(yōu)先匹配,命中則立即停止其他類型匹配;
4.所有正則匹配均未成功時,返回步驟 2 中暫存的普通前綴匹配(不帶參數(shù) ^~ )結(jié)果
以上規(guī)則簡單總結(jié)就是優(yōu)先級從高到低依次為(序號越小優(yōu)先級越高)
1. location = # 精準(zhǔn)匹配
2. location ^~ # 帶參前綴匹配
3. location ~ # 正則匹配(區(qū)分大小寫)
4. location ~* # 正則匹配(不區(qū)分大小寫)
5. location /a # 普通前綴匹配,優(yōu)先級低于帶參數(shù)前綴匹配。
6. location / # 任何沒有匹配成功的,都會匹配這里處
#案例分析
//修改配置文件
//刪除原來location的內(nèi)容添加新的內(nèi)容
[root@Masters ~]# vim /usr/local/nginx/conf/nginx.conf
42
43 location = / {
45 echo "[ configuration A ]"; //絕對引用根訪問時IP打印"[ configuration A ]"
46 }
47
48 location / {
49 echo "[ configuration B ]"; //在根下的文件打印"[ configuration B ]"
50 }
51
52 location /documents/ {
53 echo "[ configuration C ]"; //在根下的/documents/目錄下文件及目錄打印"[ configuration C ]"
54 }
55
56 location ^~ /images/ {
57 echo "[ configuration D ]"; //在根下的/images/目錄下的文件及目錄打印"[ configuration D ]"
58 }
59
60 location ~* \.(gif|jpg|jpeg)$ { //在根下的以.(gif|jpg|jpeg)結(jié)尾的文件打印"[ configuration E ]"
61 echo "[ configuration E ]";
62 }
[root@Masters ~]# nginx -s reload
[root@Masters ~]# systemctl restart nginx.service
[root@Masters ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
詳細(xì)注解:
location = / {
# 只精準(zhǔn)匹配 / 的查詢.
[ configuration A ]
}
#匹配成功: /
location / {
# 匹配任何請求,因為所有請求都是以”/“開始
# 但是更長字符匹配或者正則表達(dá)式匹配會優(yōu)先匹配
[ configuration B ]
}
#匹配成功:/index.html
location /documents {
# 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續(xù)往下搜索/
# 只有后面的正則表達(dá)式?jīng)]有匹配到時,這一條才會采用這一條/
[ configuration C ]
}
#匹配成功:/documents/document.html
#匹配成功:/documents/abc
location ~ /documents/ABC {
# 區(qū)分大小寫的正則匹配
# 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續(xù)往下搜索/
# 只有后面的正則表達(dá)式?jīng)]有匹配到時,這一條才會采用這一條/
[ configuration CC ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 開頭的地址,匹配符合以后,立即停止往下搜索正則,采用這一條。/
[ configuration D ]
}
#成功匹配:/images/a.gif
location ~* .(gif|jpg|jpeg)$ {
# 匹配所有以 .gif、.jpg 或 .jpeg 結(jié)尾的請求,不區(qū)分大小寫
# 然而,所有請求 /images/ 下的圖片會被 [ config D ] 處理,因為 ^~ 到達(dá)不了這一條正則/
[ configuration E ]
}
#成功匹配:/documents/a.jpg
location /images/ {
# 字符匹配到 /images/,繼續(xù)往下,會發(fā)現(xiàn) ^~ 存在/
[ configuration F ]
}
location /images/abc {
# 最長字符匹配到 /images/abc,繼續(xù)往下,會發(fā)現(xiàn) ^~ 存在/
# F與G的放置順序是沒有關(guān)系的/
[ configuration G ]
}
location ~ /images/abc/ {
# 只有去掉 [ config D ] 才有效:先最長匹配 [ config G ] 開頭的地址,繼續(xù)往下搜索,匹配到這一條正則,采用/
[ configuration H ]
}
訪問控制
用于location段
allow:設(shè)定允許哪臺或哪些主機(jī)訪問,多個參數(shù)間用空格隔開
deny:設(shè)定禁止哪臺或哪些主機(jī)訪問,多個參數(shù)間用空格隔開
示例:
allow 192.168.1.1/32 172.16.0.0/16;
deny all;
實踐:
//拒接本機(jī)訪問
location / {
echo "GUI";
deny 192.168.79.1;
}
//除本主機(jī)外拒接所有訪問
注意!deny all要寫在后面,因為是依次檢查規(guī)則的,如果第一條直接寫deny all,下面的就不會被匹配到了。
location / {
echo "GUI";
allow 192.168.79.1;
deny all;
}
基于用戶認(rèn)證
auth_basic “歡迎信息”;
auth_basic_user_file “/path/to/user_auth_file”
user_auth_file內(nèi)容格式為:
username:password
這里的密碼為加密后的密碼串,建議用htpasswd來創(chuàng)建此文件:
htpasswd -c -m /path/to/.user_auth_file USERNAME
實踐:
//下載httpd-tools
[root@Masters ~]# yum -y install httpd-tools
//修改配置文件
[root@Masters ~]# vim /usr/local/nginx/conf/nginx.conf
location = / {
root html;
auth_basic "Welcome home!"
auth_basic_user_file ".htpasswd"
echo "GHI";
}
//創(chuàng)建密碼文件
[root@Masters ~]# cd /usr/local/nginx/conf/
[root@Masters conf]# htpasswd -c -m /usr/local/nginx/conf/.htpasswd GUI
New password:
Re-type new password:
Adding password for user GUI
[root@Masters conf]# cat .htpasswd
GUI:$apr1$DX3M6FYG$5U1/X02PWEaU.NGs2HjYA/
開啟狀態(tài)頁面
官方文檔
開啟status:
location /status {
stub_status {on | off};
allow 172.16.0.0/16;
deny all;
}
訪問狀態(tài)頁面的方式:http://server_ip/status
狀態(tài)頁面信息詳解:
狀態(tài)碼 | 表示的意義 |
---|---|
Active connections 2 | 當(dāng)前所有處于打開狀態(tài)的連接數(shù) |
accepts | 總共處理了多少個連接 |
handled | 成功創(chuàng)建多少握手 |
requests | 總共處理了多少個請求 |
Reading | nginx讀取到客戶端的Header信息數(shù),表示正處于接收請求狀態(tài)的連接數(shù) |
Writing | nginx返回給客戶端的Header信息數(shù),表示請求已經(jīng)接收完成,且正處于處理請求或發(fā)送響應(yīng)的過程中的連接數(shù) |
Waiting | 開啟keep-alive的情況下,這個值等于active - (reading + writing),意思就是Nginx已處理完正在等候下一次請求指令的駐留連接 |
實踐:
//開啟狀態(tài)頁面
location = /status {
stub_status;
}
//開啟狀態(tài)頁面基于用戶認(rèn)證
location = /status {
stub_status;
auth_basic "GHI";
auth_basic_user_file ".htpasswd";
}
https配置
生成私鑰,生成證書簽署請求并獲得證書,然后在nginx.conf中配置如下內(nèi)容:
server {
listen 443 ssl;
server_name www.idfsoft.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.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;
}
}
實踐使用OpenSSL實現(xiàn):
//創(chuàng)建存放位置
[root@Masters ~]# cd /etc/Gin/KI/
[root@Masters KI]# mkdir private
//生成密鑰
[root@Masters KI]# umask 077;openssl genrsa -out private/cakey.pem 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.....................................................+++++
.........................................................................................+++++
e is 65537 (0x010001)
//提取公鑰
[root@Masters KI]# openssl rsa -in private/cakey.pem -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyrcm52Nt2eLvdNOnbGMy
yigtRYK+w9ZXanb/8dVvLrAtU3LQb3fBNUU8z3EBegNp+b8SXHbGIx5/3cYuv9tH
duhY7YjccVY+ldyj9zheodXsXctX251hZ6oUPONFM/QnRzBooH2TQTXhnyq6D/zx
71mAHVAf5DlZ8DoJlmxnx50rtPOlc/7LRVQs4edUQiUaHSS93+CmSer1ICS5WKfo
Psg2peL6GrHTsUyqY8Cf210qq62fL8BOpvHXzUIJiIZjxV8/cho4BxccnstvDMrt
RmWWVKvGTFlDoyJQvBFWmLYXFqAmAdOFMAe8KHnY9iVZLryRI/P0z00Ouk3JG2od
RQIDAQAB
-----END PUBLIC KEY-----
//生成自簽發(fā)證書
[root@Masters KI]# openssl rsa -in private/cakey.pem -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyrcm52Nt2eLvdNOnbGMy
yigtRYK+w9ZXanb/8dVvLrAtU3LQb3fBNUU8z3EBegNp+b8SXHbGIx5/3cYuv9tH
duhY7YjccVY+ldyj9zheodXsXctX251hZ6oUPONFM/QnRzBooH2TQTXhnyq6D/zx
71mAHVAf5DlZ8DoJlmxnx50rtPOlc/7LRVQs4edUQiUaHSS93+CmSer1ICS5WKfo
Psg2peL6GrHTsUyqY8Cf210qq62fL8BOpvHXzUIJiIZjxV8/cho4BxccnstvDMrt
RmWWVKvGTFlDoyJQvBFWmLYXFqAmAdOFMAe8KHnY9iVZLryRI/P0z00Ouk3JG2od
RQIDAQAB
-----END PUBLIC KEY-----
[root@Masters KI]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:www.Gin.com
Organizational Unit Name (eg, section) []:www.Gin.com
Common Name (eg, your name or your server's hostname) []:www.Gin.com
Email Address []:123@5.com
[root@Masters KI]# ls
cacert.pem private
[root@Masters KI]# mkdir certs newcerts crl
[root@Masters KI]# touch index.txt && echo 01 > serial
[root@Masters KI]# ls
cacert.pem certs crl index.txt newcerts private serial
[root@Masters KI]# cd /usr/local/nginx/conf/
[root@Masters conf]# mkdir ssl
[root@Masters conf]# cd ssl/
//客戶端證書
[root@Masters ssl]# umask 077;openssl genrsa -out nginx.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
...........................+++++
.............................................+++++
e is 65537 (0x010001)
[root@Masters ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:^C
[root@Masters ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:www.Gin.com
Organizational Unit Name (eg, section) []:www.Gin.com
Common Name (eg, your name or your server's hostname) []:www.Gin.com
Email Address []:123@5.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123
string is too short, it needs to be at least 4 bytes long
A challenge password []:
An optional company name []:
[root@Masters ssl]# ls
nginx.csr nginx.key
//部署
[root@Masters ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 13 20:35:41 2022 GMT
Not After : Oct 13 20:35:41 2023 GMT
Subject:
countryName = CN
stateOrProvinceName = HB
organizationName = www.Gin.com
organizationalUnitName = www.Gin.com
commonName = www.Gin.com
emailAddress = 123@5.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
04:7E:A7:7C:55:34:E8:B8:52:D0:C5:CE:FA:C4:14:E2:A5:96:08:A2
X509v3 Authority Key Identifier:
keyid:76:81:2D:69:5B:17:F6:52:4C:C4:91:7C:BC:FD:90:21:01:C0:49:F7
Certificate is to be certified until Oct 13 12:38:41 2023 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@Masters ssl]# ls
nginx.crt nginx.csr nginx.key
[root@Masters ssl]# rm -f nginx.csr
[root@localhost ssl]# ls
nginx.crt nginx.key
rewrite
語法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*.jpg)$ /imgs/$1 break;
此處的$1用于引用(.*.jpg)匹配到的內(nèi)容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
如上例所示,replacement可以是某個路徑,也可以是某個URL
常見的flag
|flag |作用|
|–|–|–|
|last |基本上都用這個flag,表示當(dāng)前的匹配結(jié)束,繼續(xù)下一個匹配,最多匹配10個到20個 一旦此rewrite規(guī)則重寫完成后,就不再被后面其它的rewrite規(guī)則進(jìn)行處理 而是由UserAgent重新對重寫后的URL再一次發(fā)起請求,并從頭開始執(zhí)行類似的過程|
|break |中止Rewrite,不再繼續(xù)匹配 一旦此rewrite規(guī)則重寫完成后,由UserAgent對新的URL重新發(fā)起請求, 且不再會被當(dāng)前l(fā)ocation內(nèi)的任何rewrite規(guī)則所檢查|
|redirect |以臨時重定向的HTTP狀態(tài)302返回新的URL|
|permanent |以永久重定向的HTTP狀態(tài)301返回新的URL|
rewrite模塊的作用是用來執(zhí)行URL重定向。這個機(jī)制有利于去掉惡意訪問的url,也有利于搜索引擎優(yōu)化(SEO)
nginx使用的語法源于Perl兼容正則表達(dá)式(PCRE)庫,基本語法如下:
標(biāo)識符 | 意義 |
---|---|
^ | 必須以^后的實體開頭 |
$ | 必須以$前的實體結(jié)尾 |
. | 匹配任意字符 |
[] | 匹配指定字符集內(nèi)的任意字符 |
[^] | 匹配任何不包括在指定字符集內(nèi)的任意字符串 |
() | 分組,組成一組用于匹配的實體,通常會有 |
捕獲子表達(dá)式,可以捕獲放在()之間的任何文本,比如:
^(hello|sir)$ //字符串為“hi sir”捕獲的結(jié)果:$1=hi$2=sir
//這些被捕獲的數(shù)據(jù),在后面就可以當(dāng)變量一樣使用了
示例:
案例1:將匹配到的URI重寫至其它本機(jī)下的其它資源位置
#匹配到以images開頭,以.jpg結(jié)尾的URI,重定向到imgs目錄下的以.jpg結(jié)尾的文件
#$1是引用都第一個掛號內(nèi)匹配到的內(nèi)容
[root@nginx conf]# vim nginx.conf
..............
location /images {
rewrite ^/images/(.*\.jpg)$ /images/$1 break;
}
...............
[root@Masters ~]# cd /usr/local/nginx/html/
[root@Masters html]# mkdir images
[root@Masters html]# cd images/
[root@Masters images]# ls
test.jpg
[root@Masters ~]# systemctl reload nginx.service
案例2:將匹配到的URI重寫至其它網(wǎng)頁
#匹配到以ll開頭的,/后面是任意內(nèi)容的URI時,將重定向至www.zealf.red
[root@nginx conf]# vim nginx.conf
.............
location /ll {
rewrite ^/ll/.*$ https://www.zealf.red;
}
.............
[root@Masters ~]# cd /usr/local/nginx/html/
[root@Masters html]# mkdir ll
[root@Masters html]# systemctl reload nginx.service
if
語法:if (condition) {…}
應(yīng)用場景:文章來源:http://www.zghlxwxcb.cn/news/detail-448438.html
- server段
- location段
常見的condition文章來源地址http://www.zghlxwxcb.cn/news/detail-448438.html
- 變量名(變量值為空串,或者以“0”開始,則為false,其它的均為true)
- 以變量為操作數(shù)構(gòu)成的比較表達(dá)式(可使用=,!=類似的比較操作符進(jìn)行測試)
- 正則表達(dá)式的模式匹配操作
- ~:區(qū)分大小寫的模式匹配檢查
- ~*:不區(qū)分大小寫的模式匹配檢查
- !和!*:對上面兩種測試取反
- 測試指定路徑為文件的可能性(-f,!-f)
- 測試指定路徑為目錄的可能性(-d,!-d)
- 測試文件的存在性(-e,!-e)
- 檢查文件是否有執(zhí)行權(quán)限(-x,!-x)
基于瀏覽器實現(xiàn)分離案例
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) {
rewrite ^(.*)$ /chrome/$1 break;
防盜鏈案例
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.idfsoft.com;
if ($invalid_referer) {
rewrite ^/ http://www.idfsoft.com/403.html;
}
}
到了這里,關(guān)于Nginx配置文件配置詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!