阿里云负载均衡配置http跳转https

环境:
阿里云两台ECS,负载均衡,nginx,tomcat

后端nginx:
配置server端口为 :8081,不用配置SSL,将SSL证书交给SLB处理。

SLB配置
配置两个监听,分别是:
前端80端口,后端服务81端口
前端443端口监听后端80端口
如下图:

《阿里云负载均衡配置http跳转https》

#nginx负载
upstream duweixin.net{
        server localhost:8081;
        server 10.17.232.2:8087;#另一台负载
}
server {
        listen       80;
        root   /data/www/h5/;
        index  index.html index.htm;
        server_name  duweixin.net www.duweixin.net;

        location / {
            proxy_pass http://duweixin.net;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
        }

}
#前端如果输入端是 http://duweixin.net,81端口,强制跳转到 https://duweixin.net
server {
        listen       81;
        return 301 https://duweixin.net $request_uri;
}

总结

1.如果是https的请求直接由443端口处理,SSL数据加密的任务交给SLB处理。
2.如果是http的请求从SLB的80端口转至后端的81端口,由81端口重写成https请求。

所以不管用户用http还是https最终的请求都是https的请求。
关键的一点是要让SLB的IP能够访问你后端nginx的端口,我的是81,否则SLB会检测端口异常。

补充:
最近发现还有另一种特别简单的办法

使用阿里云CDN加速域名可以设置,如下图

《阿里云负载均衡配置http跳转https》

    原文作者:HTTP
    原文地址: https://segmentfault.com/a/1190000005136031
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞