It is common for individual developers and small businesses to run multiple independent websites on a single VPS. However, if they need to listen on the same port, such as 80
, we had to merge their configuration files together, which is not clean.
The main idea comes from How to set your VPS with docker containers for multiple websites and corresponding repo rokerkony/dockerize-vps. However, his method doesn't support communication between containers. To solve this problem, I make some modifications based on his repo.
jwilder/nginx-proxy is a automated nginx reverse proxy for docker.
nginx-proxy sets up a container running nginx and docker-gen. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.
We can use it as a unified frontend to listen on 80
and 443
port, and then dispatch request to different websites according to the url.
git clone [email protected]:Yriuns/dockerize-vps.git
cd dockerize-vps/nginx_proxy
docker-compose up -d
You need to follow the structure and files in /template
directory.
- If you have already run nginx in a docker container, just wrap it in a
/nginx
folder. - Otherwise, create a
/nginx
forlder, writeDockerfile
anddefault.conf
like/template
do. - Copy or edit your project
docker-compose.yml
file.
version: "2.0"
services:
nginx:
build: ./nginx
ports:
# any port that has not been used
- "8081:80"
links:
- backend
environment:
# your domain
- VIRTUAL_HOST=example.com
networks:
- default
# connect to nginx-proxy network
- nginx_proxy_default
backend:
build: ./backend
volumes:
- ./backend:/opt/workspace
# connect to nginx-proxy network
networks:
nginx_proxy_default:
external: true
- Run
docker-compose up -d
Start nginx_proxy
cd nginx_proxy
docker-compose up -d
Start site1
cd example/site1
docker-compose up -d
Start site2
cd example/site2
docker-compose up -d
Test sie1
curl -H "Host: site1.com" localhost
<html>
<head>
<meta charset="utf-8">
<title>Site 1</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Test site2
curl -H "Host: site2.com" localhost
<html>
<head>
<meta charset="utf-8">
<title>Site 2</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>