Search code examples
nginxload-balancing

Load balancer on Nginx give 502 Bad Gateway


I'm trying to configure Nginx server as a load balancer. I set up VM with CentOS 7. I disable Firewall (for the sake of the test), install Nginx using yum (custom .repo). I'm running my 3 SpringBoot restApi Apps on ports 8081, 8082 and 8083 and start Nginx but when I try to connect with load balancer I get 502 Bad Gateway (on VM host machine and on VM machine also).I can get a response from each of App but not from load balancer.

My conf file :

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"

Solution

  • I found what was the issue. It turns out my issue was due to SELinux. This solves the problem:

    setsebool -P httpd_can_network_connect 1
    

    I hope it will help someone, it took me some time to find it.