Nginx Reverse Proxy for iRedMail: Setup Guide
In this article
This guide walks you through placing Nginx as a reverse proxy in front of iRedMail, so all HTTP, HTTPS, and mail-related web traffic (like Roundcube, SOGo, and the admin panel) flows through Nginx first. This gives you centralized SSL termination, easier certificate management, and the ability to add caching or load balancing later. You’ll need a working iRedMail server (with its built-in web server still active or stopped), a separate Nginx installation (or you can install Nginx on the same machine), and a domain name pointing to your server.
What You'll Need
- A server with iRedMail already installed and configured (iRedMail uses Nginx or Apache internally, depending on your setup).
- Root or sudo access to the server.
- Nginx installed (if not using the one bundled with iRedMail). If iRedMail uses Nginx, you can adapt that config; if it uses Apache, install Nginx separately.
- A domain name (or subdomain) for the webmail and admin interfaces, with DNS A/AAAA records pointing to your server’s public IP.
- SSL certificates (Let’s Encrypt or your own) for the domain(s) you’ll proxy.
Step-by-Step
- Identify the internal web server port. iRedMail typically runs Roundcube or SOGo on port 80/443, or on a custom port like 8080 for the backend. Log into your server and check the configuration. For example, if iRedMail uses Apache, the config is in
/etc/httpd/conf/httpd.confor/etc/apache2/. If it uses Nginx, look in/etc/nginx/conf.d/. Note the port (often 80 for HTTP, 443 for HTTPS, but sometimes 8080). - Decide on the proxy architecture. The simplest approach: Nginx listens on the public IP (ports 80 and 443) and proxies to the iRedMail internal web server on a different port (e.g., 8080). If iRedMail’s web server is already using 80/443, you’ll need to change its listening port to something else (like 8080 for HTTP and 8443 for HTTPS) or stop it entirely and let Nginx handle all web traffic.
- Adjust the internal web server’s port. If needed, edit the iRedMail web server config to listen on a non-standard port. For Apache, edit the
Listendirective and virtual host ports. For Nginx (if iRedMail uses it), edit thelistendirectives. Restart the service to apply changes. Test that the webmail is still accessible via the new port (e.g.,http://localhost:8080). - Create an Nginx site configuration. Create a new file in
/etc/nginx/conf.d/or/etc/nginx/sites-available/(depending on your distro). Name it something likemail-proxy.conf. The configuration will define a server block for your domain, listening on 80 and 443, with SSL settings, and proxy rules. - Write the proxy server block. Here’s a basic template for HTTP (port 80) that redirects to HTTPS, and HTTPS (port 443) that proxies to the internal web server. Replace
mail.example.comwith your domain and8080with the actual backend port.server { listen 80; server_name mail.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl http2; server_name mail.example.com; ssl_certificate /etc/letsencrypt/live/mail.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/mail.example.com/privkey.pem; location / { proxy_pass http://127.0.0.1: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; } }If the backend uses HTTPS (e.g., port 8443), changeproxy_passtohttps://127.0.0.1:8443and addproxy_ssl_verify off;(or better, set the CA path if you have internal CAs). - Adjust for WebSocket and long-polling. Roundcube and SOGo may use WebSockets for real-time features. Add these lines inside the
location /block to support them:proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; - Set correct proxy timeouts. Mail clients can take time to upload/download large attachments. Increase timeouts to avoid 504 errors:
proxy_connect_timeout 75s; proxy_send_timeout 600s; proxy_read_timeout 600s; - Test the configuration. Run
nginx -tto check for syntax errors. If it’s all good, reload Nginx withsystemctl reload nginx(orservice nginx reload). - Update iRedMail’s trusted proxies. iRedMail applications (like Roundcube) need to know that requests from Nginx are trusted, so they can log the real client IP. In iRedMail’s web server config, set the trusted proxy to
127.0.0.1(or the IP of the Nginx server). For Roundcube, edit/etc/roundcubemail/config.inc.phpand add:$config['proxy_whitelist'] = ['127.0.0.1'];For SOGo, adjust theWOPortandWOHostsettings if needed. Restart the web server. - Adjust firewall rules. Ensure that only Nginx listens on ports 80 and 443 externally. If the internal web server is listening on 8080, block that port from external access (e.g., with
ufw deny 8080or iptables). This prevents users from bypassing Nginx. - Test the setup. Open your domain in a browser. You should see the webmail login page. Check that the URL stays your domain, and that the SSL certificate is valid. Test sending and receiving email to ensure the proxy doesn’t break anything.
Common Mistakes
- Forgetting to change the backend port – If iRedMail’s web server still uses 80/443, Nginx can’t bind to those ports. Always change the backend to a different port first.
- Not setting the Host header – Without
proxy_set_header Host $host;, the backend may generate links with the wrong domain or IP. - Ignoring WebSocket support – Roundcube and SOGo’s real-time features will break without the Upgrade headers.
- Blocking the backend port in the firewall – If you block 8080 but Nginx is on the same machine, it still works, but if Nginx is on a different machine, you need to allow access from Nginx’s IP.
- Using HTTP instead of HTTPS to the backend – If the backend is HTTPS, you must use
proxy_pass https://...and handle the SSL certificate verification.
Tips
- Use a single Nginx instance – If iRedMail already uses Nginx, you can modify its existing config instead of adding a second Nginx. Just add a new server block for the public domain and change the original to listen on a different port.
- Enable HTTP/2 – It improves performance and is already included in the template above.
- Set up SSL certificates with Certbot – After Nginx is running, use
certbot --nginxto automatically obtain and configure Let’s Encrypt certificates. - Monitor logs – Check Nginx’s error log (
/var/log/nginx/error.log) and access log for any proxy errors. - Consider caching static assets – You can add a
location ~* \.(css|js|png|jpg)$block to cache static files, reducing load on the backend.
Troubleshooting
- 502 Bad Gateway – Nginx can’t connect to the backend. Check that the backend web server is running and listening on the correct port. Use
ss -tlnpto see listening ports. Verify theproxy_passURL matches. - 504 Gateway Timeout – The backend took too long to respond. Increase the timeout values in the Nginx config, or check if the backend is overloaded.
- SSL errors – If you see “SSL certificate error” in the browser, ensure the certificate paths are correct and the certificate covers the domain. Test with
curl -v https://mail.example.com. - Redirect loop – This happens if the backend is also trying to redirect to HTTPS. Make sure the backend is not forcing HTTPS (or that it trusts the X-Forwarded-Proto header). You may need to set
proxy_set_header X-Forwarded-Proto $scheme;and configure the backend to honor it. - Login works but pages are broken – Often a missing Host header or X-Forwarded-Proto. Check that the backend generates correct URLs by looking at the page source.
- WebSocket connection fails – Ensure the Upgrade headers are set and that the backend supports WebSockets. Test with a browser console for errors.
Frequently Asked Questions
Can I use Nginx as a reverse proxy for iRedMail if it’s on a different server?
Yes, just change the proxy_pass to the backend server’s IP address (e.g., http://192.168.1.10:8080). Ensure the firewall allows traffic from Nginx to that port.
Do I need to change the iRedMail web server’s port?
Only if you want Nginx to listen on the standard ports (80/443). If you’re okay with Nginx using non-standard ports, you can leave the backend as is, but then users would need to specify the port in the URL.
Will this break email sending/receiving?
No, the reverse proxy only affects web traffic (HTTP/HTTPS). SMTP (port 25), IMAP (143/993), and POP3 (110/995) are unaffected unless you also proxy those, which is not covered here.
How do I get a Let’s Encrypt certificate for my domain?
Install Certbot, then run certbot --nginx -d mail.example.com. It will automatically modify your Nginx config to use the certificate and set up auto-renewal.
Can I proxy both webmail and admin panel through Nginx?
Yes, you can create separate server blocks or use location blocks for different paths (e.g., /mail for Roundcube, /admin for iRedAdmin). Just set up the appropriate proxy_pass for each location.
What if iRedMail uses Apache instead of Nginx?
The process is the same. You install Nginx separately, change Apache’s listening port (e.g., to 8080), and then proxy from Nginx to Apache. Make sure to set the X-Forwarded-Proto header so Apache knows the original scheme.