So, I have some idea on what a reverse proxy does and will be using nginx (with the neat proxy manager UI) for my setup.
However, I’m not completely clear what exactly I want it to do and how I cn use it to run different services on one machine. I’m especially unclear on the ports configuration … tutorials will say things like “change the listening port to xxx for that service and to port yyy for the other service”
How does this work, which ports can I use and how do I need to configure the respective services?
EDIT: thanks everybody, your replies did help me a lot! I have my basic setup now up and running using portainer + nginx + fail2ban.
ELI5
So it’s saturday afternoon, a very hot one, so you ask your daddy for an ice cream (hosted service). The shop you go in is very bizarre though, as there is one vendor (TCP port) for each flavor (docker service/virtualhost). But it’s tricky because they’re all roaming in the shop, and you don’t know who’s responsible for each flavor. Your dad is also not very comfortable paying these vendors directly because they only accept cash and do not provide any receipt (self-signed certificate/no TLS).
Hopefully, there is the manager (reverseproxy) ! This girl is right where you expect her: behind the counter (port 80/443), accept credit cards and has a receipt machine (Domain name + associated certificate). She also knows everyone on her team, and who’s responsible for each flavor !
So you and your dad come to see the nice lady, ask for a strawberry + chocolate ice cream, and pay her directly. Once done, she forwards your request directly to the vendors responsible for each flavor, and give you back your ice cream + receipt. Life is good, and tasty !
Worth noting OP: port 80 is HTTP, and port 443 is HTTPS
Fantastic! I’d love to hear your explanation for DNS.
That one is easy ! Because in a few years (remember, you’re 5), you’ll be a scout ! And to collect a few dollars for your summer camp, you’ll sell pastries to the neighborhood. It’s easier than ever because it’s 2030, and everyone can just order the pastries on your website, and pay online. All you have to do now is hop on your bike, and deliver the pastries (network connections) to your neighbors (online servers). So you grab the first package, and read the label on it:
- Mrs. Britneak
And that’s it ! You have no idea who this person is, or where they live ! So you call out your leader (DNS server):
- Hi Mr. Leader !
- … (nobody ever get my UDP jokes)
- So I got this package to deliver to mrs. Brtineak. But I don’t know where she lives
- Oh sure, let me lookup the register (zone file). Hold on for a sec… Alright, she’s here: 62.644888, -160.194309
And then he hangs up immediately (this is UDP, remember?).
You write it down (local caching DNS server), and look it up. You’re a scout, so you’re trained to read and find GPS coordinates. You go there in a few minutes and deliver the package in time ! Mrs Britneak is happy, and you go on to the next package:
- Mr. Tomburgh
Time to call leader again !
This is a really good explanation!
This is just beautiful. I may steal this 😇
Please do ! Networking is beautiful and people need to know it !
A reverse proxy is a service that takes incoming traffic on an IP address and port. It reads the URL the connection came into and passes it to the service it is configured for.
Example: A server runs Plex. There is a DNS entry plex.myhome.nework that points to the IP of that server. Nginx listens on port 80 and 443. If a client connects to port 80 using plex.myhome.network nginx will pass it to Plex. If it comes in on 443 nginx will still pass it to Plex but it will also provide the configured SSL cert to the client connecting to Plex.
If the server is also running jellyfin and DNS is setup for jellyfin.myhome.network with the same IP. The user connects to jellyfin.myhome.network on port 80 Nginx instead passes it to jellyfin.
So from our example you can see that we have both jellyfin and Plex using the same IP address and port 80.
I’ll provide an ELI5, though if you actually want to use it you’ll have to go beyond ELI5.
You contact a web service via a combination of IP address and port. For the sake of simplicity, we can assume that domain name is equivalent to IP address. You can then compare domain name/port with street name/street number: you need both to actually find someone. By default, some street numbers are really standard, like 443 is for regular encrypted connection. But you can have any service on any street number, it’s just less nice and less standard. This is usually done on closed networks.
Now what happens if you have a lot of services and you want all of them reachable at address 443? Well basically you are now in the same situation as a business building with a lobby. Whenever you want to contact a service, you go to 443, ask the reception what floor they are in, and they will direct you there. The reception desk is your proxy: just making sure you talk to the right people.
Since you already got a lot of ELI5s, here is a basic to-do to get you up and running. From my experience, since I use the exact same setup as you describe.
- Set up your containers in a way you can reach them from you local network (e.g. http://123.456.789.10:123)
- Get a domain name (you can get one at the registrar of your choice, e.g. mydomain.com)
- Set up NGINX proxy manager (NPM) (default address of webui would be http://123.456.789.10:81)
- Set up a new proxy host in NPM:
- Domain name: mycontainer.mydomain.com
- Scheme: http
- Forward Hostname/IP: 123.456.789.10 (if you get an error later on, you can use the docker container name if NPM and your container are connected to the same Docker network)
- Port: 123
- Via access lists you can provide a very basic username/pw login to protect your sites (you can do more and cooler stuff with Authelia)
- In the SSL tab you can (and should) setup the SSL encryption: https://www.youtube.com/watch?v=TBGOJA27m_0
- Go to the DNS management of your registrar
- Add an A-record for mydomain.com and the public IP of your server (you can google public IP to find it out)
- Add a CNAME record for the subdomain with name mycontainer and target mydomain.com
- open port 443 of your server in your router If everything worked right, you can visit mycontainer.mydomain.com, your DNS server will resolve this to your public IP and forwards the request to nginx, which will serve the data of your local container
Here is an alternative Piped link(s): https://piped.video/watch?v=TBGOJA27m_0
Piped is a privacy-respecting open-source alternative frontend to YouTube.
I’m open-source, check me out at GitHub.
Good bot
So a reverse proxy is sort of like a phonebook or directory, it routes outside requests to the appropriate place. So imagine your reverse proxy is a receptionist, someone comes in and says “hey I am looking for plex.mydomain.com” the receptionist would then use the phonebook and say “ok if you are looking for plex.mydomain.com, go to building 192.168.1.10 (the ip), room 9000 (the port)”
Since you are asking about dockerized services, the networking for those can be done in several different ways, but the one thing that really matters is that each service needs to have a unique combination of ip and port, because only 1 service can live at each address. With docker, you could set up multiple services that use the host server’s ip, in which case each container will need to be on different ports, or you could have it so each container has its own ip, in which case the port can be anything.
This makes it clearer to my, would you mind helping me to understand all steps for my usecase. I want to run a lemmy instance and a mastodon instance on the same VPS, using the same domain but different subdomains - lmy.my-domain.tld and mstdn.my-domain.tld. I have my VPS IP address and setup the 2 subdomains with my domain provider (both subdomains are resolving the same IP).
I also did setup nginx on my server and can install SSL certificates for both of these domains. I’m now at the step where lmy.my-domain.tld should by directed to the lemmy service and mstdn.my-domain.tld to the mastodon service. As I understand it, both services listen to the ports 80 (http) and 443 (https). Do I now setup a room/building for Lemmy / Mastodon respectively where I tell nginx that lmy.my-domain.tld is at 0.0.0.0:3001 and mstdn.my-domain.tld is at 0.0.0.0:3002 for example. And in the config files for each of these installs I’d specify “0.0.0.0:300x” respectivly? (also have to make sure, that these docker installs don’t mess with my nginx config by themselves, right?)
It sounds like what you need to do at this point is find what IP address your lemmy instance and mastodon instance containers are using on your VPS. you can do “docker inspect containername” and look for the IP address in there. it might be something like 172.16.0.1 for lemmy and 172.17.0.1 for mastodon. then you want to set up your reverse proxy to point lmy.my-domain.tld to 172.16.0.1:80 (or whatever port you set lemmy to use) and then mstdn.my-domain.tld to point to 172.17.0.1:80 (again, port might be different, i dont know what the default port is)
-IF- both of the containers are using the same IP, then you will need to make sure that they are using different ports. if they are on the same ip and same port, whichever container loads 2nd will fail to properly load, because when a port is taken on an IP address, it is reserved and nothing else can try to listen on that port.
IPv4 version: Think of your public IP:Port as the office building, your internal IP:Port as the floor number, and reverse proxy as the reception on that floor.
(Your public IP:Port is routed to your internal IP:Port by the NAT on your router. The router knows which public port relates to which internal IP:Port due to the port forwarding rules you setup.)
IPv6 version: Think of your public IP:Port as the office, and the reverse proxy as the reception.
The following will be common to both IP protocols.
The port is usually 80 or 443, because reverse proxy is used for HTTP(S) connections, and by default those connections use the aforementioned ports.
When someone connects to your IP:Port, they ask the reverse proxy “hey, can you bring me to Mr. https://my-awesome-plex.xyz ?” and the reverse proxy will act as a middleman between that someone and the actual server that is serving that domain name.
The reverse proxy, as a middleman, forwards your requests to the server, and the server’s response is forwarded back to you by the reverse proxy too.
And just to make things complete… Why do we use reverse proxies?
-
To hide the identity of the actual server. This is easy to understand - you are only ever talking to the proxy, never the actual server. It’s just that your messages are continually forwarded to the actual server.
-
To save IP addresses. (One public address can serve multiple websites, if the actual servers are given only private IP addresses.)
-
To load balance. The reverse proxy can direct one to another server if the first server is overloaded. This requires a website to be served by more than one server though, and selfhosters like us never really need it.
-
To prevent attacks. If the reverse proxy realises that someone has been making too many connections to https://my-awesome-nas.com, the reverse proxy can reject subsequent connections. This is how Cloudflare works.
-
Caching. If the middleman remembers that the server responded “what is the answer to everything” with “42”, then the next time someone asks the same question again, the middleman will simply reply with the same response. This takes off the workload on the server.
-
A reverse proxy delegates HTTP requests to the web servers that should respond. It may also decrypt from HTTPS and/or cache them, it static content is served.
If you are using containers, the web servers running inside the containers are proxied. That means the delegation forwards the requests to the container which web server has to respond to.
This is more like an ELI35 for sys engineers…
Yeah, but you have to admit that children wouldn’t ask you what a reverse proxy is. I tried my best to write short, omitting details and in a simple language.
One big thing they’re used for is sort of multiplexing port 80/443. You have one daemon listening on them, and you can have multiple domains pointing at the same IP. The reverse proxy will figure out which backend service to forward requests too.
Proxies like Caddy and I think Traefik also automatically manage SSL certificates. In many cases you could have your application server handle SSL, but usually it’s a good idea to have dedicated software for this.
Could you have a look at my answer to the poster above - would multiplexing mean, that I configure my internal IP 0.0.0.0:XXXA for one service and 0.0.0.0:XXXB for another?
Yeah that’s exactly right! You have the proxy listen on 80/443 and use the subdomains to proxy to the respective other services that you have listen to other ports. Make sure those other ports are not open to the outside, though, as that would allow someone to bypass the proxy. In you example, you would change away from 0.0.0.0 to 127.0.0.1, which means the port is only open to the loop back interface, not the other ones. This happens accidentally especially when using docker for the app service. Also you should probably run some firewall to block all ports that you don’t wish to expose.
I’d really suggest you take a look at Caddy for the reverse proxy. It completely handles SSL certificate creation and renewal so you don’t have to do anything.
thank you, that clears things up a bit. Now it’s to play around with it, until I get it up and running :)
For future reading this “multiplexing” is called SNI inspection/routing and it can only be used when TLS/SSL is in use.
You can already do that without a reverse proxy.
A reverse proxy allows you to have multiple services running on 0.0.0.0:XXXA
For example you might have two websites at a server on 192.168.0.123
Your server will be setup to show those websites at two different ports, say “192.168.0.123:123” and “192.168.0.123:321” - with foo.com on 123 and example.net at 321
Your reverse proxy will listen to requests on port 80 (where websites are usually served) and look at each request. If it’s a request for the website at foo.com, it’ll send it to port 123. If it’s a request for example.net it will send it to port 321
But the client who is requesting the sites will only see port 80, at the same IP address for both sites.
Another important aspect of a reverse proxy is that it helps you with SSL. So your services (like Plex or whatever you want) doesn’t support SSL but you can manage to do it with the reverse proxy
Sounds smart. Good luck. I went the easy way and just did Cloudflare Tunnels to my apps and am starting to set up and look in to using Cloudflare access to give it all forces 2FA
If I can add to this … what’s the sort of etymology for the term. Why is it a reverse proxy exactly?
In the long long ago it was reasonably common (particularly on corporate or school/uni networks) to force all web traffic outbound from browsers through a proxy.
They’d then use these proxies to enforce policies (block sites basically) and often cache stuff too, important when internet connections sucked.
So generally proxies sit on the same network as the clients (browsers).
Reverse proxies sit on the same network as the servers.
Standard proxy is when your outgoing traffic is proxied through it. Reverse is when the incoming traffic is proxied.
Thanks!!
@remindme@mstdn.social 1 day
deleted