背景描述:原来的项目前后台都是http,现在某个服务要求前台必须使用https;

方案1:前台部署在https里,后面代码修改;但是微服务架构,后台工作量太大;

方案2:前台部署在https里,后台代码不修改;

             此时需要完成三个问题:

问题1:前台如何部署在https中;tomcat或者nginx;优先使用nginx;

步骤1:生成nginx的对应证书;使用域名证书,或者研发环境证书都可以;

参考文档:安装openshell,生成nginx证书;

Windows/Linux 生成Nginx证书【Nginx代理https】_crt pem key nginx代理-CSDN博客

步骤2:

1.1,把证书,前端代码都放在nginx中

1.2,配置证书和前台路径

案例中9909是前端https端口,9903是后端https端口,9901是后端http请求

1.3,启动nginx,通过https在浏览器访问前端;

1.3.1,nginx日志,在logs/error.log中;【假如启动报错,这里面都能看到错误信息】

问题2:https的前端,对应的必须是https的后端;需要把https转换为http【同样用nginx,可以使用同一个nginx】

    server {
        listen 9903 ssl;
        server_name  192.168.100.104;

        ssl_certificate      E:/nginx-1.16.1a/cert/server.pem;
        ssl_certificate_key  E:/nginx-1.16.1a/cert/server.key;
         location / {
                proxy_pass http://218.96.105.54:9901;
        }

配置后台的端口和证书

问题3:如何处理前后端端口不一样的跨域问题【核心】

启动nginx;在浏览器使用https访问前台,并登录系统;查看浏览器控制台是否报错;

常见错误信息

1、重复处理跨域【需要去掉一个跨域】

Access to XMLHttpRequest at 'https://192.168.100.104:9989/GateWayPC/LoginAndSetting/login' from origin 'https://192.168.100.104:9905' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://192.168.100.104:9905, http://192.168.100.104:9901', but only one is allowed.

解决方案:去掉多余的跨域处理

2、没有处理跨院【需要增加跨域处理】

Access to XMLHttpRequest at 'https://192.168.100.104:9989/jf/PSysyteminit/selectPSysyteminitByNameAndCode?code=A59' from origin 'https://192.168.100.104:9905' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

解决方案:增加跨域处理

3、其他错误

nginx Request header field uuid is not allowed by Access-Control-Allow-Headers in preflight response

解决方案add_header 'Access-Control-Allow-Headers' 'uuid,

最终nginx配置:

1,证书;2,前端https端口和后端https端口,后端http端口;3,如何处理跨域;

经验总结:

1,最开始使用nginx只能做一些简单的配置,这么复杂的头一次遇到;

1.1,遇到问题,先网络找答案;你遇到的问题,大家都遇到过;注意答案可以参考,但不要一模一样照抄;

1.2,一定要详细查看每种方案的报错信息。。。。。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐