nginx 配置如下:
server {
listen 443 default_server;
ssl on;
ssl_certificate /etc/nginx/domain.com.crt;
ssl_certificate_key /etc/nginx/domain.com.key;
return 444;
}
server {
listen 443;
server_name www.domain.com;
ssl on;
ssl_certificate /etc/nginx/domain.com.crt;
ssl_certificate_key /etc/nginx/domain.com.key;
location / {
proxy_pass http://myserver;
}
}
通过 https://www.domain.com 访问,正常
通过 https://ip 访问,返回444,也正常
通过 http://ip:443 访问,返回“400 Bad Request”,错误页面如下:
400 Bad Request,The plain HTTP request was sent to HTTPS port
问题是怎样自定义这个400错误页面?
这错误实际上是497错误。
要显示自定义错误,就要Catch 497错误。
具体方法有两个:
(1)跳转到主页: error_page 497 https://www.domain.com;
(2)显示自定义错误:
error_page 497 /497.html;
location = /497.html {
}
完整的代码是:
server {
listen 443 default_server;
root /var/www/html;
ssl on;
ssl_certificate /etc/nginx/domain.com.crt;
ssl_certificate_key /etc/nginx/domain.com.key;
location / {
return 444;
}
# Catch the error: "400 Bad Request,
# The plain HTTP request was sent to HTTPS port"
# 重定向497到https主页
# error_page 497 https://www.domain.com;
# 显示自定义497错误
error_page 497 /497.html;
location = /497.html {
}
}