Table of Contents
How to install Nginx as a reverse proxy server with Docker
Introduction
This is the most standard practice which is used and we use docker run command with -p option to bind docker port with and host machine port. Now if we have to do this with a couple of services this process might work well but if we had to cater a large number of containers, remembering port numbers and managing them could be a hurricane task.
This problem can be dealt by installing Nginx, which is a reverse proxy server and directs the client requests to the appropriate docker container
Installing Nginx Base Image
Nginx Image can be downloaded from docker hub and can be installed by simply using. docker run nginx Nginx Configuration is stored in file /etc/nginx/nginx.conf. This file holds a reference to default.conf
include /etc/nginx/conf.d/*.conf;
Follow the below steps to run a nginx server and have a peek around nginx configuration
- Run the latest Nginx docker Image using
1 2 3 | docker run -d --name nginx nginx |
- Open the bash console for accessing Nginx configuration
1 2 3 | bash -c "clear && docker exec -it nginx sh" |
Navigate to /etc/nginx/conf.d and cat default.conf copy file contents.We will use this file contents in next steps.
Creating our own custom Nginx Image
In this step we will try to modify the base nginx image, with changes to default.conf
Create a simple project in eclipse(File->New ->General-> Project) and create a new file called default.conf in the project directory.
In this file add the location block where
1 2 3 4 5 | location /<URL-To-BE-ACCESSED> { proxy_pass http://<DOCKER_CONTAINER_NAME>:<DOCKER-PORT>; } |
for eg,
1 2 3 4 5 | location /app1 { proxy_pass http://microservice1:8080; } |
where the app1 is the URL and microservice1 is the docker container name and 8080 is the docker port , this info can be found using docker ps-a
This is how the default.conf looks like for 2 docker containers named microservice1 and microservice2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server { listen 80; server_name nginxserver; location /app1 { proxy_pass http://microservice1:8080; } location /app2 { proxy_pass http://microservice2:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } |
Creating Docker Image
Next step is to create a Dockerfile where we will replace the default configuration file default.conf in the nginx base image with our version of default.conf
Create a file called Dockerfile and add below contents. Make sure the file is at same level as default.conf and under project root directory.
1 2 3 4 5 6 7 8 9 10 | #GET the base default nginx image from docker hub FROM nginx #Delete the Existing default.conf RUN rm /etc/nginx/conf.d/default.conf #Copy the custom default.conf to the nginx configuration COPY default.conf /etc/nginx/conf.d |
Now all we need is to build this docker image. Open terminal/command prompt and navigate to project directory
1 2 3 | docker build -t mynginx . |
If above command was suucessful, our own custom nginx image is ready with our configuration.
Running our own Custom Nginx Image
Each Docker Container is a seperate process and is unawaare of other docker container. However docker has –link attribute which can be used to create links between 2 docker containers and make them aware about the existence of other containers.
Before we run our image we need to make sure that the services mentioned in the location block are up and running , in our case microservice1 and microservice2.
docker ps -a
Next we need to link these 2 docker containers with our nginx container using this command
1 2 3 | docker run -d --name mynginx -p 80:80 -p 443:443 --link=microservice1 --link=microservice2 mynginx |
If the above command was successful with no errors we have successfully installed nginx as reverse proxy server and can be tested by opening a browser and accessing
http://localhost/app1
http://localhost/app2
For reference , please see below video.
Nice job! What if I want to use an actual virtual domain like test.dev though?
Do you mean you want to use nginx on virtual domain or forward incoming requests to virtual domain.
If you want to forward it to virtual domain you need to put URL in proxy pass block.
Proxy pass
If you question was related to using Nginx on Virtual domain then all you need is point DNS to the server where nginx image is running. Since we have exposed http port 80 and https port 443 for incoming traffic to nginx. All requests will be forwarded through nginx.
Thank you for your quick response!
Ideally instead of accessing my app on , I would prefer
That’s because what if I need to run two apps in the browser () which have different version of php for example hence different docker-compose files
I hope it makes sense to you.what I describe there
This is what a quick search on internet led me to , could this be answer to your problem ?
server {
listen 8001 default_server;
server_name web1.example.com;
location / {
proxy_pass ;
proxy_set_header Host web1.local:80;
}
}
server {
listen 8002 default_server;
server_name web2.example.com;
location / {
proxy_pass ;
proxy_set_header Host web2.local:80;
}
}
server {
listen 8003 default_server;
server_name web3.example.com;
location / {
proxy_pass ;
proxy_set_header Host web3.local:80;
}
}
upstream main {
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass ;
}
}
As you can see, the trick is to create a local server responding to a particular port that will proxy the server by rewriting the right Host for each servers. Then, you can use this local servers in your upstream and finally use that upstream in the real proxy.
Will this work for docker services also. Currently i am running 3 web servers as 3 different docker services on host ports 8444, 8445, 8446.
I want to run a proxy server on port 8443 and want to redirect based on path to the respective service.
– >
– >
– >
Any idea
hi
i’m a newbie i don’t understand the use of eclipse to create our own nginx custom image
why can’t we use a simple text editor ?
thanks for your answer
Amigo! You are the best! It’s work