Featured image of post nginx 负载均衡代理 tomcat 站点 400 错误

nginx 负载均衡代理 tomcat 站点 400 错误

现象

今天在做nginx 负载均衡实现动静分离实验的时候,终端curl命令可以正常访问动态请求打到指定的后端服务器,但是浏览器的请求返回400

原因

经过排查后发现是nginx 转发给后端服务器的名字不支持下划线

具体到 The character [_] is never valid in a domain name. 错误,这是 Tomcat 8.5.6(或更早一点)之后引入的更严格的 Host 头解析机制的结果。

修改

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost ~]# cat /etc/nginx/nginx.conf 
user  nginx;
worker_processes  auto;
 
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
http {
    upstream frontend_server {
      server 10.0.0.117:8080 max_fails=3 fail_timeout=5s;
    }
    upstream file_server {
      server 10.0.0.118:8080 max_fails=3 fail_timeout=5s;
    }
    upstream backend-server {
      server 10.0.0.119:8080 max_fails=3 fail_timeout=5s;
    }
 
    server {
        listen       9090;
 
        location / {
           proxy_pass  http://frontend_server;
        }
 
        location ~* \.(css|js|jpg|png|gif)$ {
           proxy_pass  http://file_server;
        }
 
        location ~* \.jsp$ {
            proxy_pass http://backend-server;
        }
    }
}

将原 backend_server 改成 backend-server

systemctl reload nginx 即可

使用 Hugo 构建
主题 StackJimmy 设计