So, I’m trying to run NGINX inside containers but the main server (not a container) is also running nginx. I’m aware that I need to alter the port 80/443 inside containers, and use proxy pass. But thus far I seem to be failing. I can’t use lxc config device add with listen/connect to port 80 seeing the port is already in use.
So it would be great if someone can help me with this. Also running UFW by the way
Indeed, it is an issue when you want to have more than one website being served on the same ports 80/443. What is needed, is a way for whichever service responds to an Internet connection, is able to deal connections that were intended for the other websites.
If you really want to keep that nginx running on the host, you need to adapt the above guide to work as a reverse proxy, with an additional web server (host’s web server). The above link should help you as well.
Id strongly recommenced you take the time to read and apply the tutorial, it will all seem so simple once your understand whats going on.
2 things can’t listen on the same port at the same time, the proxy device on the host isnt going to be able to listen for requests to port 80 because NGINX is already listening on port 80.
We use a reverse proxy here to distribute the inbound requests on port 80 between one or more hosts / containers typically by domain name (in the context of web)
Simos is saying you can do one of the following
The ideal option
The website running on the host NGINX will need to be put into a container / moved somewhere else because you are going to change NGINX on the host into a reverse proxy as per this part of the tutorial
You will not need to add a proxy device if you follow this solution because NGINX will act as the proxy device
Even more ideal would be to follow the tutorial and move the reverse proxy into a container & use an additional proxy device which would look like this
Thanks, Guess I’ll make some adjustments to have 1 proxy NGINX container then. I apologize for my edit, but I noticed I was a bit fast in replying. Noticed that it was the proxy only, forwarding to the nginx container, as such the root files are located there as well.
Guess this means some downtime, as I was hoping I could be running nginx on my main host until I swapped them over one by one. But I prefer the ideal method.
Which reminds me to ask another question regarding storage. I’m still new to lxd/lxc, so hope I can ask this in the same thread/post. If I generate more containers for the proxy and nginx, would that mean it would use an existing storage image file for all the containers or generate a new one each time?
Thus far I kept making new storage volumes for each container, alongside with new profiles. Not sure if that’s the ideal method. That’s the only thing I’m confused of right now how it generates storages for each container created.
Small example:
lxc launch ubuntu:18.04 apache1
Great, new ubuntu container. Yet, doesn’t explain where it gains the storage from, nor which type of storage either.
Sorry for the bit of off-topic question, but had to ask as it would be making my container creation for all my nginx containers easier.
storage is just a mystery to me, its another thread, and I think your question has been asked before so do some digging
A proxy isn’t used to serve files - it used to redirect requests to servers that will
Your containers for websites will contain the root directives
I dont use NGINX, so ive guessed done / quick google
// You would have one of these for each domain name you want to proxy
server {
// Listen on ipv4 port 80 - enabling proxy_protocol
listen 80 proxy_protocol;
// Listen ipv6 port 80 - enabling proxy_protocol
listen [::]:80 proxy_protocol;
// This server will handle connections for the domain name xxx
server_name nginx1.example.com;
// Im assuming this is a special charectar for proxy all requests
// you may have to tweak this
location / {
// When you use a reverse proxy you loose the original IP
// address so this provides the clients IP address as a
// new HTTP header
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
// This is where the request is forwarded to your container/s
// running webservers for this domain (because you could run
// more than one, I guess you could also provide an ip
// here aswell but im not sure E.G
// proxy_pass http://MY_CONTAINERS_IP
proxy_pass http://nginx1.lxd;
}
// This is some kind of module enabling in NGINX
real_ip_header proxy_protocol;
// Anyone's guess
set_real_ip_from 127.0.0.1;
}
Regarding the question on the storage, please create a new topic. The practice is to make the questions easier to find to new users when they search the forum.
PHP-FPM runs on all the containers serving websites not the proxy.
a reverse proxy just forwards trafic its not related to a specific implementation (PHP) I.E it can also “proxy” (forward) requests other than HTTP like web sockets or UDP / TCP connections
Imagine the reverse proxy as the person working at the train station who tells you what platform your train is on if you dont know - they dont know anything about your particular train they just know how to look it up and sends you off to the right platform
Please take the time todo some reading we are explaining what 1000+ articles on the web do
And I do read before making topics, hence I generated this topic as apparently I was trying to do it in a way that usually isn’t the best nor ideal way. So couldn’t find much helpful to help me out in my situation. Now I know it’s just better to create a single proxy container.
Again, I appreciate the replies. Thank you
Merged to prevent multi posts
I do have one more question, and I’ve googled it up but I couldn’t find any results that would help me.
According to the tutorial, you can set up:
set_real_ip_from proxy.lxd;
and:
proxy_pass http://nginx1.lxd;
But everytime I attempt to do so, I keep getting errors like:
nginx: [emerg] host not found in upstream "containername.lxd" in /etc/nginx/sites-enabled/default:10
And:
nginx: [emerg] host not found in set_real_ip_from "proxy.lxd" in /etc/nginx/conf.d/real-ip.conf:2
And yes, the container names exists. Once I replace the container name with their own IP address, I no longer receive any errors and it seems to be accepting it. How come?
can you ping your hosts from the container ? If not you have a DNS issue, a quick work around will be to put the dns names in /etc/hosts but thats the same as setting the IP’s in the config file
I can ping the containers from the main host, and ping other containers within a container. Nginx (proxy) and nginx container works just fine as well. I’m just unable to use the container hostname, only works by using their IP addresses.
While your solution would work, I’m just curious why the hostname is not recognized.
root@sourceop:~# ping sourceop.lxd
PING sourceop.lxd(sourceop.lxd (fd42:1020:56ec:da27:216:3eff:fe67:ae3a)) 56 data bytes
64 bytes from sourceop.lxd (fd42:1020:56ec:da27:216:3eff:fe67:ae3a): icmp_seq=1 ttl=64 time=0.101 ms
64 bytes from sourceop.lxd (fd42:1020:56ec:da27:216:3eff:fe67:ae3a): icmp_seq=2 ttl=64 time=0.072 ms
I merely answered your question if my web server was listening to ipv6, and I mentioned yes. But in general, I’m not using ipv6 so I probably should disable it.
I guess that’s the reason why it’s not working as it should, as it replies back with ipv6 as you mentioned rather than ipv4.
Just to make sure it works, which it does. I haven’t changed/altered it yet to my needs. I noticed some web configs have a proxy variable in listen, though not used in the tutorial.
Everything is working nicely with a single proxy container, and other nginx containers. But I have noticed is loop errors when I wanted to restore nginx web config files. I know it’s because both proxy and the nginx container forwards http to https. And I know the root directory is added within the nginx container as well. So what exactly is put in the proxy container? Only the port 80 and redirect to 443 alongside with their encryption location files?
I would assume then I would put the web configurations inside the 443 server block on the nginx container, as long it won’t have any redirection (from http to https?)