学习nginx的负载均衡配置,应用服务器是tomcat,都部署在同一台电脑上面,配置文件中的重点部分如下:
upstream localhost {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /Test {
proxy_pass http://localhost;
}
}
当upstream 中server地址为127.0.0.1的时候工作正常。
当我把upstream 中的127.0.0.1换为localhost的话,发现工作不正常,会出现超时:
upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /Test/testServlet HTTP/1.1", upstream: "http://[::1]:8081/Test/testServlet", host: "localhost"
这是为什么呢?希望朋友们指点。
server_name配置成localhost有问题吧,不会和本地dns冲突吗?
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name test.com;
location / {
root html;
index index.html index.htm;
}
location /Test {
proxy_pass http://server1;
}
}
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server1;
}
改成这样试下