Nginx Reverse Proxy: Throwing 502 Bad Gateway
Asked Answered
P

3

12

I was following this tutorial to learn about Reverse Proxy https://www.youtube.com/watch?v=ZmH1L1QeNHk&t=227s

I'm running the docker image like this

sudo docker run -d --name nginx-base -p 80:80 nginx:latest

I was able to edit the default.conf Here is the file

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /test {
        proxy_pass http://localhost:8086/test;
    }

    location /home {
        proxy_pass http://localhost:3000;
    }

   location /home/auth {
        proxy_pass http://localhost:3000/auth;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
     
    

I get the Nginx Welcome screen, when I go to http://localhost/

But when I try to access http://localhost/test or http://localhost/home

enter image description here

Also, I'm able to access localhost:3000 and localhost:8086/test

Not sure why nginx is throwing 502, did I miss any configuration?

Provision answered 24/11, 2022 at 9:38 Comment(0)
B
10

Bad Gateway 502 is typically a sign that your destination server is not responding. I assume your other services are Docker container as well? If that is the case you could try to change localhost to their containername and use Docker-dns. But you need to put all containers in the same network for that to work.

Another thing to try is to reverse your order of routes. If I remember correctly nginx takes the first matching route. In your case / matches every other route so every request is directed to your nginx. But your nginx can't handle /test or /home.

I hope this will help you to find the problem.

Bohon answered 24/11, 2022 at 10:43 Comment(5)
Would it be an issue if other services are not a Docker container? I tried both and getting the same resultProvision
also, Thanks for the help, what does try to change localhost to their containername exactly mean?Provision
At the moment you do proxy_pass http://localhost:8086/home you could change it to proxy_pass http://mysecondserver:8086/home instead.Bohon
Thanks, but I wanted to use the same domain for /home and /test, that was the purpose of using reverse proxy.Provision
@Provision that will not change the domain from the user perspective! It is just the way your reverse proxy will forward the requests internally.Bohon
L
2

I was encountering a "502 Bad Gateway" error with my NGINX server in a Kubernetes pod:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>

To troubleshoot, I first checked the pod status, which appeared to be running without issues. Then, I executed into the pod and kept the logs open to monitor for any errors. Here's what I found in the logs:

[1] [ERROR] Worker (pid:80) was sent SIGKILL! Perhaps out of memory?

Based on this error, I concluded that the issue might be related to insufficient CPU or memory resources. To address this, I modified my Kubernetes deployment to allocate more CPU resources and increased the memory limits as follows:

resources:
  requests:    
    memory: "750Mi" 
    cpu: "750m"  
  limits:
    memory: "2000Mi"
    cpu: "1000m"

After applying these changes, the issue was resolved.


Laddy answered 2/4 at 13:59 Comment(0)
T
0

In my case it was a firewall issue. I disabled the firewall and it worked.

I got a clue by curling the docker IP, which properly output the html homepage.

sudo ip addr show docker0
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:a9:60:8e:f4 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

curl 172.17.0.1:5870

I had the app running on port 5870, and before disabling firewall:

curl 127.0.0.1:5870
curl: (56) Recv failure: Connection reset by peer

curl 0.0.0.0:5870
curl: (56) Recv failure: Connection reset by peer

After: I get the HTML page output.

Thermal answered 29/11, 2023 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.