2016-03-15 42 views
0

是否有可能安裝nginx的配置就像下面的工作:Nginx的錯誤頁面外根

我的網站將駐留在根路徑,即:在/ var/www/html等

是否有可能到位例如錯誤頁面,如401一樣,即一些其他的路徑內404等:/一些/其它/路徑

我設法通過使用這即得到這個工作(加載錯誤頁面外的網站根目錄):

error_page 404 /custom_404.html; 
location = /custom_404.html { 
    root /path/to/my/root/for/error/pages 
    internal; 
} 

現在這是問題...我的404頁面可能包含一些圖像和CSS文件,當它顯示我的404頁面時,CSS和圖像根本不會加載(如果我將錯誤頁面的CSS和圖像放入/ var/www/html等)。

我想保留錯誤頁面內的錯誤頁面的CSS和圖像,其中錯誤頁面是...任何人都可以指出我應該如何配置它,是否有可能在某處,因爲我正在尋找有關它的日子。

非常感謝。

回答

0

說你的custom_404.html看起來是這樣的:

<html> 
<head><title>Oops!</title></head> 
<body> 
    <h1>404</h1> 
    <img src="oops.gif" /> 
</body> 
</html> 

將其更改爲:

<html> 
<head><title>Oops!</title></head> 
<body> 
    <h1>404</h1> 
    <img src="/error/oops.gif" /> 
</body> 
</html> 

,然後在你的.conf文件:

error_page 404 /error/custom_404.html; 
location /error/ { 
    root /path/to/error/files; 
} 
0

你可以有一個像這樣的回退位置:

location/{ 
    root /normal/path/to/html; 
    try_files $uri @fallback; 
} 

location @fallback { 
    root /path/to/error/files; 
} 
0

您的問題是internal規則,該規則限制只能從內部請求訪問的位置。爲了保持internal Nginx的規則,你可以做以下的事情:

  • 使用inline CSS
  • 將您的圖像Base64

這樣做後,你可以嵌入生成的Base64編碼字符串轉換成background-image CSS規則如下:

background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEADI.....==) 

您可以使用字符串<img>個標籤爲好,只是把它傳遞給src屬性如下:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEADI.....==" /> 

否則,如果你仍然想使用外部的內容,你將需要刪除該定位的internal規則,致力於爲錯誤頁面在Nginx配置中。