1

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.

2 Answers 2

2

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;
    }
}

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.