http项目改为/支持https的方案、无需修改后台代码

背景描述:原来的项目前后台都是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,一定要详细查看每种方案的报错信息。。。。。

相关推荐
wearegogog1238 小时前
MySQL中实施排序(sorting)及分组(grouping)操作
数据库·mysql
2301_803554528 小时前
MySQL 主从读写分离架构
数据库·mysql·架构
正在走向自律9 小时前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
没有bug.的程序员9 小时前
Redis 内存管理机制:深度解析与性能优化实践
java·数据库·redis·性能优化·内存管理机制
小蒜学长9 小时前
基于SpringBoot+Vue的健身房管理系统的设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
失散139 小时前
分布式专题——2 深入理解Redis线程模型
java·数据库·redis·分布式·架构
DemonAvenger9 小时前
数据库迁移实战:最小化停机时间的方法与经验分享
数据库·sql·性能优化
xhbh66610 小时前
Python操作MySQL的两种姿势:原生SQL与ORM框架SQLAlchemy详解
数据库·oracle
CookieCrusher13 小时前
数据泄露危机逼近:五款电脑加密软件为企业筑起安全防线
运维·数据库·windows·安全·文件加密·数据防泄漏·dlp
这周也會开心14 小时前
SQL-窗口函数
数据库·sql