Experimental environment: Two tomcats are used for load balancing on nginx, and the load code is as follows:
upstream tomcat_local {
server 192.168.8.186:8080 weight=1;
server 192.168.8.186:8081 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://tomcat_local;
# proxy_set_header Host $host:$server_port;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header REMOTE-HOST $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Access http://192.168.8.186/
will report an error! ! !
Error content:
Error reason:tomcat_local
There is an underscore in the domain name _
, and if the Host of proxy_set_header is not set, the value corresponding to proxy_pass will be used directly, which is tomcat_local. But the underscore is illegal for the domain name, so an error 400 error request will be reported.
Solution:
① Method 1: Remove the underscore, for example, write the domain name as tomcatlocal
② Method 2: Modify the reverse proxy configuration as follows:
location / {
root html;
proxy_pass http://tomcat_local;
proxy_set_header Host $host:$server_port; # nginx服务器的主机名和端口号,不使用tomcat_local
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}