apache與nginx的rewrite規則問題。

來源:互聯網
上載者:User

標籤:style   blog   http   color   ar   使用   sp   for   檔案   

儘管apache與nginx的rewirte模組都聲明基於perl正則,但實際使用中還是有所不同,特別是nginx。

在apache中,在server全域配置的httpd.conf檔案中使用全域變數%{…}判斷並不穩妥,當我使用 %{REQUEST_FILENAME}判斷請求是否為檔案時就出錯了。無論請求是否匹配檔案,它都返回否。究其原因在手冊中都有提示:

If used in per-server context (i.e., before the request is mapped to the filesystem) SCRIPT_FILENAME and REQUEST_FILENAME cannot contain the full local filesystem path since the path is unknown at this stage of processing. Both variables will initially contain the value of REQUEST_URI in that case. In order to obtain the full local filesystem path of the request in per-server context, use an URL-based look-ahead %{LA-U:REQUEST_FILENAME} to determine the final value of REQUEST_FILENAME.

它說,如果在server配置環境使用,SCRIPT_FILENAME和REQUEST_FILENAME變數並不能包含整個檔案系統路徑,因為這個處理階段,這些路徑還未知。這時,這兩變數只是REQUEST_URI變數的初始值。為了擷取系統路徑,可以使用一個基於URL的變數%{LA-U:REQUEST_FILENAME}在前面做檢測。

 

nginx有個非常奇怪的現象,在正則中點號(.)如果沒有跟正則語意的語句並聯在一起,它就是單純的點號,如果你使用\轉義反而會出錯,因為它把\.看成是兩個字元。例如:

rewrite /dd\.html /abc.php last;

替換的部分的點號並不需要轉義。但如果是這樣:

rewrite /dd\.html /.*\.php last;

就需要,不知其它類型是否這樣?!

apache與nginx的rewrite規則問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.