Apache Reverse Proxy Configuration: Accessing Applications by Subdomains

Channel: Linux & Server Guides Linux
Abstract: you simply want to add a virtual host block for the regular domain If you are running Nextcloud as a regular installation and you want t

This tutorial explains how to set up an Apache reverse proxy to access different applications running on your own server using different subdomains.

Preprequisites
  • Root access (via SSH) to a Linux Server (VPS, Dedicated Server, Home Serve)
  • A domain name (free or bought)
1. Get a domain name

You can either get a free domain name from providers such as ClouDNS, or buy one, e.g. from hosting companies such as Hostinger.

2. Add CNAME DNS entries for each subdomain

Add a CNAME entry for each subdomain required. Assuming that you want to run a regular Apache website, a WordPress blog, and a Nextcloud server on the same machine, we want to point the host 「blog」 and 「cloud」 to our domain name as follows:

Example DNS configuration on HostingerExample CNAME entries on ClouDNS

Also make sure to select a low TTL (time to live) time which allows you to quickly change the CNAME entries of you need to. You can then set a higher TTL once you have got everything set up.

3. Setup the Apache reverse proxy

You can now add your subdomain names in the 000-default.conf file to route your subdomains to different directories or applications running on your server.

First, you simply want to add a virtual host block for the regular domain:

<VirtualHost *:80>
        ServerName yourdomain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you are running Nextcloud as a regular installation and you want to route the domain cloud.yourdomain.com to it, you need to add the following virtual host block:

<VirtualHost *:80>
        ServerName cloud.yourdomain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nextcloud
        <Directory /var/www/html/nextcloud/>
                Options +FollowSymlinks
                AllowOverride All
                <IfModule mod_dav.c>
                        Dav off
                </IfModule>
                SetEnv HOME /var/www/html/nextcloud
                SetEnv HTTP_HOME /var/www/html/nextcloud

                RewriteEngine On
                RewriteRule ^/\.well-known/carddav http://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
                RewriteRule ^/\.well-known/caldav http://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
                RewriteRule ^/\.well-known/host-meta http://%{SERVER_NAME}/public.php?service=host-meta [QSA,L]
                RewriteRule ^/\.well-known/host-meta\.json http://%{SERVER_NAME}/public.php?service=host-meta-json [QSA,L]
                RewriteRule ^/\.well-known/webfinger https://%{SERVER_NAME}/public.php?service=webfinger [QSA,L]
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Furthermore, you need to edit the config file to allow connections to Nextcloud on the new domain:

<?php
$CONFIG = array (
...
  'trusted_domains' => 
array (
        0 => 'https://cloud.yourdomain.com',
),
);

If you are running Nextcloud as a snap instance however, the required block is slightly different. Note that in this case we use the ProxyPass and ProxyPassReverse directives to route access on the subdomain to the snap instance:

<VirtualHost *:80>
        ServerName cloud.yourdomain.com
        ServerAdmin [email protected]
        ProxyPreserveHost On
        ProxyRequests Off
        RewriteEngine On
        RewriteRule ^/\.well-known/carddav http://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
        RewriteRule ^/\.well-known/caldav http://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
        RewriteRule ^/\.well-known/host-meta http://%{SERVER_NAME}/public.php?service=host-meta [QSA,L]
        RewriteRule ^/\.well-known/host-meta\.json http://%{SERVER_NAME}/public.php?service=host-meta-json [QSA,L]
        RewriteRule ^/\.well-known/webfinger http://%{SERVER_NAME}/public.php?service=webfinger [QSA,L]
        ProxyPass / http://localhost:81/
        ProxyPassReverse / http://localhost:81/
</VirtualHost>

Additionally, you must change the default port on which the nextcloud snap is running, e.g. to port 81 as we need must use port 80 to accept un-encrypted connections to our server.

sudo snap set nextcloud ports.http=81

Again, you need to add this domain to your trusted domains

<?php
$CONFIG = array (
...
  'trusted_domains' => 
array (
        0 => 'https://cloud.yourdomain.com',
),
);

Finally, if you want to link to another applications, e.g. a wiki running under port 8080 this would be the required virtual host block:

<VirtualHost *:80>
        ServerName wiki.yourdomain.com
        ServerAdmin [email protected]
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/
</VirtualHost>

As a next step, I would highly recommend you to secure your traffic using an SSL certificate! Check out this follow up guide on how to set up a Free Wildcard SSL Certificate for Nextcloud and WordPress.

Ref From: techguides

Related articles