- 2 Mail Server Setup and 2.1 Setting Up SSL for the Mail Server must be done.
Assumptions
- NGINX was installed and configured by iRedMail (version 1.26 from the Rocky Linux 10 repository).
AandAAAArecords exist formail.xenonly.comandiredadmin.xenonly.com.- Commands are run on the server with root rights (
sudo).
Understanding the Current Configuration#
iRedMail’s Default Layout#
The NGINX on the system was installed by iRedMail, and iRedMail changed its configuration.
On a normal RHEL installation, the include directive in /etc/nginx/nginx.conf includes the conf.d directory. iRedMail uses the same directory layout on every distribution, so during the installation it rewrites nginx.conf and includes the sites-enabled and conf-enabled directories.
In the steps part, I’ll change this layout like this:
conf.dfor configuration filesvhost.dfor virtual host definitions
Templates (/etc/nginx/templates)#
Under /etc/nginx/templates there are the default templates iRedMail brought. The netdata.tmpl, netdata-subdomain.tmpl, sogo.tmpl, sogo-subdomain.tmpl and adminer.tmpl templates won’t be used; we’ll delete them in the steps part.
Let’s look at the other templates.
fastcgi_php.tmpl#
Has the shared settings for passing PHP requests to PHP-FPM (
php_workers→127.0.0.1:9999). Roundcube and the other PHP blocks include this file.
hsts.tmpl#
Sends the
Strict-Transport-Securityheader to the browser. A browser that gets this header connects to the site only over HTTPS for 1 year.
The current NGINX version (1.26) doesn’t support the add_header_inherit directive. So if add_header is defined in a virtual host or a location block, the headers in conf.d/headers.conf are not applied in that block.
The hsts.tmpl template is included from the roundcube-subdomain.tmpl, roundcube.tmpl and iredadmin-subdomain.tmpl templates.
So in the steps part we’ll add the definition in hsts.tmpl to headers.conf, remove the include lines from these three templates and delete hsts.tmpl.
iredadmin-subdomain.tmpl#
A ready configuration for serving the admin panel that comes with iRedMail on a subdomain. This is what we’ll use on our system too.
iredadmin.tmpl#
For the admin panel that comes with iRedMail; it passes requests for the
/iredadminpath to iRedAdmin.
We’ll serve iRedAdmin on a subdomain, so we’ll delete this one in the steps part.
misc.tmpl#
- Handles
.well-knownrequests that come over HTTPS - Blocks access to hidden files
- Turns off logging for
favicon.icoandrobots.txtrequests
php-catchall.tmpl#
Passes every
.phprequest not caught by the earlier templates to PHP-FPM.
This is not what we want; we’ll decide ourselves what runs where. So we don’t need it, and we’ll delete it in the steps part.
redirect_to_https.tmpl#
Redirects requests for the
sogo,iredadminand
In the current setup every request is already redirected to HTTPS. We don’t need it; we’ll delete it in the steps part.
roundcube-subdomain.tmpl#
A ready configuration for serving Roundcube webmail on a subdomain.
roundcube.tmpl#
Used for Roundcube webmail; passes requests for the
On this system Roundcube will be served on a subdomain, but for other domains we may need to serve it on the /mail path. So I won’t delete it.
ssl.tmpl#
The standard SSL settings for HTTPS, and the definition of the SSL certificate that iRedMail, which set up our mail server, uses.
We’ll use this server for many services and virtual hosts, so in the steps part we’ll remove the certificate definition from here.
Also, the settings in ssl.tmpl can be set at the http level, and we’ll use them for every virtual host. So instead of including it in every virtual host configuration, we’ll move it to the conf.d directory.
stub_status.tmpl#
Serves the NGINX (
/stub_status) and PHP-FPM (/status) status pages for monitoring tools, reachable only fromlocalhost.
I don’t plan to use it, so I’ll delete it in the steps part.
Configuration Files (/etc/nginx/conf-available → conf.d)#
Applied at the http {} level; unless something else is set in a virtual host or location block, they apply to every request.
I’ll only look at the ones that need to change.
What We’ll Change#
cache.conf#
We’ll add the current JavaScript MIME types (application/javascript, text/javascript).
headers.conf#
We’ll remove the outdated X-XSS-Protection and X-Download-Options headers and add the Strict-Transport-Security definition from hsts.tmpl here. We’ll also remove the Content-Security-Policy header iRedMail set.
Virtual Hosts (/etc/nginx/sites-available → vhost.d)#
The current configuration has only the default virtual host definitions and the templates included in them.
00-default.conf#
Its default version roughly does this:
- Listens on port 80 (HTTP)
- Serves
.well-known/acme-challengerequests for SSL from the/opt/www/well_knowndirectory - Redirects all other requests to HTTPS
00-default-ssl.conf#
Its default version roughly does this:
- Listens on port 443 (HTTPS)
- Has
/var/www/htmlas the root directory andindex.phpas the index - Then includes a set of templates from the
templatesdirectory
By default these files are included:
misc.tmpl → Will be included in the vhosts that need it
ssl.tmpl → Will be moved to conf.d
iredadmin.tmpl → Will be served on a subdomain
roundcube.tmpl → Will be served on a subdomain
php-catchall.tmpl → Not used
stub_status.tmpl → Not usedThe Layout We Want#
Every request that comes to port 80 (HTTP), except the acme challenge, should be redirected to 443 (HTTPS). The conf.d directory should hold the default settings for every request, and the vhost.d directory the configuration of each virtual host.
Steps#
Directory Layout#
As we said in the understanding part, we’ll use the conf.d directory for configuration files and vhost.d for virtual host definitions.
The whole conf-available directory becomes conf.d, so if there is a file there with no link in conf-enabled (so a file not in use right now), it will start being loaded too. Let’s compare the two directories before the move:
ls /etc/nginx/conf-available
ls -l /etc/nginx/conf-enabledThen let’s rearrange the directories:
cd /etc/nginx
sudo rm -rf conf.d # If it exists; it isn't used right now anyway
sudo mv conf-available conf.d
sudo mv sites-available vhost.d
sudo rm -rf sites-enabled conf-enabledand change the conf-enabled and sites-enabled include lines in nginx.conf like this:
http {
...
# Requires Nginx 1.25.1+.
# http2 on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/vhost.d/*.conf;
}Templates#
hsts.tmpl#
As we said in the understanding part, the hsts.tmpl template is included from three templates:
sudo grep -rn hsts.tmpl /etc/nginx/templates/etc/nginx/templates/iredadmin-subdomain.tmpl:4:include /etc/nginx/templates/hsts.tmpl;
/etc/nginx/templates/roundcube-subdomain.tmpl:19: include /etc/nginx/templates/hsts.tmpl;
/etc/nginx/templates/roundcube-subdomain.tmpl:23: include /etc/nginx/templates/hsts.tmpl;
/etc/nginx/templates/roundcube-subdomain.tmpl:28: include /etc/nginx/templates/hsts.tmpl;
/etc/nginx/templates/roundcube.tmpl:27: include /etc/nginx/templates/hsts.tmpl;
/etc/nginx/templates/roundcube.tmpl:32: include /etc/nginx/templates/hsts.tmpl;Let’s delete the include /etc/nginx/templates/hsts.tmpl; lines in these three templates:
sudo sed -i '/hsts.tmpl/d' /etc/nginx/templates/{iredadmin-subdomain,roundcube-subdomain,roundcube}.tmplWe’ll add the definition in hsts.tmpl to headers.conf below, and delete the template itself together with the unused templates.
ssl.tmpl#
Let’s delete the lines starting with ssl_certificate and ssl_certificate_key in /etc/nginx/templates/ssl.tmpl and move the file to the conf.d directory:
sudo sed -i '/^\s*ssl_certificate/d' /etc/nginx/templates/ssl.tmpl
sudo mv /etc/nginx/templates/ssl.tmpl /etc/nginx/conf.d/ssl.confConfiguration Files#
cache.conf#
/etc/nginx/conf.d/cache.conf will look like this:
map $sent_http_content_type $expires {
default off;
application/javascript 1d;
text/javascript 1d;
application/x-javascript 1d;
text/css 1d;
~image/ 1d;
}
expires $expires;map only defines the $expires variable. For the variable to be used, the expires $expires; line at the bottom is needed; without it the map has no effect.
headers.conf#
Let’s edit /etc/nginx/conf.d/headers.conf like this:
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Frame-Options sameorigin always;
add_header X-Content-Type-Options nosniff always;
add_header X-Permitted-Cross-Domain-Policies none always;
add_header Referrer-Policy strict-origin always;Without the always parameter, NGINX adds the headers only to successful and redirect responses (200, 301, 304 and so on); it doesn’t send them with 403, 404 and 5xx responses.
We removed the Content-Security-Policy header iRedMail set. Its value, default-src https: data: 'unsafe-inline' 'unsafe-eval', allows every HTTPS source, inline scripts and eval, so it gives almost no protection against XSS. On the other hand, because it is set at the http level, it goes to every application behind NGINX, and since it blocks blob: and wss: sources it can break applications. Applications that need a CSP (for example Vaultwarden) send their own header.
Virtual Hosts#
00-default.conf#
Expected behavior: redirect every request except the
acme challengeto 443/HTTPS.
We add the default_server parameter to the listen 80 and listen [::]:80 directives in the current configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Allow ACME challenge to be served over HTTP (don't redirect to HTTPS).
location ~* ^/.well-known/acme-challenge/ {
root /opt/www/well_known;
try_files $uri =404;
allow all;
}
# Redirect all insecure http requests to https
location / {
return 301 https://$host$request_uri;
}
}00-default-ssl.conf#
Expected behavior: there will be a separate definition for each virtual host, so this one takes the requests that no virtual host catches.
We edit /etc/nginx/vhost.d/00-default-ssl.conf like this:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
}If no server_name (SNI) matches an incoming request, the TLS handshake is rejected.
As we said in the understanding part, the templates that used to be included here are no longer used.
After this configuration, HTTPS requests to names that have no virtual host (for example https://xenonly.com, or straight to the IP address) are rejected. The /mail and /iredadmin addresses from 2 Mail Server Setup are now used through the subdomains below.
vhost_mail.xenonly.com.conf#
We’ll use it to serve Roundcube Webmail. Let’s create /etc/nginx/vhost.d/vhost_mail.xenonly.com.conf like this:
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name mail.xenonly.com;
ssl_certificate /etc/letsencrypt/live/mail.xenonly.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mail.xenonly.com/privkey.pem;
include /etc/nginx/templates/misc.tmpl;
include /etc/nginx/templates/roundcube-subdomain.tmpl;
}We got the certificate in 2.1 Setting Up SSL for the Mail Server, so we don’t get it again.
Using a subdomain is purely a personal choice; it can also be served on a path like /mail.
If we need to serve the webmail for more than one domain, each domain will have its own certificate, so we need to create a separate virtual host or add a new server block to this file.
vhost_iredadmin.xenonly.com.conf#
First let’s get the certificate:
sudo certbot certonly --webroot -w /opt/www/well_known -d iredadmin.xenonly.comThe deploy hook we created in 2.1 Setting Up SSL for the Mail Server reloads NGINX when the certificate is renewed.
Then let’s create /etc/nginx/vhost.d/vhost_iredadmin.xenonly.com.conf like this:
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name iredadmin.xenonly.com;
ssl_certificate /etc/letsencrypt/live/iredadmin.xenonly.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/iredadmin.xenonly.com/privkey.pem;
include /etc/nginx/templates/misc.tmpl;
include /etc/nginx/templates/iredadmin-subdomain.tmpl;
}Using a subdomain is purely a personal choice; it can also be served on the /iredadmin path.
It is an admin panel and doesn’t need to be reached from outside, so it can be made reachable only over the VPN. We’ll do that in 7.2 Putting Services Behind the VPN.
Deleting the Unused Templates#
The templates we said won’t be used in the understanding part:
- netdata.tmpl
- netdata-subdomain.tmpl
- sogo.tmpl
- sogo-subdomain.tmpl
- adminer.tmpl
- stub_status.tmpl
- redirect_to_https.tmpl
- php-catchall.tmpl
- iredadmin.tmpl
- hsts.tmpl
cd /etc/nginx/templates
sudo rm -f netdata.tmpl netdata-subdomain.tmpl sogo.tmpl sogo-subdomain.tmpl adminer.tmpl
sudo rm -f stub_status.tmpl redirect_to_https.tmpl php-catchall.tmpl iredadmin.tmpl hsts.tmplRight now NGINX serves only Roundcube webmail and the iRedAdmin panel. More sites will come later and we’ll add their definitions too. That’s it for now.
Checking#
Let’s check the configuration and restart the service:
sudo nginx -t
sudo systemctl restart nginxTroubleshooting#
If nginx -t gives an error, the problem is most likely invisible or formatted characters that got mixed into the configuration text while copying it from the document.
sudo cat -A /etc/nginx/vhost.d/<FILE>.conf | headWith this command we can check whether the problem comes from character formatting. Non-breaking space (NBSP) characters show up as M-BM- in the lines.
sudo sed -i 's/\xC2\xA0/ /g' /etc/nginx/vhost.d/<FILE>.confAnd with this command we can fix it.
text/css or image/png. The server sends it in the Content-Type header..php requests to PHP-FPM over a port or a socket.mail.xenonly.com). This way one IP address can serve many sites, each with its own certificate.https:// and secure mail connections. The server proves who it is with a certificate, for example one from Let’s Encrypt.server_name), so one server can host many domains.