Efie
Efie

Reputation: 1680

Can't redirect traffic to localhost with nginx and docker

I'm new to Docker and nginx so this may be a simple question but I've been searching through question/answers for a while and haven't found the correct solution.

I'm trying to run an nginx server through docker to reroute all requests to my.example.com/api/... to localhost:3000/api/...

I have the following Dockerfile:

FROM nginx
COPY ./nginx.conf /etc/nginx/conf.d/default.conf

and the nginx.conf file:

server {
    server_name my.example.com;
    location / {
        proxy_pass http://localhost:3000/;
    }
}

When I make the calls to the api on localhost:3000 this works fine but when I try to run against my.example.com I get a network error that the host isn't found. To be clear the domain I want to 'redirect' traffic from to localhost host is a valid server address, but I want to mock the api for it for development.

Upvotes: 4

Views: 2191

Answers (1)

Sushil Kumar
Sushil Kumar

Reputation: 179

This isn't working because your nginx proxying request to localhost which is container itself but your app is running on host's port 3000 -- outside of container. check this article

change
proxy_pass http://localhost:3000/;
  to
proxy_pass http://host.docker.internal.

add 127.0.0.1 example.com my.example.com in etc/hosts

Upvotes: 5

Related Questions