【问题标题】:Load balancer on Nginx give 502 Bad GatewayNginx 上的负载均衡器给出 502 Bad Gateway
【发布时间】:2018-09-10 21:11:52
【问题描述】:

我正在尝试将 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"

【问题讨论】:

    标签: nginx load-balancing


    【解决方案1】:

    我发现了问题所在。原来我的问题是由于 SELinux。 这样就解决了问题:

    setsebool -P httpd_can_network_connect 1
    

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

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-04-22
      • 2014-03-27
      • 2020-10-29
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多