Nginx实现负载均衡
confighttp {
upstream backend { server 127.0.0.1; server 127.0.0.2; } server { listen 80; server_name www.example.com;
location / { proxy_pass http://backend; } } }
|
upstream 使用域名
以下使用会出现 502 bad gateway 错误
confighttp {
upstream backend { server a.example.com; server b.example.com; }
server { listen 80; server_name www.example.com;
location / { proxy_pass http://backend; } } }
|
正确使用
confighttp {
upstream backend { server localhost:8081; server localhost:8082; }
server { listen 8081; location / { proxy_set_header HOST a.example.com; proxy_pass http://a.example.com; } }
server { listen 8082; location / { proxy_set_header HOST b.example.com; proxy_pass http://b.example.com; } } server { listen 80; server_name www.example.com;
location / { proxy_pass http://backend; } } }
|
DNS 修改
出现[emerg] host not found in upstream “a.example.com” 错误
尝试ping下域名
如果提示ping: unknown host a.example.com,则应该是dns解析域名失败,检查/etc/resolv.conf,发现没有配置域名服务器,增加域名服务器
nameserver 8.8.8.8 nameserver 8.8.4.4
|