- Docker Wordpress Nginx Reverse Proxy
- Wordpress Docker Image
- Docker Wordpress Nginx Php-fpm
- Docker Wordpress Compose
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. In this article, we’ll learn how to deploy WordPress using NGINX, PHP-FPM and MariaDB using Docker Compose in an Ubuntu machine. The below image illustrates the multi-container deployment that we. WordPress 5 in Docker with Nginx and LetsEncrypt TL;DR If you are comfortable with Docker and Docker Compose, you can go straight to the GitHub repo and get started. For the everyone else, read on WordPress + Docker =.
Why do we need Reverse Proxy?
The most prominent reason of using Reverse Proxy is to avoid changing ports everytime 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. Other important reasons of using Reverse Proxy is to mask services behind a proxy and avoid dealing with CORS issues.
Prerequisites
- Docker Compose on Linux
- iptables should be enabled. Note that Docker uses iptables to access incoming connections.
Building docker-compose.yml
For the reference of 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.
services
command defines the service you will build in Docker.
- As shown below, the
running_wp
service usesnginx: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 currentlyproduction_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 mapped8081
host to80
port for container.
![Docker Docker](/uploads/1/1/9/5/119550656/417149738.jpg)
- Assign necessary access credentials with
environment
command.
- Assign the base image and container name. Base image will create Nginx on the first run. You can also add encrypted SSL files through https://letsencrypt.org/, it is free.
- Add Nginx configuration file under
conf.d
path - this is the default directory for virtual host descriptions.
- 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 (
8080
and443
). Secondly, the forward traffic would be directed to port8081
- this is the port of production container for reaching.
- Enable secure HTTP with the
ssl on
command.
- 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.
- For Reverse Proxy as mentioned in the beginning, we will give a path for the Wordpress container in Nginx conf. Let us say it is using e.g, http://localhost/wp to http://localhost:8081 which is asymmetric path.
- Now we can start our containers with the command below. Remember to be in same path with
docker-compose.yaml
while starting containers.
![Docker Wordpress Nginx Docker Wordpress Nginx](https://www.mscto.com/wp-content/uploads/2020/05/20200506211901268.png)
Docker Wordpress Nginx Reverse Proxy
- The docker-compose up command is a shorthand form of docker-compose build and docker-compose run.
- When complete, we should have two containers deployed, one of which we cannot access directly:
- 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:
Wordpress Docker Image
- Also check with navigating to http://localhost:8081 or http://localhost/wp, through the Nginx Reverse Proxy asymmetric path and the Apache web application will be loaded:
Troubleshooting
Docker Wordpress Nginx Php-fpm
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:Conclusion
Docker Wordpress Compose
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.