We have several rails apps under common domain in Docker, and we use nginx to direct requests to specific apps.
our_dev_server.com/foo # proxies to foo app
o
I had the same "Host not found" issue because part of my host was being mapped using $uri
instead of $request_uri
:
proxy_pass http://one-api-service.$kubernetes:8091/auth;
And when the request changed to the auth subrequest, the $uri
lost its initial value. Changing the mapping to use $request_uri
instead of $uri
solved my issue:
map $request_uri $kubernetes {
# ...
}
For me, option 3 of the answer from @Justin/@duskwuff solved the problem, but I had to change the resolver IP to 127.0.0.11 (Docker's DNS server):
location /foo {
resolver 127.0.0.11 valid=30s;
set $upstream_foo foo;
proxy_pass http://$upstream_foo:80;
}
location /bar {
resolver 127.0.0.11 valid=30s;
set $upstream_bar foo;
proxy_pass http://$upstream_bar:80;
}
But as @Justin/@duskwuff mentioned, you could use any other external DNS server.
If you can use a static IP then just use that, it'll startup and just return 503
's if it doesn't respond.
Use the resolver
directive to point to something that can resolve the host, regardless if it's currently up or not.
Resolve it at the location
level, if you can't do the above (this will allow Nginx to start/run):
location /foo {
resolver 127.0.0.1 valid=30s;
# or some other DNS (you company/internal DNS server)
#resolver 8.8.8.8 valid=30s;
set $upstream_foo foo;
proxy_pass http://$upstream_foo:80;
}
location /bar {
resolver 127.0.0.1 valid=30s;
# or some other DNS (you company/internal DNS server)
#resolver 8.8.8.8 valid=30s;
set $upstream_bar foo;
proxy_pass http://$upstream_bar:80;
}
You can do not use --link
option, instead you can use port mapping and bind nginx to host address.
Example: Run your first docker container with -p 180:80
option, second container with -p 280:80
option.
Run nginx and set these addresses for proxy:
proxy_pass http://192.168.1.20:180/; # first container
proxy_pass http://192.168.1.20:280/; # second container
The main advantage of using upstream
is to define a group of servers than can listen on different ports and configure load-balancing and failover between them.
In your case you are only defining 1 primary server per upstream so it must to be up.
Instead, use variables for your proxy_pass
(es) and remember to handle the possible errors (404s, 503s) that you might get when a target server is down.