linuxa
Goto Top

Weiterleitungen in VirtualHosts

Guten Abend Administrator.de Community,

sagen wir es mal so, ich habe mehrere Sub-Domains und so wie es derzeit konfiguriert ist, funktioniert es auch. Nur denke ich mir, dass man es auch "einfacher" und "sauberer" machen könnte.

Folgende Sub-Domains verweisen auf den Server (fiktive IP 123.456.789.012): eins.foobar.de, zwei.foobar.de org.foobar.de

Ich möchte, dass org.foobar.de auf sein DocumentRoot zugreift und eins.foobar.de sowie zwei.foobar.de zu Google weitergeleitet wird. Meine Configuration-Files sehen derweil wie folgt aus.

/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>

        ServerName meinserver.vomprovider.com
        ServerSignature Off

        RewriteEngine On
        Redirect / https://google.com

        LogLevel warn
        ErrorLog /opt/logs/apache2/root/error.log
        LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
        CustomLog /opt/logs/apache2/root/access.log combined

</VirtualHost>

/etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost org.foobar.de:443>
                ServerAdmin webmaster@localhost
                ServerName org.foobar.de

                ServerSignature Off

                RewriteEngine On

                DocumentRoot /opt/applications/cloud

                <Directory "/opt/applications/cloud">  
                        Options -Indexes
                        AllowOverride All
                        Require all granted
                </Directory>

                Header always set Strict-Transport-Security "max-age=15552000; includeSubdomains;"  

                LogLevel warn
                ErrorLog /opt/logs/apache2/cloud/error.log
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/cloud/access.log combined

                SSLEngine on

                SSLCertificateFile      /etc/letsencrypt/live/org.foobar.de/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/org.foobar.de/privkey.pem

                <FilesMatch "\.(cgi|shtml|phtml|php)$">  
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>
</VirtualHost>

<VirtualHost eins.foobar.de:443>
                ServerName eins.foobar.de
                ServerSignature Off

                RewriteEngine On
                Redirect / https://google.com

                LogLevel warn
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/misrouted/eins.foobar.access.log combined
</VirtualHost>

<VirtualHost zwei.foobar.de:443>
                ServerName zwei.foobar.de
                ServerSignature Off

                RewriteEngine On
                Redirect / https://google.com

                LogLevel warn
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/misrouted/zwei.foobar.access.log combined
</VirtualHost>

<VirtualHost 123.456.789.012:443>
                ServerName 123.456.789.012
                ServerSignature Off

                RewriteEngine On
                Redirect / https://google.com

                LogLevel warn
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/misrouted/ip-address.foobar.access.log combined
</VirtualHost>

</IfModule>

Es ist auch soweit, dass es funktioniert. Wenn ich http://eins.foobar.de aufrufe, schmeißt er erst einen SSL Error, weil das Zertifikat nicht matched, wenn man hier aber nun weiter geht, leitet er weiter an https://google.com (ebenso funktioniert es mit http://zwei.foobar.de). Wenn https://org.foobar.de aufgerufen wird, wird auch die richtige Application (in meinem Fall eine Cloud) aufgerufen.

Wenn ich den Apache über die IP direkt ansteuer, wird ebenfalls zu Google weitergeleitet.

Meine Frage an die Community ist nun, kann man eine Weiterleitung für alle anfragen, welche nicht https://org.foobar.de ist, in den VirtualHosts einstellen? Wenn ich nur <VirtualHost org.foobar.com:443> und den Rest (die Weiterleitung zu Google) über <VirtualHost *:443> laufen lasse, funktioniert es nicht und es wird alles auf den vHost von org.foobar.com geleitet.

Ich hoffe es ist soweit verständlich und bis dahin erst einmal vielen Dank!
Toorms

Content-Key: 428534

Url: https://administrator.de/contentid/428534

Printed on: April 23, 2024 at 08:04 o'clock

Member: StefanKittel
StefanKittel Mar 15, 2019 at 06:15:22 (UTC)
Goto Top
Moin,

das hängt ein bisschen von Deiner Apache-Version ab.
Bei alten Versionen war das noch anders gelöst.

The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first <VirtualHost>.
https://httpd.apache.org/docs/2.4/vhosts/examples.html
Member: Linuxa
Linuxa Mar 15, 2019 updated at 15:54:26 (UTC)
Goto Top
Moin,

habe Version 2.4.25

Wie ich das hier sehe, koennte ich aber nicht einfach es wie folgt schreiben oder?

<VirtualHost 192.168.1.1 172.20.30.40>
    DocumentRoot "/www/server1"  
    ServerName server.example.com
    ServerAlias server
</VirtualHost>

meine Variante wuerde dann so aussehen:

<VirtualHost eins.foobar.de zwei.foobar.de>
    ServerName eins.foobar.de zwei.foobar.de
    ServerSignature Off

    RewriteEngine On
    Redirect / https://google.com
</VirtualHost>

geht da so?

Update: Ich habe es so mal ausprobiert. Es geht nicht - jemand anderes einen Vorschlag? Besten Dank.
Member: timene0
timene0 Mar 18, 2019 updated at 05:24:45 (UTC)
Goto Top
Moin Toorms,

nimm mal die Zeile ServerName raus, also:

<VirtualHost *:80>
    ServerSignature Off

    RewriteEngine On
    Redirect / https://google.com
</VirtualHost>

und/oder

<VirtualHost *:443>
    ServerSignature Off

    RewriteEngine On
    Redirect / https://google.com
</VirtualHost>

Das sollte dann alle nicht an andere Virtual Hosts gebundenen Anfragen an google weiterleiten.

Ist von mir nicht getestet, sollte aber so funktionieren.
Member: Linuxa
Linuxa Mar 18, 2019 updated at 17:00:08 (UTC)
Goto Top
Hi timene0,

danke erstmal fuer deine Antwort. Das hat so leider nicht funktioniert. Meine default-ssl.conf sieht nun so aus:

<IfModule mod_ssl.c>
<VirtualHost cloud.foobar.com:443>
                ServerAdmin webmaster@localhost
                ServerName cloud.foobar.com

                ServerSignature Off

                RewriteEngine On

                DocumentRoot /opt/applications/cloud

                <Directory "/opt/applications/cloud">  
                        Options -Indexes
                        AllowOverride All
                        Require all granted
                </Directory>

                Header always set Strict-Transport-Security "max-age=15552000; includeSubdomains;"  

                LogLevel error
                ErrorLog /opt/logs/apache2/cloud/error.log
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/cloud/access.log combined

                SSLEngine on

                SSLCertificateFile      /etc/letsencrypt/live/cloud.foobar.com/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/cloud.foobar.com/privkey.pem

                <FilesMatch "\.(cgi|shtml|phtml|php)$">  
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>
</VirtualHost>

<VirtualHost *:443>
                ServerSignature Off

                RewriteEngine On
                Redirect / https://google.com

                LogLevel debug
                LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
                CustomLog /opt/logs/apache2/misrouted/all-missrouted.access.log combined
</VirtualHost>
</IfModule>

und die von meiner 000-default.conf sieht so aus:

<VirtualHost *:80>

        ServerName server123123.meinprovider.com
        ServerSignature Off

        RewriteEngine On
        Redirect / https://google.com

        LogLevel warn
        ErrorLog /opt/logs/apache2/root/error.log
        LogFormat "%h %l %u %t \"%r\" %>s %b" combined  
        CustomLog /opt/logs/apache2/root/access.log combined

</VirtualHost>

Der ganze Apache2 verhaelt sich nun so, das wenn ich http://abc.foobar.com (OHNE SSL) funktioniert die Weiterleitung problemlos. Wenn ich nun aber https://abc.foobar.com (MIT SSL) greift die Weiterleitung nicht und es erscheint die Seite der Cloud Software, welche eigentlich unter https://cloud.foobar.com zu finden ist. Die Cloud-Software sagt mir dann, dass es eine nicht autorisierte Domain ist, mit der ich zugreifen moechte und es deswegen nicht weiter komme. Schoen und gut, ich moechte aber, dass mich der Apache gar nicht erst dahin leitet.

Bis dahin
Toorms