Run web applications under subdomain with Nginx

Attention

Check out the lightweight on-premises email archiving software developed by iRedMail team: Spider Email Archiver.

Introduction

By default, Roundcube, SOGo, Netdata and iRedAdmin are located at /mail, /SOGo, /netdata and /iredadmin. If you have SOGo but no Roundcube, /mail will be redirected to /SOGo too.

For example, if your server hostname is mail.example.com, and you correctly added an A type DNS record pointed to this iRedMail server, you should be able to visit them with URLs below:

The URIs are defined in the catch-all Nginx web host config file /etc/nginx/sites-available/00-default-ssl.conf, here's its full content:

#
# Note: This file must be loaded before other virtual host config files,
#
# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name _;      # <- Name `_` will catch all web domain names,
                        # <- this is why this file must be loaded before other
                        # <- web host config files.

    root /var/www/html;
    index index.php index.html;

    include /etc/nginx/templates/misc.tmpl;         # <- Misc
    include /etc/nginx/templates/ssl.tmpl;          # <- SSL related configurations.
    include /etc/nginx/templates/iredadmin.tmpl;    # <- iRedAdmin
    include /etc/nginx/templates/roundcube.tmpl;    # <- Roundcube webmail
    include /etc/nginx/templates/sogo.tmpl;         # <- SOGo Groupware
    include /etc/nginx/templates/netdata.tmpl;      # <- Netdata monitor
    include /etc/nginx/templates/php-catchall.tmpl; # <- php support
    include /etc/nginx/templates/stub_status.tmpl;  # <- Nginx status monitoring
}

As you can see, it loads multiple configuration snippets, and they define the URIs you can access to visit the web applications.

iRedMail also generates configuration snippet files to run them under subdomain:

Run web applications under subdomain

To run Roundcube, SOGo and/or iRedAdmin under subdomain, you can simply create a new web host config file, and load the *-subdomain.tmpl file.

Let's say you want to run Roundcube under subdomain webmail.example.com.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name webmail.example.com;

    include /etc/nginx/templates/misc.tmpl;
    include /etc/nginx/templates/ssl.tmpl;
    include /etc/nginx/templates/roundcube-subdomain.tmpl;
}
ln -sf /etc/nginx/sites-available/webmail.example.com.conf /etc/nginx/sites-enabled/webmail.example.com.conf
include /etc/nginx/templates/roundcube.tmpl;
service nginx restart

Extra steps for SOGo

SOGo is only accessible by /SOGo URI, so when you visit https://your-site.com/ it won't redirect to https://your-site.com/SOGo. In this case you must add a location directive for / URI and redirect requests to /SOGo.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name webmail.example.com;

    # Redirect homepage to /SOGo.
    location = / {
        return 302 https://$host/SOGo;
    }

    include /etc/nginx/templates/misc.tmpl;
    include /etc/nginx/templates/ssl.tmpl;
    include /etc/nginx/templates/subdomain-sogo.tmpl;
}

Important notes