0

I have 2 separate domains that I want to host 2 different servers on. I have both conf files set up, but the problem is; going to my second domain in my browser sends me to the first virtual host and all its files. I'm not sure what can cause this, because I'm new to most apache things, so I will put both conf files here.

Site 1:

NameVirtualHost *:80
Listen *:80
<VirtualHost *:80>
    ServerAdmin admin@site1.example
    ServerName site1.example
    ServerAlias www.site1.example
    DocumentRoot /var/www/site1.example/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Site 2:

LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
NameVirtualHost *:443
Listen 443
<VirtualHost *:443>
        RewriteEngine On
        RewriteCond %{HTTPS}  !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/%1 [R,L]
        ServerName site2.example
        ServerAlias *.site2.example
        ErrorLog ${APACHE_LOG_DIR}/site2/SSL_error.log
        TransferLog ${APACHE_LOG_DIR}/site2/SSL_access.log
        VirtualDocumentRoot "/var/www/site2/%1"

        SSLEngine on
        SSLProtocol all -SSLv2 -SSLv3
        SSLCipherSuite ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL

        SSLCertificateFile /etc/letsencrypt/live/site2.example/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/site2.example/privkey.pem
        <Files ~ "\.(cgi|shtml|phtml|php3?)$">
                   SSLOptions +StdEnvVars
        </Files>

        SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0~
        <Directory /var/www/site2.example>
                Options +Indexes +FollowSymLinks -MultiViews
                UseCanonicalName Off
                AllowOverride All
                Allow from all
                RewriteEngine On
                RewriteCond %{HTTP_HOST} ^(.*)\.site2\.example$ [NC]
                RewriteCond /var/www/site2.example/%1 !-d
                RewriteRule ^(.*) https://404.site2.example/
        </Directory>
        <Directory /var/www/site2.example/%1>
                Options +Indexes +FollowSymLinks -MultiViews
                UseCanonicalName Off
                AllowOverride All
                Allow from all
        </Directory>

        <Location />
                UseCanonicalName Off
                Order allow,deny
                Allow from all
        </Location>
</VirtualHost>

From this, what I mean is that Site 2 is sending me to Site 1, not as a redirect, but actually loads all of its files. Perhaps I did something wrong? When I set Site 2's port to 80, it tells me "Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please" even when using https://

  • Site 2 is for port 443, is it supposed to be HTTPS only? – Stephen Ostermiller Mar 21 at 1:15
  • Yes, it's supposed to be HTTPS only, it's also a .dev domain – Steven Webster Mar 21 at 1:16
  • It sounds like you might be trying to contact it using HTTP. Have you tried adding a redirect host for it on port 80? – Stephen Ostermiller Mar 21 at 1:48
  • I think you're right, as I got this error: "Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please." but I'm not sure how to fix it – Steven Webster Mar 21 at 2:18
  • I still don't think that's why it's bringing me to site 1 instead of erroring sometimes though. When I have it on port 80, it gives me the above error, when I have it on 443 it goes to site 1. I'm just at a loss of what the cause could be – Steven Webster Mar 21 at 2:34
0

I would recommend adding additional virtual hosts:

  • A default HTTP virtual host that shows an error for unrecognized host names
  • A default HTTP virtual host that shows an error for unrecognized host names
  • A virtual HTTP host for your .dev site that redirects to HTTPS.

Even with HSTS enabled for your .dev domain, you should still redirect any HTTP requests to HTTPS. There could be browsers, bots, or crawlers that don't support HSTS and still need the redirect. Google recommends redirecting even when HSTS is enabled.

Apache treats the first virtual host as the default. If a virtual host is listed first, it may get non-matching host name requests if those requests don't match any other virtual hosts. It sounds like your requests are coming in on port 80, Apache doesn't find a matching host name on port 80 and sends the request to the first virtual host for port 80. Installing a default virtual host that shows an error prevents duplicate content and may help you debug your configuration.

Place these virtual hosts before other virtual hosts in your config file. If each site is in its own file, name the file starting with zeros so that it comes first alphabetically. eg /etc/apache2/sites-available/0000-default.conf then enable the site with sudo a2ensite 0000-default. You may have to create the /var/www/default directory to get these to work. You may also need to create a self-signed certificate if there isn't one already available on your system for the default HTTPS.

<VirtualHost *:80>
    ServerName localhost.localdomain
    ErrorDocument 404 "Host name not recognized"
    DocumentRoot /var/www/default
</VirtualHost>

<VirtualHost *:443>
    ServerName localhost.localdomain
    ErrorDocument 404 "Host name not recognized"
    DocumentRoot /var/www/default

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL
    SSLCertificateFile /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

Then create a virtual host for site2 on port 80 that does nothing but redirect to HTTPS:

<VirtualHost *:80>
    ServerAdmin admin@site2.site
    ServerName site2.site
    ServerAlias www.site2.site
    Redirect permanent / https://site2.site/
</VirtualHost>

When you got the error "Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please." that probably means that you were trying to visit an HTTP version site on port 443 with a URL like http://site2.site:443/ when you should have been trying https://site2.site/ You may have caused this by typing site2.site:443 with no protocol into your RUL bar and your browser assumed HTTP. The solution is to never specify the :443 in the URL and always just add an s as in https:// instead.

  • Even with https:// in the url I got the error, and when I type http:// in my url, it automatically became https:// on its own with the rewrite engine that I used (the question wasn't updated yet). Also, my page has a 404 page that should redirect to the 404 when not found. I should add, that every subdomain I add will redirect to site 1. – Steven Webster Mar 21 at 13:38
  • Sorry for multiple comments, I enabled default site and now site 2 goes to the default site instead, so do you know what would cause this or how to fix it? – Steven Webster Mar 21 at 14:25
  • I think the traffic is coming in on port 80 still. Did you enable the port 80 site 2 redirect? Do you have a load balancer or a CDN that could be downgrading the requests? – Stephen Ostermiller Mar 21 at 14:29
  • I just enabled the redirect on port 80, but it says ERR_INVALID_REDIRECT, I'm trying to redirect them to the subdomain they also put in I added Redirect permanent / https://%1.site2.site is this invalid? – Steven Webster Mar 21 at 14:35
  • No. You would either need to create a separate virtual host for each hostname or use ServerAlias *.site2.site and use a rewrite condition and rule. RewriteCond "%{HTTP_HOST}" "(.*)" then RewriteRule "^/?(.*)" "https://%1/$1" – Stephen Ostermiller Mar 21 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.