You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Marcel Roșca <ro...@gmail.com> on 2021/04/20 12:25:11 UTC

[users@httpd] How can I configure “apache2” correctly using multiple sites through VirtualHost?

I configured two sites using the "VirtualHost" method in apache2, but if
there is an error in the first configuration file when I call my site, it
enters the second site. my configuration files are:
1: audio-castle.live:
<VirtualHost *:80>
Protocols h2
Redirect / https://audio-castle.live
</VirtualHost>
<IfModule mod_ssl.c>
    <VirtualHost *:443>
Protocols h2
ServerName audio-castle.live
ServerAlias www.audio-castle.live
        ServerAdmin

        DocumentRoot /var/www/audiocastle/web

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on

Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile
/etc/letsencrypt/live/audio-castle.live/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/audio-castle.live/privkey.pem

<Directory /var/www/audiocastle/web>
AllowOverride All
</Directory>
    </VirtualHost>
</IfModule>
the second file is: gtdvm.com:
<VirtualHost *:80>
ServerName gtdvm.com
Redirect / https://gtdvm.com/
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName gtdvm.com
ServerAlias www.gtdvm.com
    ServerAdmin

    DocumentRoot /var/www/gtdvm.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLEngine on

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/gtdvm.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/gtdvm.com/privkey.pem

<Directory /var/www/gtdvm.com>
AllowOverride All
</Directory>
</VirtualHost>
</IfModule>
the problem is when an error occurs in one of these two files, when I
access for example audio-castle.live, I reach the site gtdvm.com, if the
error is in the audio-castle.live file. my question is: how to avoid this
redirect from one site to another when one of them encounters errors? . I
use Ubuntu 20.04, apache2.4.46.

Re: [users@httpd] How can I configure “apache2” correctly using multiple sites through VirtualHost?

Posted by Jonathon Koyle <li...@gmail.com>.
On Tue, Apr 20, 2021 at 6:25 AM Marcel Roșca <ro...@gmail.com> wrote:
>
> I configured two sites using the "VirtualHost" method in apache2, but if there is an error in the first configuration file when I call my site, it enters the second site.
...
> the problem is when an error occurs in one of these two files, when I access for example audio-castle.live, I reach the site gtdvm.com, if the error is in the audio-castle.live file. my question is: how to avoid this redirect from one site to another when one of them encounters errors? . I use Ubuntu 20.04, apache2.4.46.

This is expected behavior.  When you use name-based virtual hosts, the
first host processed becomes the "default", and will respond to
requests for Hosts that do not match any other definition.  One way to
address this might be to create a simple virtual host in the main
configuration, or that is included before any other virtual hosts,
that returns an error page

<VirtualHost *:80>
    Redirect 404 /
    ErrorDocument 404 "Page Not Found"
</VirtualHost>

Doing this should cause all requests that do not match a ServerName
directive to 404 instead of going to some other host.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] How can I configure “apache2” correctly using multiple sites through VirtualHost?

Posted by "@lbutlr" <kr...@kreme.com>.
20 Apr 2021, at 06:25, Marcel Roșca <ro...@gmail.com> wrote:
> I configured two sites using the "VirtualHost" method in apache2, but if there is an error in the first configuration file when I call my site, it enters the second site. my configuration files are:

This is very confusing. Using white space to set off the two files and you comments would be helpful. Bonus points for a clear indents and structure.

> 1: audio-castle.live:
> <VirtualHost *:80>
>   Protocols h2
>   Redirect / https://audio-castle.live
> </VirtualHost>

> <IfModule mod_ssl.c>
> <VirtualHost *:443>

For what it's worth, my configurations all list the web server IP in the 443 VirtualHost. I seem to recall having issues if I used *. YMMV.

>   Protocols h2
>   ServerName audio-castle.live
>   ServerAlias www.audio-castle.live
>   ServerAdmin
>   DocumentRoot /var/www/audiocastle/web

Document root requires a final / (I think this was a bad decision in the parsing of the config file and causes far more trouble than allowing document roors without a final / could ever solve, but I did not get a vote).

>   ErrorLog ${APACHE_LOG_DIR}/error.log

Where is APACHE_LOG_DIR defined? If it's defined outside this file, pretty sure that doesn't work.

>   CustomLog ${APACHE_LOG_DIR}/access.log combined
>   SSLEngine on
>   Include /etc/letsencrypt/options-ssl-apache.conf
>   SSLCertificateFile /etc/letsencrypt/live/audio-castle.live/fullchain.pem
>   SSLCertificateKeyFile /etc/letsencrypt/live/audio-castle.live/privkey.pem
>   <Directory /var/www/audiocastle/web>
>     AllowOverride All
>   </Directory>
> </VirtualHost>
> </IfModule>

This virtualhost only exists inside your ifmodule, so if it is not loading the virtaulost, it is probably because of this. I have no idea why you have an if module at all, since your virtual host is defined as listening to port 443. What are you expecting that if module to do? If it is loading, I would expect alt you would get a 404 error on "https://audio-castle.liveindex.html".

> the second file is: gtdvm.com:

And when you say it is the second file, are you sure that it is SECOND as far as apache is concerned? `apachectl -S` will list your virtual configurations in the order apache loads them.

> the problem is when an error occurs in one of these two files, when I access for example audio-castle.live, I reach the site gtdvm.com, if the error is in the audio-castle.live file. my question is: how to avoid this redirect from one site to another when one of them encounters errors? . I use Ubuntu 20.04, apache2.4.46.

The way to avoid errors is to check your configuration for errors with `apachectl -t`, `apachectl -S`  and maybe `apachectl -X` if you are still having troubles.

I suspect that what you think is your "second file" is the only one being loaded.


-- 
"We take off our Republican hats and put on our American hats" --
	Many Republicans in Sep 2008


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org