Nginx上的负载均衡器给出了502 Bad Gateway

时间:2018-04-01 12:14:48

标签: nginx load-balancing

我试图将Nginx服务器配置为负载均衡器。我使用CentOS 7设置了VM。我禁用了防火墙(为了测试),使用yum(自定义.repo)安装Nginx。我在端口8081,8082和8083上运行我的3个SpringBoot restApi应用程序并启动Nginx,但是当我尝试连接负载均衡器时,我得到502 Bad Gateway(在VM主机上和VM机器上)。我可以得到来自每个应用程序但不来自负载均衡器的响应。

我的配置文件:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {

upstream test1 {
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
    server 127.0.0.1:8083;
}

server {

    listen 8090;
    access_log /var/log/nginx/http_redirect.log;
    location / {

             proxy_pass http://test1;
                }
}

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

http_redirect.log:

192.168.70.1 - - [01/Apr/2018:08:10:02 -0400] "GET /favicon.ico HTTP/1.1" 502 575 "http://192.168.70.4:8090/api/prime/100" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
192.168.70.1 - - [01/Apr/2018:08:10:03 -0400] "GET /api/prime/100 HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
192.168.70.1 - - [01/Apr/2018:08:10:04 -0400] "GET /api/prime/100 HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
192.168.70.1 - - [01/Apr/2018:08:10:04 -0400] "GET /favicon.ico HTTP/1.1" 502 575 "http://192.168.70.4:8090/api/prime/100" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

error.log中

2018/04/01 06:37:16 [crit] 2549#2549: *10 connect() to 127.0.0.1:8083 failed (13: Permission denied) while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8083/", host: "192.168.70.4"
2018/04/01 06:37:16 [warn] 2549#2549: *10 upstream server temporarily disabled while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8083/", host: "192.168.70.4"
2018/04/01 06:37:16 [crit] 2549#2549: *10 connect() to 127.0.0.1:8081 failed (13: Permission denied) while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "192.168.70.4"
2018/04/01 06:37:16 [warn] 2549#2549: *10 upstream server temporarily disabled while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "192.168.70.4"
2018/04/01 06:37:16 [crit] 2549#2549: *10 connect() to 127.0.0.1:8082 failed (13: Permission denied) while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8082/", host: "192.168.70.4"
2018/04/01 06:37:16 [warn] 2549#2549: *10 upstream server temporarily disabled while connecting to upstream, client: 192.168.70.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8082/", host: "192.168.70.4"

1 个答案:

答案 0 :(得分:1)

我发现了什么问题。事实证明我的问题是由于SELinux。 这解决了这个问题:

setsebool -P httpd_can_network_connect 1

我希望它会帮助某人,我花了一些时间才找到它。 参考: http://blog.frag-gustav.de/2013/07/21/nginx-selinux-me-mad/