阿里云SLB配置http跳转https

来源:互联网 发布:linux安装windows软件 编辑:程序博客网 时间:2024/06/05 18:42

1、在ECS中安装Nginx,可以参考我的另一篇文章Linux安装Nginx
2、 nginx配置server参考

server {
listen 80;
server_name xx.domain.com xx.domain.com;
rewrite ^/(.*)https://xx.domain.com/1 permanent;

   # access_log  logs/hiracer.com.80.log  access;}

3、阿里云SLB https配置参考
SLB配置

4、配置思路:
这样用户访问的流程是这样的:
1.如果是 https 的请求直接由 443 端口转到后端我的真实网站 8080 端口获取内容,SSL 数据加密的任务交给 SLB 处理。
2.如果是 http 的请求从SLB的 80 端口转至后端的 80 端口,由 Apache 重写成 https URL,转至 SLB 的 443 端口,对应流程1。
这样不管用户用 http还是 https 最终的请求都是 https 的请求。

5、ngnix 如此配置之后,实际遇到了一个问题,就是以接口形式post请过来,强制跳转之后会变成GET请求,同时请求数据会丢失,因此,我对请求路径做了不同处理

  • 页面请求强制跳转https
  • 接口请求不做强制跳转,只做代理
  • server {
    listen 80;
    server_name xx.domain.com xx.domain.com;

    location = / {
    rewrite ^/(.*)https://xx.domain.com/1 permanent;
    }

    location ^~ /h5/ {
    rewrite ^/(.*)https://xx.domain.com/1 permanent;
    }

    location ^~ /admin/ {
    rewrite ^/(.*)https://xx.domain.com/1 permanent;
    }

    location / {
    proxy_pass http://localhost:8080;
    }

}

原创粉丝点击