2017-02-19 39 views
0

我有一個服務Spring MVC應用程序的Tomcat服務器。Spring MVC相當於nginx的try_files

我想實現一個靜態的servlet一定的路徑,我想它在莊園的行爲等同於nginx的try_files指令:

... 
root /my/path/to/app 

location /app { 
    try_files $uri $uri/ /index.html; 
} 
... 

對於那些不熟悉nginx的:

我想讓servlet將/app路徑直接映射到/webapp/app目錄。如果它在與請求匹配的目錄中找到靜態文件,那麼返回該內容。否則返回/webapp/app/index.html文件。

例如,如果我的目錄看起來是這樣的:

/webapp 
    /app 
     index.html 
     existing-file.js 
     /sub-dir 
      file.js 

然後...

mydomain.com/app      returns /webapp/app/index.html 
mydomain.com/app/index.html   returns /webapp/app/index.html 
mydomain.com/app/non-existant-file returns /webapp/app/index.html 
mydomain.com/app/existing-file.js returns /webapp/app/existing-file.js 
mydomain.com/app/sub-dir/file.html returns /webapp/sub-dir/file.html 

回答

0

本地路徑是通過連接root參數和URI構造。因此,/app已經在URI中,並且不會出現在root中。從你的問題來看,你是否對root有正確的價值並不清楚。如果這與其他location塊的根衝突,則root語句可以在未修改的此location塊內移動。有關更多信息,請參閱this document

try_files語句的最後一個元素是默認操作(URI或響應代碼)。在你的情況下,你需要你的index.html文件的URI,這是/app/index.html。有關更多信息,請參見this document

root /webapp; 

location /app { 
    try_files $uri $uri/ /app/index.html; 
}