Chạy nhiều service trên một VPS nhưng muốn gom về 1 domain chuẩn SSL? Nginx reverse proxy là cách nhanh, dễ maintain và rất hợp stack Docker Compose. Bài này đi thẳng vào cấu hình thực tế.
Kiến trúc mục tiêu
- Nginx public:80/443
- App services chạy private network
- SSL từ Let’s Encrypt
- Routing theo subdomain
Compose mẫu cho app backend
services:
app:
image: myapp:latest
expose:
- "8080"
networks:
- internal
networks:
internal:
driver: bridge
Nginx reverse proxy config cơ bản
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://app: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;
}
}
Bật SSL Let’s Encrypt
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d api.example.com
sudo certbot renew --dry-run
Lỗi thường gặp và cách xử lý
- 502 Bad Gateway: kiểm tra service name/port trong proxy_pass
- SSL issue: DNS chưa trỏ đúng hoặc port 80/443 bị chặn
- Redirect loop: thiếu X-Forwarded-Proto hoặc config app sai
Setup đúng từ đầu giúp giảm rất nhiều sự cố khi mở rộng thêm service mới sau này.
