项目搭建体系为:腾讯云负载均衡+Nginx+Tomcat
多Tomcat下,由Nginx反向代理,找到对应的各个端口
某实例,如:
负载均衡配置为:80端口跳转后端8080,443端口跳转后端1443
Tomcat配置为,http端口8081,https端口1443
Nginx部分配置:
server {
listen 1443;
location / {
proxy_pass http://127.0.0.1:8081;
}
server_name www.ooo.com;
}
server {
listen 8080;
server_name localhost;
location / {
client_max_body_size 200m;
rewrite ^(.*)$ https://$host$1 permanent;
}
}
java项目redirect时,源代码:
@RequestMapping("abc")
public String getAbc(HttpServletRequest request, HttpSession session) {
String abc = request.getParameter("abc");
session.setAttribute("abc", abc);
return "redirect:/xxx/yyy.do";
}
这时浏览器会跳转为http:www.ooo.com:1443/xxx/yyy.do,无效,,,
解决一:
把java项目redirect改为:
@RequestMapping("abc")
public String getAbc(HttpServletRequest request, HttpSession session) {
String abc = request.getParameter("abc");
session.setAttribute("abc", abc);
return "redirect:https://www.ooo.com/xxx/yyy.do";
}
跳转成功,响应正常。
解决二:
负载均衡添加多一个监听端口,监听1443端口,跳转后端8071端口(自定义)。
Nginx添加多一个server,监听8071端口,然后rewrite强制https,如:
server {
listen 1443;
location / {
proxy_pass http://127.0.0.1:8081;
}
server_name www.ooo.com;
}
server {
listen 8071;
location / {
rewrite ^(.*)$ https://$host$1 permanent;
}
server_name www.ooo.com;
}
server {
listen 8080;
server_name localhost;
location / {
client_max_body_size 200m;
rewrite ^(.*)$ https://$host$1 permanent;
}
}