2

I have tried this: NGINX reverse proxy not working to other docker container

and this: Docker nginx-proxy : proxy between containers

and followed nginx config from here: nginx proxy_pass to a linked docker container

I am simply trying to tell nginx to proxy to a linked api service on port 4000. I do not want to expose 4000 to host machine because there will be multiple services running on this port.

This is my docker-compose.yml:

version: '3'
services:
   api:
     build: ./api
     image: myapi:latest
     container_nameE: api
   api_nginx:
     image: nginx:latest
     container_name: api_nginx
     depends_on:
       - api
     links:
       - api
     ports:
       - "80:80"
     environment:
       - NGINX_SERVER_NAME:localhost
     volumes:
       - ./nginx:/etc/nginx/conf.d
...
...

and my nginx server is super minimal:

upstream backend {
   server api;
}
server {
   listen 80;
   listen [::]:80;
   server_name ${NGINX_SERVEE_NAME};
   location / {
      resolver 127.0.0.1;
      proxy_pass http://backend/$1;
   }
}

This is the error is throwing:

...[error] 20#20: *1 no resolver defined to resolve api, client: 172.23.0.1, server: ${nginx_server_name}....

and the page shows a 502 Bad Gateway

What is going on? I've followed other people's nginx configs and it's not working, I have no idea.

Zac
  • 1,719
  • 3
  • 27
  • 48
  • Both services need to share a common network to be addressed by their names. Create a network and add it to your containers, as in https://stackoverflow.com/a/54411790/4632951 – Andrew Morozko Sep 16 '20 at 22:46
  • 1
    @AndrewMorozko Both containers are on the `default` network; you don't need to manually declare anything for inter-container networking to work (and you don't need `links:` either). [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation has more details. – David Maze Sep 17 '20 at 00:16
  • David is correct, you don't seem to need them. I could've sworn there was some issue that required manually creating a network, but it seems like it was resolved. – Andrew Morozko Sep 17 '20 at 00:21
  • @AndrewMorozko without the link it still gives same error, I am going to try that network thing that andrew posted even though you said i might not need it, I'll give that a shot – Zac Sep 17 '20 at 00:36
  • @DavidMaze nope, same error adding the bridge network in that other question andrew posted. It's still saying `1 no resolver defined to resolve api...` I think it's something to do with my nginx config? – Zac Sep 17 '20 at 00:48

0 Answers0