First, we need to create a directory example36 on the host.
$ mkdir example36 $ cd example36
Inside the directory, create nginx.conf file to store nginx's reverse proxy configuration.
nginx.conf:
server {
listen 80;
location / {
proxy_pass http://web;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
As mention in the directive proxy_pass http://web;, this allow nginx to work as
a reverse proxy server for the URL http://web. The web is the DNS name in the docker network.
When we create an nginx container we can copy or mount the nginx.conf file as
the /etc/nginx/conf.d/default.conf file in the container.
We can create example36/html1 directory on the host.
Inside the directory, create an index.html file.
$ mkdir html1 $ cd html1 $ touch index.html
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>web1</title>
</head>
<body>
<h1>This is Web1</h1>
<h2>CPE405: Docker is fun</h2>
</body>
</html>It is time to create example36/docker-compose.yml file.
docker-compose.yml:
version: '3'
services:
proxy:
container_name: proxy1
image: nginx:alpine
ports:
- '8080:80'
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
web:
container_name: web1
image: httpd:alpine
volumes:
- ./html1:/usr/local/apache2/htdocs:ro
Start all services in foreground. docker-compose will shows all the logs from both containers.
$ docker-compose up Creating network "example36_default" with the default driver Creating proxy1 ... done Creating web1 ... done Attaching to web1, proxy1 web1 | AH00558: httpd: Could not reliably determine ... web1 | AH00558: httpd: Could not reliably determine ... web1 | [Tue Aug 28 18:10:15.751008 2018] ... resuming normal operations web1 | [Tue Aug 28 18:10:15.751349 2018] ... 'httpd -D FOREGROUND'
Now, we should be able to access the service with URL http://localhost:8080. The request will go through the nginx reverse proxy which then redirect the request to the web server. All static contents on the website will be cached on the reverse proxy. This will eases up the load on web server.