2013-01-07 44 views
0

我在更改文件結構。 以前在網站根級別可用的某些網址現在已移至子文件夾。在nginx的子文件夾中查找文件

所以我有一個$uri。 I try_files $uri @check_subfolder; 如果文件(普通的.html文件)仍然存在於根級別,或者它的url編寫正確,那麼try_files $uri就像它應該那樣工作。 但是如果文件移動到子文件夾,然後我進入

location @check_subfolder { 
} 

如果我在這裏只是做一個重定向rewrite ^(.*) $scheme://$host/articles$1 permanent;然後一切依然完美。 但我首先要檢查是否真的存在於/articles子文件夾中。 所以我做

location @check_subfolder { 
    if (-f "/articles${uri}") { 
     rewrite ^(.*) $scheme://$host/articles$1 permanent; 
    } 
} 

它只是跌倒到錯誤404。重定向不會發生。沒有if重定向工作。所以很顯然,我如何制定條件的方式存在問題。

if (-f "/articles${uri}")不起作用。 if (-f "/articles${request_filename}")不起作用。 if (-f "/articles/${request_filename}")不起作用。 if (-f /articles/$request_filename)不起作用。 if (-f /articles/$uri)不起作用。

我明顯寫錯了連接。

回答

1

我的答案是,我不認爲-f在文件系統中尋找文件,而不是網頁,所以我不得不包括$document_root。 所以現在它可以像

location @check_subfolders { 
    if (-f $document_root/articles/$uri) { 
     rewrite ^(.*) $scheme://$host:8090/articles$1 permanent; 
    } 
}