nginx 禁止直接访问 http 端口

发布于 2025-05-07
对于家宽公网 ip 来说,没有标准的 80, 443 端口,为了安全考虑对外使用一个 nginx 作为出口,并配置 https 协议使用一个端口,不配置 http,这时直接访问 https://example.com:port 会提示:400 The plain HTTP request was sent to …

对于家宽公网 ip 来说,没有标准的 80, 443 端口,为了安全考虑对外使用一个 nginx 作为出口,并配置 https 协议使用一个端口,不配置 http,这时直接访问 https://example.com:port 会提示:400 The plain HTTP request was sent to HTTPS port

可我不想让用户看到这个提示来知道可以切换为 https 访问,解决办法为在 nginx 的 server 代码块中配置:

error_page 497 =444 @close;

location @close {
   return 444;
}