標籤: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規則問題。