通过 url 参数根据区域设置在 nginx 上设置自定义 404 错误页面
Set custom 404 error page on nginx based on locale through url parameter
我是 运行 GNU/Linux
OS 上 Nginx
的最新稳定版本,并且拥有以下虚拟主机,我正在尝试 setup custom localized 404 error pages avoiding if
但我总是陷入重定向循环。
到目前为止,我只考虑以下区域设置 es
、en
和 ca
,我将它们作为 URL 参数获取,始终在域旁边. expected URLs are
类似于:
http://www.example.com/ca
http://www.example.com/ca/home
http://www.example.com/en
http://www.example.com/en/contact
这是我的 nginx 服务器块:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html/example_com;
# By default, show Castilian index
index es/index.html;
access_log /var/log/nginx/example_com.access.log main;
error_log /var/log/nginx/example_com.error.log error;
#Remove HTML Extension
rewrite ^(/.*)\.html(\?.*)?$ permanent;
# Remove trailing slash
rewrite ^/(.*)/$ / permanent;
# Make sure Nginx knows what files to look for, and for that we use the try_files directive.
# Look for a file with the current $uri and an .html extension, and if no file exists,
# check for a directory with that name and serve the index. Otherwise, render a 404 error.
try_files $uri/index.html $uri.html $uri/ $uri =404;
location = /form/contact.php {
deny all;
return 404;
}
# Custom error pages
set $error404 /en/404.html;
location /ca {
set $error404 /ca/404.html;
}
error_page 404 =404 $error404;
location = /form/contact {
try_files $uri.php =404;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ~ /(config) {
deny all;
return 404;
}
}
你不能那样使用位置块,你会破坏所有其他位置块。有关详细信息,请参阅 how nginx
processes a request。
有一个更简单的方法。使用命名位置处理 404 响应,只需将原始 URI 重写为所需的错误页面即可。
例如:
error_page 404 @error404;
location @error404 {
rewrite ^/(en|es|ca) //404.html last;
rewrite ^ /en/404.html last;
}
有关更多信息,请参阅 this document。
只需在服务器内部的配置中添加下面这行错误页面重定向即可。
error_page 404 https://www.example.com/404-page.html;
这解决了我的问题
我是 运行 GNU/Linux
OS 上 Nginx
的最新稳定版本,并且拥有以下虚拟主机,我正在尝试 setup custom localized 404 error pages avoiding if
但我总是陷入重定向循环。
到目前为止,我只考虑以下区域设置 es
、en
和 ca
,我将它们作为 URL 参数获取,始终在域旁边. expected URLs are
类似于:
http://www.example.com/ca
http://www.example.com/ca/home
http://www.example.com/en
http://www.example.com/en/contact
这是我的 nginx 服务器块:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html/example_com;
# By default, show Castilian index
index es/index.html;
access_log /var/log/nginx/example_com.access.log main;
error_log /var/log/nginx/example_com.error.log error;
#Remove HTML Extension
rewrite ^(/.*)\.html(\?.*)?$ permanent;
# Remove trailing slash
rewrite ^/(.*)/$ / permanent;
# Make sure Nginx knows what files to look for, and for that we use the try_files directive.
# Look for a file with the current $uri and an .html extension, and if no file exists,
# check for a directory with that name and serve the index. Otherwise, render a 404 error.
try_files $uri/index.html $uri.html $uri/ $uri =404;
location = /form/contact.php {
deny all;
return 404;
}
# Custom error pages
set $error404 /en/404.html;
location /ca {
set $error404 /ca/404.html;
}
error_page 404 =404 $error404;
location = /form/contact {
try_files $uri.php =404;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ~ /(config) {
deny all;
return 404;
}
}
你不能那样使用位置块,你会破坏所有其他位置块。有关详细信息,请参阅 how nginx
processes a request。
有一个更简单的方法。使用命名位置处理 404 响应,只需将原始 URI 重写为所需的错误页面即可。
例如:
error_page 404 @error404;
location @error404 {
rewrite ^/(en|es|ca) //404.html last;
rewrite ^ /en/404.html last;
}
有关更多信息,请参阅 this document。
只需在服务器内部的配置中添加下面这行错误页面重定向即可。
error_page 404 https://www.example.com/404-page.html;
这解决了我的问题