This is a simple nginx proxy server setup to map web apps running independently on separate containers.
To setup the proxy server for you local apps just update proxy/servers.json
file with respective settings.
[
{
"site_a": {
"server_name":"my-site-a",
"location":"/"
},
"site_b": {
"server_name":"my-site-b",
"location":"/"
}
}
]
For instance, if we are running more than one web apps on docker containers then obviously, we will not be able to expose same external port for the app.
for example:
- http://my-site-a/
- http://my-site-b/
By using, nginx proxy server we can request respective servers irrespective to the exposed port to host.
Docker containers need to be in same network in order to identify each other. Therefore, we will have to create a bridge network and assign that network to every docker container we wish to put in the proxy network.
docker network create -d bridge nginx-proxy
Include, network config in docker compose file, for example:
version: '3.5'
services:
site_a:
container_name: mysite_a
image: nginx:alpine
networks:
- nginx-proxy
networks:
nginx-proxy:
name: nginx-proxy
Add hostnames entries in your host machine's /etc/hosts
file.
127.0.0.1 my-site-a
127.0.0.1 my-site-b
This repo consists of a working proxy server and sample web app site_a
and site_b
.
Steps:
docker-compose -f site-A/docker-compose.yml up --build -d
: Spin upsite_a
container.docker-compose -f site-B/docker-compose.yml up --build -d
: Spin upsite_b
container.docker-compose -f proxy/docker-compose.yml up --build -d
: Spins up thethe_proxy
container.
Curl request from host machine would look like below.
➜ curl -I my-site-a
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 27 Jan 2019 12:01:02 GMT
Content-Type: text/plain
Content-Length: 18
Connection: keep-alive
upstream site_a { server site_a; }
or,
upstream site_a { server site_a:3000; }
upstream site_b { server site_b:3000; }
Here site_a
and site_b
is name of the running docker containers and 3000
is the port exposed in that container.
So, typical proxy server config looks like as below.
upstream site_a { server site_a:3000; }
server {
listen 80;
server_name my-site-a;
location / {
proxy_pass http://site_a;
proxy_redirect off;
}
}
upstream site_b { server site_b:3000; }
server {
listen 80;
server_name my-site-b;
location / {
proxy_pass http://site_b;
proxy_redirect off;
}
}