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

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

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

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

slb监听

#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请求。tomcat

因此无论用户用http仍是https最终的请求都是https的请求。
关键的一点是要让SLB的IP可以访问你后端nginx的端口,个人是81,不然SLB会检测端口异常。负载均衡

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

使用阿里云CDN加速域名能够设置,以下图加密

图片描述

相关文章
相关标签/搜索