Nginx Reverse Proxy with Dynamic Containers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Nginx Reverse Proxy with Dynamic Containers



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:


docker-compose up


docker-compose up service1


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.


host not found in upstream "service1" in /etc/nginx/conf.d/default.conf:13



Is there any way to get this behavior?





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 8 at 7:37




2 Answers
2



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





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 6 at 4: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 6 at 16:55






I am confident that I want the behavior I described and not a load balancer
– John Howard
Aug 6 at 20:37



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;







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 6 at 15:31


no resolver defined to resolve ...


set $upstream service; proxy_pass http://$upstream/;


/


proxy_pass http://service/;





@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 7 at 8:42






You can also request whatever upstream url you like (not just /) by including a rewrite url rule within the location directive.
– moebius
Aug 8 at 1:43


rewrite






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard