Nginx local proxy with docker results in 502 Bad Gateway for next.js

Viewed 22

I want to use nginx locally for a proxy without having to use cors.

My nginx.conf looks like this:

http {
  
  server {
    listen 0.0.0.0:4000;
    server_name local;

    location / {
      proxy_pass http://host.docker.internal:3001;
    }

    location /api {
      proxy_pass http://host.docker.internal:3000;
    }
  }

}

I am running my api (nest.js) and frontend (next.js) in wsl2 directly (not in a container). My api endpoint is accessible via nginx but the frontend (/) is not accessible and returns Connection refused:

Inside the nginx container:

curl http://192.168.65.2:3000 (this is my api works)

curl http://192.168.65.2:3001 (this is my frontend returns connection refused)

access and error logs of nginx:

2022/09/21 20:39:58 [error] 24#24: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: local, request: "GET / HTTP/1.1", upstream: "http://192.168.65.2:3001/", host: "localhost:4000"
172.20.0.1 - - [21/Sep/2022:20:39:58 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
2022/09/21 20:40:00 [error] 24#24: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: local, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.65.2:3001/favicon.ico", host: "localhost:4000", referrer: "http://localhost:4000/"
172.20.0.1 - - [21/Sep/2022:20:40:00 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
2022/09/21 20:40:02 [error] 24#24: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: local, request: "GET / HTTP/1.1", upstream: "http://192.168.65.2:3001/", host: "localhost:4000"
172.20.0.1 - - [21/Sep/2022:20:40:02 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
172.20.0.1 - - [21/Sep/2022:20:40:04 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost:4000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
2022/09/21 20:40:04 [error] 24#24: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: local, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.65.2:3001/favicon.ico", host: "localhost:4000", referrer: "http://localhost:4000/"
0 Answers
Related