我有一个作为负载平衡器的Ubuntu系统, /etc/nginx/sites-available/default如下所示:
http { upstream myapp1 { server 192.168.0.20:80; } server { listen 80; location / { proxy_pass http://myapp1; } } }
在IP地址为192.168.0.20的设备上,我设置了一个命令来监听所有的80端口连接
sudo tcpdump -n -tttt -i eth0 port 80
但是当通过端口80访问负载均衡器时, 192.168.0.20没有任何提示,为什么会发生这种情况呢?
而192.168.0.20的/etc/nginx/sites-available/default :
server { listen 80 default_server; #listen [::]:80 default_server ipv6only=on; root /var/www; index index.php index.html index.htm; server_name 192.168.0.20; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
这是curl -v 192.168.0.20在LB上运行时返回的内容:
* Rebuilt URL to: 192.168.0.20/ * Hostname was NOT found in DNS cache * Trying 192.168.0.20... * Connected to 192.168.0.20 (192.168.0.20) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.35.0 > Host: 192.168.0.20 > Accept: */* > < HTTP/1.1 200 OK * Server nginx/1.2.1 is not blacklisted < Server: nginx/1.2.1 < Date: Fri, 13 Mar 2015 14:26:13 GMT < Content-Type: text/html < Content-Length: 344 < Last-Modified: Fri, 13 Mar 2015 14:25:58 GMT < Connection: keep-alive < Accept-Ranges: bytes < <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Load balancing set up!</title> </head> <body> Success! You have set it up!! </body> </html> * Connection #0 to host 192.168.0.20 left intact
所以如果我把LB上的/etc/nginx/sites-available/default改为:
server { listen 80; server_name 192.168.0.20; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://192.168.0.20; } }
它的工作原理,但是这不是真的给select添加更多的集群?
删除http { .. }然后在你的/etc/nginx/sites-available/default :
upstream myapp1 { server 192.168.0.20; } server { listen 80; location / { proxy_pass http://myapp1; } }