nginx as reverse proxy to two apps

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?

Thanks in advance!

HI there - this is more of an nginx configuration question than a Fly.io one :slight_smile:

each of your .internal hostnames will resolve to all the IP addresses of the machines in that app. Example:

# dig +short ipcheck-roadmr.internal aaaa
fdaa:2:7d1e:a7b:144:5b46:765:2
fdaa:2:7d1e:a7b:144:e926:3ee0:2

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 :slight_smile:

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.

Hope this helps!

  • Daniel
2 Likes

Hi Daniel
Thanks a lot for your profound explainations! I just learned a lot about nginx even though I’ve been using it for years.
Daniel (too ;-))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.