Nginx非80/443端口的http强制跳转https

在《 Nginx如何配置http强制跳转https 》的博文中,我们已经介绍了,在默认80端口的状态下http如何跳转到默认443端口https,但是很多时候,我们nginx监听的并不是默认的80/443端口,这时候如何设置,例如,我的实例需要https访问8088端口,每次访问又不想手动输入https,想在默认输入test.orcy.net.cn:8088自动https://test.orcy.net.cn:8088,则nginx的配置文件需要如下设置
server {
listen 8088 ssl;
server_name test.orcy.net.cn;
ssl on;
ssl_certificate /usr/local/nginx/key/server.crt;
ssl_certificate_key /usr/local/nginx/key/server.key;
ssl_session_timeout 60m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
error_page 497 301 https://$http_host$request_uri;
}
重点是,错误时返回301转成https :
error_page 497 301 https://$http_host$request_uri;
实例测试成功,如下图:
