我有一個簡單的網站,其中包含兩種語言的一些 PHP 檔案:主要語言和翻譯,比如說西班牙語。
如果有人嘗試加載不存在的 URL,我想為主要語言顯示 404 錯誤頁面,并為 /es 子目錄中不存在的 URL 顯示翻譯的 404 錯誤頁面。
這是我的組態檔:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
location / {
error_page 404 /404.php;
try_files $uri $uri/ @extensionless-php;
index index.php;
}
location /es {
error_page 404 /es/404.php;
try_files $uri $uri/ @extensionless-php;
index index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
它沒有按預期作業。當加載一個不存在的 URL(無論是在根目錄中還是在 /es 子目錄中)時,nginx 回傳標準的 404 頁面(顯示 404 Not Found 的頁面)。
如果我添加error_page 404 /404.php;服務器塊(外部位置塊),那么 example.com/foo 和 example.com/es/foo 將回傳未翻譯的 404 錯誤頁面。
我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
您對不存在的資源的任何請求最終都會出現在location ~ \.php$ { ... }您根本沒有error_page指令的塊中。如果您在server級別定義自定義錯誤頁面,則該位置會繼承該頁面。您可以定義兩個 PHP 處理程式(location塊的順序很重要!):
location ~ ^/es. \.php$ {
error_page 404 /es/404.php;
... # other content is the same as in the question
}
location ~ \.php$ {
error_page 404 /404.php;
... # other content is the same as in the question
}
或者更好地使用map像這樣的塊
map $uri $errpage {
~^/es /es/404.php;
default /404.php;
}
server {
...
error_page 404 $errpage;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/406916.html
標籤:
下一篇:Varnish503后端獲取失敗
