<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                更多參考上面Nginx相關->nginx.config配置文件的結構->2、http->nginx的location配置詳解 ## **全局變量** $args : 請求參數,同$query_string $content_length : 請求頭中的Content-length字段。 $content_type : 請求頭中的Content-Type字段。 $document_root : 當前請求在root指令中指定的值(結尾不帶斜杠, 不論配置中server->root有沒有以斜杠結尾)。 $host : 請求主機頭字段,否則為服務器名稱。 $http_user_agent : 客戶端agent信息 $http_cookie : 客戶端cookie信息 $limit_rate : 限制連接速率。 $request_method : 客戶端請求的動作,通常為GET或POST。 $remote_addr : 客戶端的IP地址。 $remote_port : 客戶端的端口。 $remote_user : 已經經過Auth Basic Module驗證的用戶名。 $request_filename : 當前請求的文件的絕對路徑,由root或alias指令與URI請求生成。 $scheme : HTTP方法(如http,https)。 $server_protocol : 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。 $server_addr : 服務器地址,在完成一次系統調用后可以確定這個值。 $server_name : 服務器名稱。 $server_port : 服務器的端口號。 $request_uri : 包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。 $uri : 不包含主機名和請求參數的當前URI,如”/foo/bar.html”。 $document_uri : 與$uri相同。 ## **flag 標志位** **flag可以是如下參數:** **`last`** 停止處理后續rewrite指令集,然后對當前重寫的新URI在rewrite指令集上重新查找。相當于Apache的[L]標記 **`break`** 出現在server, location, if段中,完成當前設置的重寫規則,停止執行其他的重寫規則,即停止處理后續rewrite指令集,并不再重新查找, 但是當前location內剩余非rewrite語句和location外的的非rewrite語句可以執行。 **`redirect`** 如果replacement不是以"http://" 或 "https://" 開頭,返回302臨時重定向,地址欄會顯示跳轉后的地址 **`permant`** 返回301永久重定向,地址欄會顯示跳轉后的地址 **last 和 break的區別:** * last一般寫在server{}和if{}段中,而break一般使用在location{}段中; 相當于Apache的【L】標記,表示完成rewrite * last 和 break 當出現在 location{} 之外時,兩者的作用是一致的沒有任何差異; 注意一點就是,他們會跳過所有的在他們之后的 rewrite 模塊中的指令,去選擇自己匹配的 location{} * last 和 break 當出現在location{} 內部時,兩者就存在了差異: * last 指令: rewrite 后會跳出 location{} 作用域,重新開始再走一次剛剛的行為。 * break 指令: rewrite后不會跳出 location{} 作用域。它的生命也在這個location{}中終結。 * **通俗易懂的說法:** * **last**:重新將rewrite后的地址在server標簽中執行 * **break**:將`rewrite`后的地址在當前`location`標簽中執行 ## **if指令** 語法:`if (condition){...}` 作用域:`server`,`location` `conditon`可以是如下任何內容: * 當表達式只是一個變量時,如果值為空或任何以0開頭的字符串都會當做 (bool)false * 直接比較變量和內容時,使用 = 或 != * 正則表達式匹配:~(區分大小寫)和~*(不區分大小寫),取反運算!~和!~\*. 如果這個正則表達式中包含} , ;則整個表達式需要用雙引號" 或 單引號' 包圍 * -f 和 !-f 用來判斷**是否存在**文件.(當-f后面的參數值為空時不會執行, 但是!-f會執行!!!) * -d 和 !-d 用來判斷**是否存在**目錄 * -e 和 !-e 用來判斷**是否存在**文件或目錄 * -x 和 !-x 用來判斷文件**是否可執行** ``` if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; } //如果UA包含"MSIE",rewrite請求到/msid/目錄下 if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1; } //如果cookie匹配正則,設置變量$id等于正則引用部分 if ($request_method = POST) { return 405; } //如果提交方法為POST,則返回狀態405(Method not allowed)。return不能返回301,302 if ($slow) { limit_rate 10k; } //限速,$slow可以通過 set 指令設置 if (!-f $request_filename){ break; proxy_pass http://127.0.0.1; } //如果請求的文件名不存在,則反向代理到localhost 。這里的break會停止rewrite檢查 if ($args ~ post=140){ rewrite ^ http://example.com/ permanent; } //如果query string中包含"post=140",永久重定向到example.com location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jefflei.com www.leizhenfang.com; if ($invalid_referer) { return 404; } //防盜鏈 } 優先級結論 (location = 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (location /) ``` 我們在set后面寫業務邏輯去匹配uri, 當匹配之后$full_file_path變量會被重新設置非空字符串; 如果不匹配, 則仍然是空字符串. 而經過測試, 對于空字符串變量, -f時會忽略, 但是!-f時不會忽略, 導致直接返回了404. 而其他不符合這個業務邏輯的請求就會遭到誤殺. 正則匹配 `.` : 匹配除換行符以外的任意字符 `?` : 重復0次或1次 `+` : 重復1次或更多次 `*` : 重復0次或更多次 `{n}` : 重復n次 `{n,}` : 重復n次或更多次 `\d` :匹配數字 `^` : 匹配字符串的開始 `$` : 匹配字符串的結尾 `[c]` : 匹配單個字符c `[a-zA-Z0-9]` : 匹配a-z小寫字母,A-Z大寫字母, 數字0-9 中的任意一個 `(jpg|png)` : 匹配字符串 jpg 或 png , 括號表示分組 ## **Rewrite語法** ~~~css rewrite < regex > < replacement > [flag] regex:正則表達式 replacement :跳轉后的內容 flag:rewrite支持的flag標記 ~~~ Rewrite實際場景 ~~~undefined 使用rewrite進行匹配跳轉(防盜鏈) 使用if匹配全局變量后跳轉 使用location匹配再跳轉(匹配的訪問 路徑URL location可以匹配本地的重寫以及跨服務器的跳轉) ~~~ >[danger]小括號()之間匹配的內容,可以在后面通過$1來引用,$2表示的是前面第二個()里的內容 例子:對形如 /images/ef/202103/test.png 的請求,重寫到 /data?file=test.png ``` rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4; # 注意不能在上面這條規則后面加上“last”參數,否則下面的set指令不會執行 set $file_name_without_ext $3; set $file_ext $4; ``` rewrite規則中如果要忽略大小寫, 可以使用 (?i) 這個與用"/"開始結尾的javascript/php等語言的reg pattern使用/i是不同的. ``` rewrite ^(.*)_(middle|small)(\.(?i)(jpg|jpeg|png|gif))$ /thumb_fixed.php break; ``` 個規則把 xxx\_middle.jpg, xxx\_middle.JPG, xxx\_small.png, xxx\_small.PNG 這樣的圖片地址進行了重寫 >[danger]請求的URL是給人看的,重寫后的URL是給電腦看的。 執行搜索 這個規則的目的是為了執行搜索,搜索URL中包含的關鍵字。 請求的URL //hqidi.com/search/some-search-keywords 重寫后URL //hqidi.com/search.php?p=some-search-keywords 重寫規則 rewrite ^/search/(.*)$ /search.php?p=$1?; 用戶個人資料頁面 大多數運行訪問者注冊的動態網站都提供一個可以查看個人資料的頁面,這個頁面的URL包含用戶的UID和用戶名 請求的URL //hqidi.com/user/47/dige 重寫后URL //hqidi.com/user.php?id=47&name=dige 重寫規則 rewrite ^/user/([0-9]+)/(.+)$ /user.php?id=$1&name=$2?; 多個參數 有些網站對字符串參數使用不同的語法,例如 通過斜線“/”來分隔非命名參數 請求的URL //hqidi.com/index.php/param1/param2/param3 重寫后URL //hqidi.com/index.php?p1=param1&p2=param2&p3=param3 重寫規則 rewrite ^/index.php/(.*)/(.*)/(.*)$ /index.php?p1=$1&p2=$2&p3=$3?; 類似百科的格式 這種格式特點,一個前綴目錄,后跟文章名稱 請求的URL //hqidi.com/wiki/some-keywords 重寫后URL //hqidi.com/wiki/index.php?title=some-keywords 重寫規則 rewrite ^/wiki/(.*)$ /wiki/index.php?title=$1?; 論壇 論壇一般用到兩個參數,一個話題標識(topic)一個出發點(starting post) 請求的URL //hqidi.com/topic-1234-50-some-keywords.html 重寫后URL //hqidi.com/viewtopic.php?topic=1234&start=50 重寫規則 rewrite ^/topic-([0-9]+)-([0-9]+)-(.*)\.html$ viewtopic.php?topic=$1&start=$2?; 新網站的文章 這種URL結構的特點,由一個文章標識符,后跟一個斜線,和一個關鍵字列表組成。 請求的URL //hqidi.com/88/future 重寫后URL //hqidi.com/atricle.php?id=88 重寫規則 rewrite ^/([0-9]+)/.*$ /aticle.php?id=$1?;
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看