0

I try to use NGINX inside a Docker Container with my Spring Boot App running both on localhost (later on a server).

NGINX should act as a reverse Proxy for the App running on port 5500. The App has a working Endpoint for GET localhost:5500/test

@RestController
public class TestController {
    @GetMapping(value = "/test")
    public String get(){
        return "test";
    }
}

With the use of NGINX i try to acces the Endpoint over localhost:8080/api/test, but i get always a 502 Bad Gateway.

NGINX config inside the Docker Container

worker_processes 1;
events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
            location / {
            root /usr/share/nginx/html;
            try_files $uri /index.html;
        }

         location /api {
            proxy_pass http://127.0.0.1:5500/test;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
        }
    }
}

NGINX works fine a serves on / a default index.html

NGINX Error

[error] 31#31: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: , request: "GET /api/test HTTP/1.1", upstream: "http://127.0.0.1:5500/api/test", host: "127.0.0.1:8080"

"GET /api/test HTTP/1.1" 502 157 "-" "PostmanRuntime/7.28.1"
  • I guess it's because you're using 127.0.0.1, which actually points to the container Nginx is running in. Try pointing it to your PC's IP, something like 192.168.1.XX – Cristian Jul 11 '21 at 21:56
  • "Localhost" could be any of several things here; if Nginx is in a container, `localhost` is the Nginx container. Have you read [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation, if the application is running in a second container, or [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) if the proxy is in a container but the application is not? – David Maze Jul 12 '21 at 00:55

0 Answers0