<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # rewrite重寫模塊 [TOC] ## 一、 說明 rewrite地址可以實現地址重寫,簡單的用法就是偽靜態網頁,Nginx的rewrite規則需要pcre軟件的支持,即通過Perl兼職正則表達式對語法進行規則匹配 ### 1. rewrite語法 ```sh rewrite regex replacement [flag] ``` 解釋:rewrite指令匹配regex處的正則表達式,匹配成功后跳轉到replacement處的網頁,最后flag是標記 ``` rewrite ^/(.*) http://www.etiantian.org/$1 permanent ``` 解釋:此處的正則表達式表示匹配所有,匹配成功后跳轉到etiantian.org這個網頁上。$1是后向引用正則表達式括號里面的內容,并用permanent做標記為永久301重定向 ### 2. regex常用正則表達式 ```sh \ 將后面的字符標記為一個特殊字符、原意字符、后向引用 如\n匹配換行符,\$匹配$ ^ 匹配輸入字符串的起始位置 $ 匹配輸入字符串的結束位置 * 匹配前面的字符零次或多次 + 匹配前面的字符一次或多次 ? 匹配前面的字符零次或一次 . 匹配除\n之外的任何單個字符 (pattern) 匹配括號內的表達式,常用來做后向引用 ``` ### 3. rewrite指令flag標記說明 ```sh last 本條規則匹配完成后,繼續向下匹配新的location URI規則。 break 本條規則匹配完成后終止,不再向后匹配規則。 redirect 返回302臨時重定向。 permanent 返回301永久重定向。 ``` **說明:** 1. last和break實現url重寫,瀏覽器顯示的地址不變,服務器端訪問的程序和路徑發生了變化 2. redirect和permanent實現url跳轉,瀏覽器顯示的地址是跳轉后的地址 3. 使用別名alias時,要使用last標記,。 4. 使用proxy_pass時,要使用break標記 5. 在根location(即location/{......})中或server{......}標簽中,編寫rewrite規則,建議使用last標記 6. 在普通的loaction(如location/oldboy/{......}或if{ })中編寫rewrite規則,則建議使用break標記 ### 4. rewirt在企業中的應用場景 1)可以跳轉用戶瀏覽的url,使其看起來更規范,符合開發及產品人員的需求 2)為了讓搜索引擎搜錄網站的內容,將動態url地址偽裝成靜態地址 3)網站換新域名后,將舊域名的訪問跳轉到新域名上 4)根據特殊變量、目錄、客戶端的信息進行url跳轉 ## 二、 企業案例 ### **1. 需求** 要求訪etiantian.org時,跳轉到`www.etiantian.org ` ### 2. 錯誤配置[很常見] 1) 配置 ```sh [root@xxx ~]# cat bbs.conf server { listen 80; server_name www.etiantian.org bbs.org; rewrite ^/(.*) http://www.etiantian.org/$1 permanent; root html/bbs; index index.html index.htm; } ``` 訪問測試 ```sh [root@xxx ~]# curl -L etiantian.org curl: (47) Maximum (50) redirects followed ``` 顯示最多跳轉50次,已經跳轉了47次,放棄跳轉 說明進入了無限循環狀態 2) 錯誤原因 ```sh [root@xxx ~]# curl -Lv etiantian.org #顯示循環過程,顯示內容略,可以從顯示內容中,看出無限循環 ``` **原因分析:** 第一次訪問etiantia.org后,并要求跳轉至www.etiantian.org 第二次用www.etiantian.org訪問,不管前面是什么域名,再次匹配跳轉規則,被要求再次跳轉,所以進入了無限循環 ### 3. 正確配置 1) 方法1:使用2個虛擬服務器 ```sh [root@xxx ~]# cat www.conf server { listen 80; server_name etiantian.omg; rewrite ^/(.*) www.etiantian.omg/$1 permanent; } server { listen 80; server_name www.etiantian.omg; location / { root html/www; index index.html index.htm; } ``` 輸入etiantian.omg網址后,會永久重定向到www.etiantian.omg網址,且地址欄顯示跳轉后的地址 2) 方法2:用if語句判斷后,再進行地址重寫 ``` [root@xxx ~]# cat bbs.conf server { listen 80; server_name bbs.etiantian.org bbs.org; if ($host ~* "^etiantian.org$") { rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent; } root html/bbs; index index.html index.htm; } ```
                  <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>

                              哎呀哎呀视频在线观看