How to NGINX Reverse Proxy with Docker Compose
How to NGINX Reverse Proxy with Docker Compose
Take a look at how to use NGINX reverse proxy with Docker Compose to expose multiple services without changing ports.
Join the DZone community and get the full member experience.Join For Free
While developing a web application, a common method of calling the application from a local machine is through
http://localhost:x ports, which essentially means that we are required to expose several ports to access different modules of the application. In the article below, we will go through the method of using Reverse Proxy to call an application and the benefits of using it.
Why Do We Need Reverse Proxy?
The most obvious reason for using Reverse Proxy is to avoid changing ports every time you try to access different modules of the application through the same URL. Through Reverse Proxy we can reach frontend, backend, or other services without changing port through a single domain. Another important reason for using Reverse Proxy is to mask services behind a proxy and avoid dealing with CORS issues.
- Docker Compose on Linux
- iptables should be enabled. Note that Docker uses iptables to access incoming connections.
For reference for this article, let us create a Wordpress-MySQL server with NGINX in one service. Start by creating the Docker container, along with defining ports, base image, container name, and service names.
Let's explain the setup in steps.
servicescommand defines the service you will build in Docker.
2. As shown below, the
running_wp service uses
nginx:latest as the base image. You can also use your own custom image for MySQL and Wordpress. Next is to assign a container name, for instance currently
production_wp is used below. Copy the local files under volume section to the given directory while building this docker image. It is important to assign Ports which are to be used for accessibility within or outside of the application. As shown below, we have mapped
8081 host to
80 port for container.
3. Assign necessary access credentials with the
4. Assign the base image and container name. The base image will create NGINX on the first run. You can also add encrypted SSL files through https://letsencrypt.org/; it is free.
5. Add NGINX configuration file under
conf.d path - this is the default directory for virtual host descriptions.
6. As shown below there are two parts of this config file. First one shows the server side defining which port will be listened by the NGINX Container (
443). Secondly, the forward traffic would be directed to port
8081 — this is the port of production container for reaching.
7. Enable secure HTTP with the
ssl on command.
8. To prefix headers for proxy connections, we can use the
proxy_set_header directive which helps in redefining or appending fields to the request header passed through the proxied server.
9. For Reverse Proxy as mentioned in the beginning, we will give a path for the Wordpress container in NGINX configuration. Let us say it is using http://localhost/wp to http://localhost:8081, which is an asymmetric path.
10. Now we can start our containers with the command below. Remember to be in same path with
docker-compose.yaml while starting containers.
docker-compose up command is a shorthand form of
docker-compose build and
docker-compose run .
12. When complete, we should have two containers deployed, one of which we cannot access directly:
13. We can check our applications (one with NGINX and the other one with Apache).
Navigate to http://localhost:8080, and this will hit NGINX Reverse Proxy which will in turn will load the NGINX web application:
Once the services are up, try to connect your web application to the localhost link. If it is not answered, check your iptables table for correctness.
By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers. Each outgoing connection will appear to originate from one of the host machine’s own IP addresses thanks to an
iptables masquerading rule on the host machine that the Docker server creates when it starts:
This article is aimed to show how you can use Nginx with
docker-compose easily. It also shows setting of Reverse Proxy configuration on containers. Using this will give you additional flexibility during deployment of a web application.
I hope you liked this post! More to come soon.
Until then, here are some more Kubernetes and Docker best practices for managing and deploying containers.
Published at DZone with permission of Sudip Sengupta . See the original article here.
Opinions expressed by DZone contributors are their own.