2016-10-02 115 views
0

部分:NGINX:在子目錄中搜索文件nginx的配置的

location =/{ 
     root www/html; 
     try_files $uri $uri/ $uri.html; 

    } 

    location ~* \.(jpg|jpeg|png)$ { 
     root www/resources/img; 
     try_files $uri $uri/; 
     #autoindex   on; 
     expires 1y; 
     add_header Cache-Control public; 
    } 

    location ~* \.(css)$ { 
     root www/resources/css; 
     expires 1y; 
     add_header Cache-Control public; 
    } 

    location ~* \.(js)$ { 
     root www/resources/js; 
     expires 1y; 
     add_header Cache-Control public; 
    } 

在目錄www/resources/img我有1個圖像文件1.jpg和1子文件夾包含另一個文件2.jpg。所以,如果我的要求像localhost/1.jpg我得到我的形象,但如果我要求localhost/2.jpg它返回404找不到。如何設置nginx來搜索子文件夾中的文件?

+0

localhost/1.jpg - > www/resources/img/1.jpg,但是localhost/2.jpg - > www/resources/img/subfolder/2.jpg? –

回答

0

嘗試改變下面的代碼塊,因爲如果在根沒有找到該文件,找到它在/子/ $ URI,

location ~* \.(jpg|jpeg|png)$ { 
    root www/resources/img; 
    try_files $uri /subfolder/$uri $uri/; 
    #autoindex   on; 
    expires 1y; 
    add_header Cache-Control public; 
} 

切換您的error_log上,看到正在訪問哪些文件,你也可以從他們那裏糾正。