0

I'm trying to create a reverse proxy towards an app by using nginx with this docker-compose:

version: '3'
services:
  nginx_cloud:
    build: './nginx-cloud'
    ports:
        - 443:443
        - 80:80
    networks:
        - mynet
    depends_on:
        - app

  app:
    build: './app'
    expose:
        - 8000
    networks:
        - mynet

networks:
    mynet:

And this is my nginx conf (shortened):

 server {
  listen 80;
  server_name reverse.internal;

  location / {
    # checks for static file, if not found proxy to app
    try_files $uri @to_app;
  }

  location @pto_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app:8000;
  }
 }

When I run it, nginx returns:

[emerg] 1#1: host not found in upstream "app" in /etc/nginx/conf.d/app.conf:39

I tried several other proposed solutions without any success. Curiously if I run nginx manually via shell access from inside the container it works, I can ping app etc. But running it from docker-compose or directly via docker itself, doesn't work.

I tried setting up a separate upstream, adding the docker internal resolver, waiting a few seconds to be sure the app is running already etc with no luck. I know this question has been asked several times, but nothing seems to work so far.

Slartibartfast
  • 477
  • 1
  • 4
  • 13

2 Answers2

1

Can you try the following server definition?

server {
    listen       80;
    server_name  reverse.*;

    location / {
        resolver 127.0.0.11 ipv6=off;

        set $target http://app:8080;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass $target;
    }
}

Al-waleed Shihadeh
  • 2,697
  • 2
  • 8
  • 22
  • thank you, now I don't get the error anymore but nginx exits immediately with: "nginx_cloud exited with code 0" both from docker-compose or running it through docker directly. Logs show no activity of sort. – Slartibartfast Mar 02 '20 at 22:15
  • Ok this doesn't really solve it but it helped as nginx doesn't seem to timeout when a variable is used (??) so that pointed me in another direction: rebooting my computer. Something non-obvious happened to docker and rebooting solved it. Also it wasn't a matter of order of loading as the app loads near-instantly (and I had a wait-for-it.sh added to nginx just to be sure of loading nginx after the app). Thanks you and to Maxim. – Slartibartfast Mar 03 '20 at 16:09
1

The app service may not start in time. To diagnose the issue, try 2-step approach:

docker-compose up -d app

wait 15-20 seconds (or whatever it takes for the app to be up and ready)

docker-compose up -d nginx_cloud

If it works, then you have to update entrypoint in nginx_cloud service to wait for the app service.