Hi
Is there a sample or advice how the upstream part of an nginx config would look like for two fly apps?
Given we have app1 and app2, each has two instances they can be reached as app1.internal and app2.internal.
Would it look like this?
upstream app1 {
server app1.internal:3000;
}
upstream app2 {
server app2.internal:3000;
}
But actually there are two machines behind app1.internal and app2.internal. How can I adress this problem?
Nginx’s documentation for the server directive in upstream states:
A domain name that resolves to several IP addresses defines multiple servers at once.
so the configuration you posted would effectively define two backend servers, one for each machine’s IP and it should generally work.
HOWEVER
If you add or remove machines or the internal IP address of your existing machines changes, for example due to a machine migration, nginx will NOT refresh the list of IP addresses. See this Stackoverflow answer for more details.
In short, you can use the resolve argument in your server definition to have nginx monitor the DNS record for changes to the list of IPs, but it requires a commercial subscription to nginx. The Stackoverflow answer suggests a workaround using a proxy_pass directive instead, with a variable to hold and refresh the list of IP addresses.
You can also just restart nginx when you do add, remove or change your .internal app’s list of machines.