Port forwarding to profile?

Hello,

I just need to know if it’s possible to forward port 80 & 443 to profile , I know I can port forward to my container like

sudo lxc config device add mycontainer port80 proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80
sudo lxc config device add mycontainer port443 proxy listen=tcp:0.0.0.0:443 connect=tcp:127.0.0.1:443

What about my other containers, I’ve 4 containers, Do I’ve to run above command for each container ?

You cannot forward the same port on the host to multiple instances, so whilst there’s nothing stopping you from adding the proxy device to your profile, only the first running instance will be able to start.

You should add a per instance proxy device using a different host side listen address or port, or run something like nginx or haproxy to perform HTTP level reverse proxying based on requested domain name.

Thanks, so I’ve to modify http and https port for each container with in my NGINX , is there any other way where I can tell LXD to use port 80 & 443 for all my containers ?

As per my understanding, I will run the following commands

Container 1
sudo lxc config device add container1 port80 proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

Container 2
sudo lxc config device add container2 port81 proxy listen=tcp:0.0.0.0:81 connect=tcp:127.0.0.1:81

I am just avoiding to go inside nginx and change listening port, so looking for easier version, if there’s any available.

You can use the same connect value on each container, just change the listen address or port .

Than again, what is point of having public interface listening on ports 81,82 etc… ? Not sure what is enviroment like but if this is public Web servers and you have routed public IPv6 subnet just use that and make something like Cloudflare translate IPv6 <> Ipv4. That is what I do for over a year now.

Yes, That’s what I am going to be doing . Thanks

So proxy is the only way to out, for me to have multiple containers hosting websites ? I did tried

Container 1
sudo lxc config device add container1 port80 proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

Container 2
sudo lxc config device add container2 port81 proxy listen=tcp:0.0.0.0:81 connect=tcp:127.0.0.1:81

It didn’t work for me, after running those two commands I went in NGINX and change the port in the conf file & it didnt do anything.

Like I said you don’t need to change the connect part for each container, just the listen part:

sudo lxc config device add container2 port81 proxy listen=tcp:0.0.0.0:81 connect=tcp:127.0.0.1:80

The proxy device just forwards connections it is not an L7 reverse proxy, so it doesn’t understand the http protocol so you can’t have multiple proxies listening on the same address and port on the lxd host.

So for each container I.e running nginx for example

HTTP PORT

container -1 port-81
container -2 port-82
container -3 port-83

sudo lxc config device add container1 port81 proxy listen=tcp:0.0.0.0:81 connect=tcp:127.0.0.1:80

sudo lxc config device add container2 port82 proxy listen=tcp:0.0.0.0:82 connect=tcp:127.0.0.1:80

And do the same for https, I hope it’s clear

Yea that would work if you only have 1 listen ip to use

After spending a lot of time, I was able to configure this and I wrote a blog about it How to setup LXD with HA Proxy with full configuration , if any one need to setup the same, be my guest to use the configuration, you can use ssl and http . All the best & Thank you every one :slight_smile:

Thanks! Maybe change the blog title from LXC to LXD, as that’s what you’re using?

Fixed :slight_smile:

1 Like