Nginx中if語句中的判斷條件


一、if語句中的判斷條件(nginx)

1、正則表達式匹配:

==:等值比較;

~:與指定正則表達式模式匹配時返回“真”,判斷匹配與否時區分字符大小寫;

~*:與指定正則表達式模式匹配時返回“真”,判斷匹配與否時不區分字符大小寫;

!~:與指定正則表達式模式不匹配時返回“真”,判斷匹配與否時區分字符大小寫;

!~*:與指定正則表達式模式不匹配時返回“真”,判斷匹配與否時不區分字符大小寫;

 

2、文件及目錄匹配判斷:

-f, !-f:判斷指定的路徑是否為存在且為文件;

-d, !-d:判斷指定的路徑是否為存在且為目錄;

-e, !-e:判斷指定的路徑是否存在,文件或目錄均可;

-x, !-x:判斷指定路徑的文件是否存在且可執行;

 

 3、部分正則表達式可以在圓括號內"()",其值可以通過后面的變量$1到$9來訪問:

rewrite ^/b/(.*)\.html /play.php?video=$1 last;#其中$1就表示引用前面匹配的(.*)里面的內容。

 

server {

listen       80;
server_name  test.enjoy.com;

#set $flag 0;
##if ($flag = 0) {
#    return 501;
#}

#客戶端請求的完整請求路徑
#if ( $request_uri ~* /(.*)\.php ) {                                        
#    return 502;
#}

if (!-f $request_filename) {
        return 414;
}

#禁止chrome訪問
#if ($http_user_agent ~ Chrome) {
#        return 503;
#}

location /loct {
    return 401;
}
location / {
    #if
    root html;
    index index.html;
}

}

 

可使用的全局變量:

- $args
- $content_length
- $content_type
- $document_root
- $document_uri
- $host
- $http_user_agent
- $http_cookie
- $limit_rate
- $request_body_file
- $request_method
- $remote_addr
- $remote_port
- $remote_user
- $request_filename
- $request_uri
- $query_string
- $scheme
- $server_protocol
- $server_addr
- $server_name
- $server_port
- $uri

參考鏈接:https://blog.csdn.net/pf1234321/article/details/83072550

https://blog.csdn.net/qq_32642039/article/details/76785352


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM