1

I have a reverse proxy with nginx set up using docker compose. It is fully working when I run all services together with docker-compose up. However, I want to be able to run individual containers, and start (docker-compose up service1) and stop them independently from the proxy container. Here is a snippet from my current nginx config:

server {
  listen 80;

  location /service1/ {
    proxy_pass http://service1/;
  }

  location /service2/ {
    proxy_pass http://service2/;
  }
}

Right now if I run service1, service2, and the proxy together all is well. However, if I run the proxy and only service2, for example, I get the following error: host not found in upstream "service1" in /etc/nginx/conf.d/default.conf:13. The behavior I want here is to just throw some HTTP error, and when that service does come up to route to it appropriately.

Is there any way to get this behavior?

John Howard
  • 61,037
  • 23
  • 50
  • 66
  • This cannot be done with standard directives. Maybe if you add Lua module you could write a script but not sure about that one. – Dusan Gligoric Aug 08 '18 at 07:37

2 Answers2

2

Your issue is with nginx. It will fail to start if it cannot resolve one of the upstream hostnames.

In your case the docker service name will be unresolvable if the service is not up.

Try one of the solutions here, such as resolving at the location level.


(edit) The below example works for me:

events {
  worker_connections  4096;
}

http {
  server {
    location /service1 {
        resolver 127.0.0.11;
        set $upstream http://service1:80;
        proxy_pass    $upstream;
    }

    location /service2 {
        resolver 127.0.0.11;
        set $upstream2 http://service2:80;
        proxy_pass    $upstream2;
    }
  }
}
moebius
  • 2,061
  • 11
  • 20
  • I tried this. I initially had the error `no resolver defined to resolve ...`. Setting the resolver to 127.0.0.11 fixed this though. However, it doesn't function correctly. With the config: `set $upstream service; proxy_pass http://$upstream/;` I can only request the index (`/`) no matter what url I go to. However, if I make this just `proxy_pass http://service/;` it works fine (as long as service is up before the proxy, which is what I am trying to avoid). – John Howard Aug 06 '18 at 15:31
  • @JohnHoward I've updated the config - this version seems to work for me. nginx starts up even if service1 or service2 are not up. – moebius Aug 07 '18 at 08:42
  • You can also request whatever upstream url you like (not just /) by including a `rewrite` url rule within the location directive. – moebius Aug 08 '18 at 01:43
0

Sounds like you need to use load balancing. I believe with load balancing it will attempt to share the load across servers/services. If one goes down, it should automatically use the others.

Example

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://myapp1;
        }
    }
}

Docs: http://nginx.org/en/docs/http/load_balancing.html

danday74
  • 52,471
  • 49
  • 232
  • 283
  • I don't think this is exactly what I want. When nginx can't find service 1 it's not because it went down unexpectedly, it's because I am intentionally not running it, so I want the /service1 route to return a 404 (or some other error). – John Howard Aug 06 '18 at 04:08
  • that may be the case, however, in the real world - when someone brings a service down, for whatever reason, they dont want their site to go down, they want to be able to do whatever they want, in their own time, bring the service back up at their leisure, with no down time - thats ONE purpose of a load balancer – danday74 Aug 06 '18 at 16:55
  • 1
    I am confident that I want the behavior I described and not a load balancer – John Howard Aug 06 '18 at 20:37