连接到上游客户端时不进行实时上游负载平衡

时间:2019-01-04 20:48:05

标签: nginx

我在本地网络上用两个docker apache实例设置了一个负载均衡器。我确认的两个节点都已启动,并且可以在单个端口8081和8082上查看,但是当尝试在apache中使用上游时,出现502错误的网关错误。

我尝试将/ etc / nginx目录的权限更改为nginx用户。我尝试重新启动apache实例和nginx服务。当我将server_name更改为localhost而不是服务器主机的IP地址时,可以从本地网络上的另一台计算机在Web浏览器上看到默认的nginx服务器页面。如果将其更改回IP地址,则会收到错误的网关错误。

我将我的default.conf符号链接到可用站点

    [root@www nginx]# vi sites-enabled/default.conf
    upstream containerapp {
        server 192.168.0.42:8081;
        server 192.168.0.42:8082;
     } 

      server {
        listen *:80;

        server_name localhost;
        index index.html index.htm index.php;

        access_log /var/log/nginx/localweb.log;
        error_log /var/log/nginx/localerr.log;

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

这是我收到的日志文件错误

    [root@www nginx]# cat /var/log/nginx/localerr.log
    referrer: "http://192.168.0.42/"
    2019/01/04 20:15:54 [error] 16310#0: *1 no live upstreams while 
    connecting 
    to upstream, client: 192.168.0.18, server: 192.168.0.42, request: 
    "GET / HTTP/1.1", upstream: "http://containerapp/", 
    host:"192.168.0.42"
    2019/01/04 20:15:54 [error] 16310#0: *1 no live upstreams while 
    connecting 
    to upstream, client: 192.168.0.18, server: 192.168.0.42, request: 
    "GET 
    /favicon.ico HTTP/1.1", upstream: "http://containerapp/favicon.ico", 
    host: 
    "192.168.0.42", referrer: "http://192.168.0.42/"

这就是我的nginx.conf的样子

 nginx.conf
        user nginx;
        worker_processes auto;
        error_log /var/log/nginx/error.log;
        pid /run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

    events {
        worker_connections 1024;
    }

    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] 
   "$request" '    
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d 
    directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #include /etc/nginx/conf.d/*.conf;
    server {
       listen       80 default_server;
       listen       [::]:80 default_server;
       server_name  _;
       root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

       location / {
       }

       error_page 404 /404.html;
          location = /40x.html {
       }

       error_page 500 502 503 504 /50x.html;
           location = /50x.html {
       }
    }

    include /etc/nginx/sites-enabled/*;
    }

我可以按http://192.168.0.42,它将把轮询直接引导到http://192.168.0.42:8081http://192.168.0.42:8082节点

2 个答案:

答案 0 :(得分:0)

我认为这是Apache实例响应缓慢的问题。

这会有所帮助:

location /{
  proxy_pass http://containerapp;
  proxy_read_timeout 5m;
}

答案 1 :(得分:0)

我发现解决方案与selinux有关。我在这里找到了另一条与此命令有关的帖子

setsebool -P httpd_can_network_connect 1

链接到原始问题,以防将来有人(13: Permission denied) while connecting to upstream:[nginx]

遇到此问题