nginx配置https及http访问,解决不同SessionId问题

最近公司项目需要和银行对接,传输协议必须是https,之前也有配过ssl证书,这次是踩坑了。废话不多说进入正题。

一、linux下载安装tomcat、nginx具体安装步骤后续会慢慢更新。

二、申请ssl证书(阿里云DV个人免费版为例)

  1. 下载申请好的 ssl 证书文件压缩包到本地并解压(这里是用的 pem 与 key 文件,文件名可以更改)。
  2. 在 nginx 安装目录conf下新建 cert 文件夹存放证书文件。
  3. 将阿里云上申请的ssl证书下载解压后上传到新建的cert文件夹(文件名最好不要修改)
  4. 修改nginx配置文件: vim nginx.conf
  5. 首先开启https默认端口443 按项目需求进行修改  
  6. 配置nginx负载均衡
  7.   upstream 负载均衡配置名1 {
                 server localhost:8080;
                 ip_hash;
               }
       upstream 负载均衡配置名2 {
                 server localhost:8081;
                 ip_hash;
               }
       upstream 负载均衡配置名3 {
                  server localhost:8082;
                  ip_hash;
               }
    

     

  8. # HTTPS server
        server {
            listen 443 ssl;
            server_name xxx.com; # 改为绑定证书的域名
            ssl on;
            ssl_certificate cert/3196179_xxx.com.pem; # 改为自己申请得到的 crt 文件的名称
            ssl_certificate_key cert/3196179_xxx.com.key; # 改为自己申请得到的 key 文件的名称
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
            ssl_prefer_server_ciphers on;
    
            location / {
                root   /usr/local/java/officialWeb/dist; #站点目录
                index index.html index.htm home.html;
            }
    
            location /访问名1 {
                 proxy_set_header  X-Forwarded-Proto https; #配置报文转发协议为https
                 proxy_set_header  X-Real-IP  $remote_addr;
                 proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header  Host $http_host;
                 proxy_set_header Cookie $http_cookie;
                  proxy_set_header Referer $http_referer;
                 proxy_redirect    off;
                 expires           off;
                 sendfile          off;
                 proxy_pass        http://负载均衡配置名1;
             }
    
             location /访问名2 {
                 proxy_set_header  X-Forwarded-Proto https; #配置报文转发协议为https
                 proxy_set_header  X-Real-IP  $remote_addr;
                 proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header  Host $http_host;
                 proxy_set_header Cookie $http_cookie;
                  proxy_set_header Referer $http_referer;
                 proxy_redirect    off; 
                 expires           off;
                 sendfile          off;
                 proxy_pass        http://负载均衡配置名2;
             }
    
           location /访问名3 {
                 proxy_set_header  X-Forwarded-Proto https; #配置报文转发协议为https
                 proxy_set_header  X-Real-IP  $remote_addr;
                 proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header  Host $http_host;
                 proxy_set_header Cookie $http_cookie;
                 proxy_set_header Referer $http_referer;
                 proxy_redirect    off;
                 expires           off;
                 sendfile          off;
                 proxy_pass        http://负载均衡配置名3;
            }
    
         }
    

     

  9. 配置http默认端口80 设置http访问重定向为https协议
  10.   server {
            listen       80;
            server_name  xxx.com;
            client_max_body_size 10M; #设置文件上传大小
            #rewrite ^(.*)$ https://$host$1 permanent;
            return   301 https://需要重定向的域名$request_uri;
       }
    

     

  11. 目前为止https已经配置完成重启nginx浏览器访问已经提示安全

    到这本以为大功告成可谁承想访问子项目时提示登陆失败,当时在想这用户名和密码没问题啊,怎么可能呢。这时又把本地项目跑起来登陆结果没有问题,然后在登陆的接口打印日志,重新打包上传,访问登陆日志打印Session信息为空。然后网上一顿好找,最后明白原来是http重定向https时出的错,这相当于两次不同的请求,每次请求tomcat都会创建一个SessionId返回浏览器,这就造成第二次访问获取不到第一次的SessionId。还需要修改tomcat的service.xml,在<Host>标签内新加下面这段标签即可解决重定向导致sessonId不同的问题

 

 <Valve className="org.apache.catalina.valves.RemoteIpValve" protocolHeader="X-Forwarded-Proto" />

参考:https://blog.csdn.net/xiao_a_gang/article/details/83014192

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值