user13682508
user13682508

Reputation:

Apache redirects to wrong virtual host

I am trying to set up two virtual hosts, one for my website and another for my nextcloud instance. This is what my config files look like:

/etc/apache2/site-available/000-default.conf

<IfModule mod_ssl.c>
  <VirtualHost _default_:80>
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot "/var/www/html"
    CustomLog /var/log/apache2/nc-access.log combined
    ErrorLog  /var/log/apache2/nc-error.log
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
  </VirtualHost>
  <Directory /var/www/html/>
    Options +FollowSymlinks
    AllowOverride All
    <IfModule mod_dav.c>
      Dav off
    </IfModule>
    LimitRequestBody 0
    SSLRenegBufferSize 10486000
  </Directory>
</IfModule>

/etc/apache2/site-available/nextcloud.conf

<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    ServerName nextcloud.domain.com
    DocumentRoot "/var/www/nextcloud"
    CustomLog /var/log/apache2/nc-access.log combined
    ErrorLog  /var/log/apache2/nc-error.log
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
  </VirtualHost>
  <Directory /var/www/nextcloud/>
    Options +FollowSymlinks
    AllowOverride All
    <IfModule mod_dav.c>
      Dav off
    </IfModule>
    LimitRequestBody 0
    SSLRenegBufferSize 10486000
  </Directory>
</IfModule>

Output of apache2ctl -S, showing the activated hosts:

VirtualHost configuration:
*:80                   domain.com (/etc/apache2/sites-enabled/000-default.conf:2)
*:443                  nextcloud.domain.com (/etc/apache2/sites-enabled/nextcloud.conf:2)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default 
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

When I enter domain.com in my browser, I am expecting it to load /var/www/html, my website. However, it loads my nextcloud (/var/www/nextcloud) instance. What do I do to make domain.com redirect to my website instead of my nextcloud instance?

Upvotes: 1

Views: 3258

Answers (2)

subterfuge
subterfuge

Reputation: 69

Apparently, Apache does not like cut and paste from a notepad++ I cleared all the whitespace from my config files, and then everything started working correctly.

probably something like a line space or something throwing it off. Maybe it's the encoding I have my notepad++ set to.

Upvotes: 0

Nic3500
Nic3500

Reputation: 8621

You should modify your configurations like this (explanation below):

/etc/apache2/site-available/000-default.conf

<VirtualHost _default_:80>
    ServerName domain.com
    ServerAlias www.domain.com

    DocumentRoot "/var/www/html"

    CustomLog /var/log/apache2/80_nc-access.log combined
    ErrorLog  /var/log/apache2/80_nc-error.log

    <Directory /var/www/html/>
        Options +FollowSymlinks
        AllowOverride All
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
        LimitRequestBody 0
    </Directory>
</VirtualHost>

/etc/apache2/site-available/nextcloud.conf

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerName nextcloud.domain.com

        DocumentRoot "/var/www/nextcloud"

        CustomLog /var/log/apache2/443_nc-access.log combined   
        ErrorLog  /var/log/apache2/443_nc-error.log

        SSLEngine on
        SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

        <Directory /var/www/nextcloud/>
            Options +FollowSymlinks
            AllowOverride All
            <IfModule mod_dav.c>
                Dav off
            </IfModule>
            LimitRequestBody 0
            SSLRenegBufferSize 10486000
        </Directory>

    </VirtualHost>
</IfModule>

Changes

  • In your :80 VirtualHost, you should not have any SSL directives. Port 80 is for http:// connections. So I remove everything that had to do with SSL.
  • In your :443 VirtualHost, there you want to have your SSL directives. So keep those.
  • <Directory> directives should be included in the <VirtualHost> sections. These only make sense in the context of your <VirtualHost> anyway. There should be a generic <Directory> in httpd.conf that blocks direct access to all directories anyway.
  • I suggest you use separate log files for each <VirtualHost>. It is a good habit to have, trust me I have had Apache server with 20 hosts, it is impossible to figure out which log entry is for which. Also you might want to put your SSL in Debug log to troubleshoot something...
  • To reach the first <VirtualHost>, enter http://example.com
  • To reach the second <VirtualHost>, enter https://example.com

There is no redirection here. Just <Virtualhost> names and ports assignment. Apache looks at what you asked for (the site in the address bar of your browser) and uses the <VirtualHost> that best matches your request. If it cannot find a specific one, it will use the first one by default.

That explains why if you try https://example.com, Apache will select the <VirtualHost> based on the port. Here it is port 443 (https://). So Apache will respond with your cloud <VirtualHost>. The port is the deciding factor here, not the domain name.

Upvotes: 2

Related Questions