Setting up Nginx Traffic Redirect for Subdomains on a VPS with Multiple Docker Containers and Cloudflare
In this article, we will discuss how to set up Nginx traffic redirect for subdomains on a VPS with multiple Docker containers, while the domain name is hosted on Cloudflare. This setup is particularly useful when you have a limited amount of available ports on your VPS and want to run multiple applications on different subdomains.
Prerequisites
- A VPS with a public IP address and root access
- A domain name hosted on Cloudflare
- Docker installed on the VPS
- Basic knowledge of Nginx, Docker, and Cloudflare
Setting up Nginx Traffic Redirect
To set up Nginx traffic redirect, we need to perform the following steps:
- Create a new Nginx configuration file
- Define server blocks for each subdomain
- Configure reverse proxy for each Docker container
- Restart Nginx
Step 1: Create a new Nginx configuration file
Create a new Nginx configuration file by running the following command:
sudo nano /etc/nginx/nginx.confAdd the following configuration to the file:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files
include /etc/nginx/conf.d/\*.conf;
}Step 2: Define server blocks for each subdomain
Create a new directory for server blocks by running the following command:
sudo mkdir /etc/nginx/sites-availableCreate a new server block configuration file for each subdomain. For example, if you have two subdomains, subdomain1.example.com and subdomain2.example.com, create two files:
sudo nano /etc/nginx/sites-available/subdomain1.example.com.confsudo nano /etc/nginx/sites-available/subdomain2.example.com.confAdd the following configuration to each file:
server {
listen 80;
server_name subdomain1.example.com;
location / {
proxy\_pass http://subdomain1:8080;
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-Proto $scheme;
}
}
server {
listen 80;
server\_name subdomain2.example.com;
location / {
proxy\_pass http://subdomain2:8080;
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-Proto $scheme;
}
}Replace subdomain1.example.com and subdomain2.example.com with your actual subdomains, and http://subdomain1:8080 and http://subdomain2:8080 with the URLs of your Docker containers.
Step 3: Configure reverse proxy for each Docker container
Configure each Docker container to listen on a different port. For example, if you have two Docker containers, configure them to listen on ports 8080 and 8081.
Add the following configuration to each Docker container:
version: '3'
services:
subdomain1:
image: your\_image\_name
ports:
- "8080:80"
subdomain2:
image: your\_image\_name
ports:
- "8081:80"Step 4: Restart Nginx
Restart Nginx to apply the new configuration:
sudo systemctl restart nginxConfiguring Cloudflare
To configure Cloudflare, perform the following steps:
- Log in to your Cloudflare account
- Select the domain name you want to use
- Click on the DNS button
- Create a new A record for each subdomain with the IP address of your VPS
- Click on the SSL/TLS button
- Select the Full SSL mode
In this article, we discussed how to set up Nginx traffic redirect for subdomains on a VPS with multiple Docker containers, while the domain name is hosted on Cloudflare. This setup allows you to run multiple applications on different subdomains with a limited amount of available ports on your VPS.
References
- Nginx: https://nginx.org/en/docs/
- Docker: https://docs.docker.com/
- Cloudflare: https://www.cloudflare.com/