如果缺少檔案夾,我想顯示自定義錯誤頁面。
我的目錄結構如下:
data
defaults
error.html
nothosted.html
example.com
misc
error.html
example.net
...
因此,當有人訪問其檔案夾不存在的 example.de 時,會顯示 defaults/nothosted.html。但如果該檔案夾存在,則應使用我的 @error 位置。
目前這是我的配置,它不起作用。
server_name ~^(\*\.)?(?<subdomain>[a-z\d][a-z\d-]*[a-z\d]\.)(?<domain>. ) $;
location / {
root /data/$subdomain$domain;
try_files $uri $uri/ @nothosted;
index index.html index.htm index.php;
}
location @nothosted {
root /data;
set $link /$subdomain$domain;
if (!-d $link) {
set $link /defaults/nothosted.html;
}
try_files $link @error;
}
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
location @error {
internal;
ssi on;
auth_basic off;
root /data;
try_files /$subdomain$domain/misc/error.html /$domain/misc/error.html /defaults/error.html =404;
}
我在 nginx 日志中得到的錯誤如下:
*1 directory index of "/data/" is forbidden
我嘗試連接到其檔案夾不存在的域時出現的錯誤是來自@error 位置的“403 禁止”。
權限應該是正確的(整個 /data/ 檔案夾歸 nginx 用戶所有)
編輯: 使用以下非托管位置時,一旦訪問,正確的鏈接就會顯示在網頁上。所以問題必須是 try_files 函式。
location @nothosted {
root /data;
set $link /$subdomain$domain;
if (!-d $document_root/$subdomain$domain) {
set $link /defaults/nothosted.html;
}
return 200 $link;
add_header Content-Type text/plain;
}
uj5u.com熱心網友回復:
這個問題無法以您嘗試解決的方式解決。這是某種初步的答案,我稍后會添加解釋。您可以嘗試以下方法:
if (!-d /data/$subdomain$domain) {
rewrite ^ /defaults/nothosted.html;
}
location / {
root /data/$subdomain$domain;
try_files $uri $uri/ =404;
index index.html index.htm index.php;
}
location = /defaults/nothosted.html {
internal;
root /data;
}
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
location @error {
internal;
ssi on;
auth_basic off;
root /data;
try_files /$subdomain$domain/misc/error.html /$domain/misc/error.html /defaults/error.html =404;
}
此解決方案有一個缺點 - /defaults/nothosted.htmlURI 不適用于任何托管站點。如果您發現它不可接受,您可以使用某種唯一的隨機字串而不是nothosted一個(并nothosted.html相應地重命名該檔案)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432650.html
上一篇:POST的NGINX代理通行證
