编辑 s3v3n

编辑 s3v3n

我有一个作为负载均衡器工作的 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任何信息被记录下来,不知道为什么会发生这种情况?

并且/etc/nginx/sites-available/default192.168.0.20

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;
    }
}

编辑 s3v3n

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

因此,如果我将/etc/nginx/sites-available/defaultLB 上的我的更改为:

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;
    }
}

它可以工作,但是这实际上并没有提供向集群添加更多内容的选项?

答案1

删除http { .. }并将它放在你的里面/etc/nginx/sites-available/default

upstream myapp1 {
    server 192.168.0.20;
}

server {
    listen 80;

    location / {
        proxy_pass http://myapp1;
    }
}

相关内容