Nginx forward to multiple containers

Hi,
I have read @simos 's guide on Digital Ocean to setup multiple containers as app servers. (Link : https://www.digitalocean.com/community/tutorials/how-to-host-multiple-web-sites-with-nginx-and-haproxy-using-lxd-on-ubuntu-16-04 )
I also read a flockport guide on using nginx to forward traffic from the host to multiple containers which was what I was after. (Link : https://archives.flockport.com/deploying-lxc-containers-with-nginx/ )

My focus is to serve the same website www.xyz.com from both containers without the www1.xyz.com and www2.xyz.com initials.

I am a little perplexed with how it works though.
Flockport says, “First port forward the public IP 1.2.3.4 port 80 to the Nginx container 10.0.3.5 port 80. Now you can configure the Nginx instance to serve WordPress, Drupal and Joomla containers as a reverse proxy.”

Does this mean that ContainerA and ContainerB would both have to have a port forward rule from the host at port 80 using iptables ?
And then nginx can be setup on the host to distribute the incoming traffic using :

upstream xyz {
server 10.0.3.27:80;
server 10.0.4.37:80;
}

Pardon the long post. Trying to Learn the ins and outs of the system. Thank you for your time. :slight_smile:

Hello!

It is not clear to me what you mean in this quoted paragraph.
Specifically, you are describing here something like load-balancing while the tutorials are about splitting websites into their own separate container.
That is, you have www.xyz.com and blog.xyz.com, and you can set up each in their own separate LXD container.

With containers, you would have many websites on a single server. It is the reverse-proxy that does the disambiguation and routes the correct Web traffic to each container.
You can use nginx as the reverse-proxy, or you can use HAProxy.

In either reverse-proxy, you specify rules as in

  1. if the connection is for www.xyz.com, then forward to container web_xyzcom.
  2. if the connection is for blog.xyz.com, then forward to container web_blogxyzcom.

The example that you give with nginx with upstream and the two IP addresses, is about setting up a load-balancing rule.

Hi,
Thank you for the reply !

Yes I understand that now. MY main goal was to split traffic across to different containers.
I apologize but i posted the topic without banging my head a little. (laugh?)

So,
My understanding was flawed in thinking that every time we defined a server in the upstream rule we were supposed to define a subdomain for the website.
I threw in a small experiment and connected my domain www.xyz.com to my host.
Next i configured the containers in the upstream rule and viola I was able to split the traffic across.

PS : No need for iptables ! :relieved: :smile: