Nginx vs Traefik: The 2024 Proxy Showdown
44sDirect comparison of two popular tools with market stats sparks curiosity and debate among developers.
▶ Play Clip"Delivers a solid comparison with practical examples, though the title oversells the 'vs' aspect slightly."
This video compares nginx and Traefik for setting up a reverse proxy with Docker. It demonstrates configuring nginx with a custom configuration file and Traefik using Docker labels, highlighting the ease of adding new services with Traefik. The video also covers using Docker networks to connect services across multiple compose files.
nginx was released in 2004 and is used by 32.3% of websites according to a Netcraft survey in January 2022. Traefik is newer (founded in 2016) and is a cloud-native application that simplifies deploying and operating applications.
The docker-compose file uses the default nginx image on port 80, along with 'whoami' and Ghost blogging services. The default nginx config is overridden with a custom one that defines locations for each service, using proxy_pass to route requests.
Adding a new service requires copying the service definition and adding a new location block in the nginx config, then restarting the service. This is a manual process.
The community tool 'nginx-proxy' (on GitHub) automatically picks up new services. It requires changing the image, mounting docker.sock, and setting environment variables like VIRTUAL_HOST, VIRTUAL_PATH, and VIRTUAL_PORT on each container.
Traefik uses Docker labels instead of environment variables. The docker-compose file includes Traefik with ports 80 (proxy) and 8080 (dashboard). Services are configured with labels like traefik.http.routers.whoami.rule=PathPrefix(`/whoami`).
The dashboard provides a visual overview of configured routers and services, with a dark theme option.
To connect services from different compose files, create an external Docker network (e.g., 'home') and add it to each service's networks. This allows Traefik to proxy services from multiple compose files.
Traefik offers a more dynamic and automated approach to reverse proxying in Docker compared to nginx, especially when adding or removing services frequently. However, nginx remains a solid choice with community tools like nginx-proxy to mitigate its manual configuration.
When was nginx originally released?
2004
00:02
What percentage of websites use nginx according to the Netcraft survey in January 2022?
32.3%
00:16
What is the main downside of using nginx as a reverse proxy in Docker?
No out-of-the-box way to start new services; requires manual configuration and restart.
02:35
What tool can be used to automatically configure nginx for new Docker services?
nginx-proxy (on GitHub)
03:17
How does Traefik know which services to proxy?
Via Docker labels on each container.
04:39
What port does Traefik use for its dashboard?
8080
05:06
How can you connect services from different docker-compose files to the same reverse proxy?
Create an external Docker network and add it to each service's networks.
05:56
nginx market share
Provides a concrete statistic on nginx's popularity, establishing its relevance.
00:16nginx manual configuration downside
Clearly articulates a key limitation of nginx, setting up the comparison with Traefik.
02:35nginx-proxy solution
Introduces a community workaround, showing practical solutions beyond the default.
03:17Traefik uses Docker labels
Highlights a fundamental difference in configuration approach between the two tools.
04:39Docker networks for multi-compose
Demonstrates a scalable pattern for managing services across multiple compose files.
05:56[00:02] at the difference between nginx and traffic for setting up a reverse proxy with docker for those not familiar with the 2 nginx originally released in 2004. according to a netcraft web server survey done in
[00:16] to a netcraft web server survey done in january 2022 32.3 websites are using traffic on the other hand is relatively new founded in 2016 it's a cloud native application that aims to take out the complexity of designing deploying and
[00:29] operating applications so first we're going to have a look at setting up nginx as a reverse proxy in my docker compose file here i've got the default nginx image which is running on port 80.
[00:42] i also have the who am i image and which is also going to be running on port 80 and the ghost blogging image which natalie runs on port 2368 so you can see here i'm overwriting the default.com file
[00:56] with my own so the default.com file is set up here locations set up i have one for who am i and i have the platform you don't need to worry too much about
[01:11] settings and you can copy and paste them from my github which there's a link in the description of this video you'll notice the proxy pass parameter this works whenever the docker containers are running within the same
[01:23] containers are running within the same docker network and see what it looks like so i'm going to write docker compose
[01:39] minus f and the name of my file and up go to our browser and if we go to our who am i you should see it's running
[01:53] it gives you all the information about the browser and everything else the browser and everything else and now similarly if we go to ghost
[02:09] we didn't have nginx running as a reverse proxy we'd have to have these set up in different ports so say this is who am i which is running on port 80 but we might have other services so let's run this on port 8080
[02:22] so let's run this on port 8080 and for ghost we're running on port 2368 so we might have to run this on port 3001 so having a reverse proxy setup allows us to easily configure multiple services
[02:35] and have them running under different subfolders or even sub-domains now the downside of nginx is there's no out-of-the-box way to start new services say i wanted to add another blog to this docker compose file
[02:48] we copy this ghost one let's call it coach two to be able to set this up inside nginx we'd also have to copy our ghost location set this up as ghost 2
[03:05] and then restart our service naturally the community's found a way of getting around this limitation if you do want to use nginx i suggest you check out this github nginx proxy which allows you to automatically pick
[03:17] now if we want to change our docker compose to use nginx proxy compose to use nginx proxy first we need to change the image
[03:30] volumes to use docker sock so that our container can access the other docker container can access the other docker containers on the system variables on each of our containers so that nginx proxy knows what to do here
[03:46] we have on our whoa my container can set up the virtual host which in our case is localhost the virtual path which is who am i and the port that the container is running on which is 18 in this case
[03:58] and then we're going to do the same for ghost and ghost 2.
[04:12] and that's it if we run this is running correctly on each of the ports ports so we have who am i running
[04:27] and of course goes to nginx proxy you can see here we've got the docker.sock added so traffic has
[04:39] access to the running docker containers instead of environment variables we're using docker labels so traffic knows if a service is to be proxied in a similar way we can set up the port and the path this time using a registered expression
[04:52] to specify the path now you also notice that we have port 80 set up for our proxy request and port 8080. now port 8080 is for the dashboard dashboard view so we can see which services are set up
[05:06] and running now you can also see i've got a traffic.yml file set up at the moment i'm just using the defaults that are specified on the docker website but documentation there's a lot more settings there so as before we have who
[05:19] am i that's working fine ghost is also pointing to our ghost and lastly on port 8080 we have our dashboard as you can see here you get a nice view of which services have been set up you
[05:31] can see which routers are running and you see here we've got our one working for ghost as well as one for who am i there isn't much you can actually nice overview of all the services and also if
[05:43] like me you don't like lots of white services you can use the dark theme as the last thing i want to show you is around docker networks at the moment i've only shown services configured in the same docker compose file however you
[05:56] docker compose files and still have them running through one instance of traffic or nginx proxy first you need to create the docker network we do this with network create and it's called our network home
[06:11] then we need to add that network to our docker compose files then the name of our network which in this case is home [Music] true
[06:30] each of our services so let's do networks
[06:51] services set up without traffic here so we've just got another whoa my instance and another ghost this time it's called who am i two and ghost two so again we're going to add
[07:03] so again we're going to add add this network to each of them so now we need to start each of these up and then we should be able to see them
[07:15] and then we should be able to see them coming through in traffic are in that file and now i'm going to create another
[07:31] console and start the one that's just got the services in it dashboard let's give this a refresh
[07:48] you can see it's coming through on who well and we'll just double check that these work go who am i refresh that one got my two
[08:01] working fine ghost ghost and ghost two useful there's a link below to the github so you can find the code for this
[08:16] and if you found this useful please click subscribe and like the video and this is quite a new channel so really help me out thank you
⚡ Saved you 0h 08m reading this? Transcribe any YouTube video for free — no signup needed.