This guide is optimized for installing Mailcow: Dockerized on a VPS running Nginx Proxy Manager (NPM) and Portainer. It includes fixes for common issues like Redirect Loops and 504 Gateway Timeout Errors.

1. Prerequisites
- VPS with Docker & Docker Compose pre-installed.
- Domain name:
mail.khaisanfood.vn(pointing to VPS IP). - Nginx Proxy Manager is running in a Docker container.
DNS record
| Type | Name | Value | Proxy Status |
|---|---|---|---|
| A | Your VPS IP | DNS Only | |
| CNAME | autodiscover | mail.khaisanfood.vn | DNS Only |
| CNAME | autoconfig | mail.khaisanfood.vn | DNS Only |
| MX | @ | mail.khaisanfood.vn | Priority 10 |
| TXT | @ | v=spf1 mx ~all | – |
2. Installation Steps
Step 1: Clone Mailcow
cd /opt
git clone https://github.com/mailcow/mailcow-dockerized
cd mailcow-dockerized
Step 2: Generate Configuration
./generate_config.sh
Hostname: mail.khaisanfood.vn
Timezone: Asia/HoChiMinh
ClamAV: Y (nếu RAM > 4GB) hoặc N.
Step 3: Configure mailcow.conf (Important)
Edit the configuration to avoid conflicts and loops.
nano mailcow.conf
Change the following variables:
- Prevent Redirect Loops:
HTTP_REDIRECT=n
(We let Nginx Proxy Manager handle HTTPS, so turn off Mailcow's internal redirection). - Bind to All Interfaces (so NPM can see it):
HTTP_BIND=0.0.0.0HTTPS_BIND=0.0.0.0 - Change Ports (to avoid 80/443 conflicts):
HTTP_PORT=8080HTTPS_PORT=8443 - Avoid IP Conflicts (Fix “Pool overlaps”):
IPV4_NETWORK=192.168.201

Step 4: Firewall Setup (Fix 504 Error)
Ensure Docker containers can communicate with each other.
ufw allow from 172.16.0.0/12 to any
Step 5: Start Mailcow
docker compose pull
docker compose up -d
3. Nginx Proxy Manager Configuration
Access Nginx Proxy Manager and add a new Proxy Host.
Details Tab
- Domain Names:
mail.khaisanfood.vn - Scheme:
http(Important! Connect via HTTP) - Forward Hostname / IP:
172.17.0.1
(This is the standard Docker Gateway IP. Checkip addr show docker0to confirm). - Forward Port:
8080 - Cache Assets: Disabled
- Block Common Exploits: Enabled
SSL Tab
- SSL Certificate: Request a new Let's Encrypt certificate.
- Force SSL: Enabled.
- HTTP/2 Support: Enabled.
Click Save. Now you can access https://mail.khaisanfood.vn.

4. Post-Installation
Sync SSL Certificates (For Email Clients)
NPM handles web SSL, but Mailcow needs certificates for SMTP/IMAP. You need to copy them from the NPM Docker volume on the host.
Find your NPM Certificate ID: Check the NPM dashboard or look in the directory. Example Host Path: /var/lib/docker/volumes/nginx-proxy-managernpmletsencrypt/_data/live/npm-21 (id 21 will be different for each domain).
Sync Command:
# Cập nhật đường dẫn này khớp với Host Path của bạn tìm thấy ở trên
NPMCERTPATH="/var/lib/docker/volumes/nginx-proxy-managernpmletsencrypt/_data/live/npm-21"
cp "$NPMCERTPATH/fullchain.pem" /opt/mailcow-dockerized/data/assets/ssl/cert.pem
cp "$NPMCERTPATH/privkey.pem" /opt/mailcow-dockerized/data/assets/ssl/key.pem
cd /opt/mailcow-dockerized
docker compose restart postfix-mailcow dovecot-mailcow nginx-mailcow
Reset Admin Password
Reach the target audience admin / moohoo not working:
cd /opt/mailcow-dockerized/helper-scripts
bash mailcow-reset-admin.sh
DNS / DKIM
Log in to Mailcow UI -> Configuration -> Mail Setup -> DNS to get the DKIM record and add it to your DNS provider.
5. Adding Additional Domains (Multi-Domain Setup)
You can host multiple email domains (e.g., @dpsmedia.vn) on this same server.
A. Add Domain in Mailcow
- Log in to Mailcow UI as Admin.
- Go to Configuration -> Mail Setup -> Domains.
- Click Add Domain.
- Domain:
dpsmedia.vn(or your new domain). - Click Add Domain and restart SOGo.
B. DNS for New Domain (dpsmedia.vn)
Go to the DNS provider for dpsmedia.vn and add these records. Important: Point them to your current mail server URL (mail.khaisanfood.vn).
| Type | Name | Value | Status |
|---|---|---|---|
| MX | @ | mail.khaisanfood.vn | Priority 10 |
| TXT | @ | v=spf1 mx include:mail.khaisanfood.vn ~all | – |
| CNAME | autodiscover | mail.khaisanfood.vn | DNS Only |
| CNAME | autoconfig | mail.khaisanfood.vn | DNS Only |
| TXT | dkim._domainkey | (Copy value from Mailcow UI -> DNS) | – |
| TXT | _dmarc | v=DMARC1; p=none; | – |
(You do NOT need a new IP address. Everything goes through the main IPs).
C. (Optional) Access Mailcow UI via mail.dpsmedia.vn
If you want to access the Admin UI via https://mail.dpsmedia.vn:
Edit Config:
nano mailcow.conf
ADDITIONALSERVERNAMES=mail.dpsmedia.vn
docker compose up -d
Nginx Proxy Manager: Copy the exact settings of mail.khaisanfood.vn.
D. SSL Certificates for Additional Domains
Option 1: (Recommended) Use mail.khaisanfood.vn for Incoming/Outgoing Server.
Option 2: (Advanced) Enable ENABLESSLSNI=y in config, create a separate SSL directory and sync the new certificate there.
6. Fix Email Sending (Gmail Block)
If you encounter an error 550-5.7.1, it means your IP has low reputation.
A. Set Up Reverse DNS (PTR) – Mandatory
Go to your VPS provider's Dashboard, set the PTR Record for your IP to mail.khaisanfood.vn.
B. Use SMTP Relay
If the IP is blacklisted, use Amazon SES, SendGrid, or Mailgun as a Relay.
7. Uninstallation Guide
Warning: This will delete ALL email data.
cd /opt/mailcow-dockerized
docker compose down -v
cd /opt
rm -rf mailcow-dockerized
Delete Proxy Host in NPM.
Ý kiến bạn đọc (9)
Đăng nhập để bình luận
Đăng nhập nhanh 1-chạm bằng Google để chia sẻ ý kiến của bạn về bài viết.
Bằng cách đăng nhập, bạn đồng ý với điều khoản và chính sách cộng đồng.
Mailcow mail to Gmail is being blocked. Is configuring SPF and DKIM alone enough? The article mentions PTR and SMTP Relay, and we want to know which option to prioritize for a newly rented VPS.
SPF and DKIM are only part of the picture. PTR records are very important for self-managed mail servers, and you also need to pay attention to DMARC, IP reputation, and sending history. For a new VPS where the provider restricts SMTP or the IP has no established reputation, an SMTP relay is usually more practical.
We once sent from a new VPS ourselves. Internal sending was fine, but quite a lot of Gmail messages landed in spam. After switching outbound mail to a relay, the delivery rate became noticeably more stable, though it means paying an additional service fee.
Changing HTTP_PORT=8080 and having Nginx Proxy Manager forward to 172.17.0.1:8080 is quite important. If NPM and Mailcow are on different Docker networks, is this approach still viable, or should the two containers share a network?
If you use a Docker gateway, you need to check the correct docker0 IP on the VPS and not assume every machine is the same. Sharing one network is usually easier to manage, but you must avoid letting the Mailcow configuration conflict with the existing network.
I once hit a 504 at this exact step because the firewall was blocking traffic between Docker subnets. Open the correct Docker network range and check port 8080 from the NPM side before concluding that Mailcow is at fault.
A redirect loop with Nginx Proxy Manager is indeed easy to run into if you enable HTTPS redirect in Mailcow and also Force SSL in NPM at the same time. Disabling HTTP_REDIRECT and letting one proxy layer handle it is more reasonable.
For ClamAV, you should consider RAM very carefully. A 2GB VPS running all Mailcow services is quite tight and can easily cause the machine to lag when there is a lot of mail.
The multi-domain section is quite useful for businesses with multiple brands. However, you should carefully check the DNS, DKIM, and certificates for each domain. Don't assume that adding a domain in Mailcow means it works right away.
Chưa có bình luận nào. Hãy là người đầu tiên chia sẻ cảm nghĩ!
Đăng nhập / Tạo tài khoản
Đăng nhập với Google để gửi bình luận và tương tác cùng cộng đồng.
Tiếp tục là đồng ý với điều khoản sử dụng và chính sách bảo mật của DPS Media.