简体   繁体   中英

Apache2 virtual host configuration: Wrong pages showing

I have 4 domains: foo.eu, foo.net, bar.eu, bar.de. And I have one virtual server, running Ubuntu 12.04 LTS and Apache2.

I want to host 3 different webpages mapped like this:

  • Page 1: foo.eu and foo.net
  • Page 2: bar.eu and bar.de
  • Page 3: sub.foo.eu

The problem is when I access "www.foo.eu", "sub.foo.eu" or "www.bar.eu" I am displayed the correct page, but if I access "foo.eu" of "bar.eu" I see the page of "sub.foo.eu".

Why isn't "foo.eu"/"bar.eu" showing me the same content as "www.foo.eu"/"www.bar.eu"?

I have the following files in my /etc/apache2/sites-available/:

foo.eu:

<VirtualHost *:80>
DocumentRoot /var/www/foo

ServerName foo.eu
ServerName www.foo.eu

ServerAdmin webmaster@foo.eu

<Directory /var/wwww/foo >
    Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/foo_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/foo_access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

foo.net is simliar, just the SeverName lines are adjusted.

sub.foo.eu:

<VirtualHost *:80>

ServerName sub.foo.eu
ServerAlias sub.foo.eu

RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

DocumentRoot /var/www/sub/

ErrorLog ${APACHE_LOG_DIR}/sub_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/sub_access.log combined

<Directory /var/www/owncloud>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        order allow,deny
        allow from all
</Directory>
</VirtualHost>

<VirtualHost *:443>

ServerName sub.steiler.eu
ServerAlias sub.steiler.eu

SSLEngine on
SSLCertificateFile /etc/ssl/crt/sub-cert.pem
SSLCACertificateFile /etc/ssl/crt/caCert.pem
SSLCertificateKeyFile /etc/ssl/key/sub-key.pem

DocumentRoot /var/www/sub/

ErrorLog ${APACHE_LOG_DIR}/sub_ssl_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/sub_ssl_access.log combined

<Directory /var/www/sub>
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    order allow,deny
    allow from all
</Directory>

</VirtualHost>

bar.eu:

<VirtualHost *:80>
DocumentRoot /var/www/bar

ServerName bar.de
ServerName www.bar.de

ServerAdmin webmaster@bar.de

<Directory /var/wwww/bar >
    Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/bar_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/bar_access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

bar.de is similar, just the SeverName lines are adjusted.

UPDATE : Here are the DNS queries to the domains:

One of the domains (foo.eu; foo.net is similar):

~dig foo.eu A

; <<>> DiG 9.9.3-rl.156.01-P1-RedHat-9.9.3-3.P1.fc19 <<>> steiler.eu A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2157
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
foo.eu.                    IN      A

;; ANSWER SECTION:
foo.eu.                 86306   IN      A       192.168.178.1

;; AUTHORITY SECTION:
steiler.eu.             86306   IN      NS      ns-de.1and1-dns.biz.
steiler.eu.             86306   IN      NS      ns-de.1and1-dns.de.
steiler.eu.             86306   IN      NS      ns-de.1and1-dns.com.
steiler.eu.             86306   IN      NS      ns-de.1and1-dns.org.

;; Query time: 3 msec
;; SERVER: 15.8.243.101#53(15.8.243.101)
;; WHEN: Thu Jun 26 09:05:52 PDT 2014
;; MSG SIZE  rcvd: 186

The subdomain (sub.foo.eu):

~ dig sub.foo.eu A

; <<>> DiG 9.9.3-rl.156.01-P1-RedHat-9.9.3-3.P1.fc19 <<>> owncloud.steiler.eu
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7371
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 7

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;owncloud.steiler.eu.           IN      A

;; ANSWER SECTION:
sub.foo.eu.             86400   IN      A       192.168.178.1

;; AUTHORITY SECTION:
foo.eu.                 86004   IN      NS      ns-de.1and1-dns.biz.
foo.eu.                 86004   IN      NS      ns-de.1and1-dns.org.
foo.eu.                 86004   IN      NS      ns-de.1and1-dns.com.
foo.eu.                 86004   IN      NS      ns-de.1and1-dns.de.

;; ADDITIONAL SECTION:
ns-de.1and1-dns.de.     3204    IN      A       217.160.80.1
ns-de.1and1-dns.de.     3600    IN      AAAA    2001:8d8:fe:53:0:d9a0:5001:100
ns-de.1and1-dns.biz.    3204    IN      A       217.160.81.1
ns-de.1and1-dns.biz.    3600    IN      AAAA    2001:8d8:fe:53:0:d9a0:5101:100
ns-de.1and1-dns.com.    3204    IN      A       217.160.82.1
ns-de.1and1-dns.org.    3204    IN      A       217.160.83.1

;; Query time: 101 msec
;; SERVER: 15.8.243.101#53(15.8.243.101)
;; WHEN: Thu Jun 26 09:10:54 PDT 2014
;; MSG SIZE  rcvd: 315

The other domain (bar.de; bar.eu is similiar):

~dig bar.de A

; <<>> DiG 9.9.3-rl.156.01-P1-RedHat-9.9.3-3.P1.fc19 <<>> steilerdev.de A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13963
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 9

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;bar.de.                 IN      A

;; ANSWER SECTION:
bar.de.                 86393   IN      A       192.168.178.1

;; AUTHORITY SECTION:
bar.de.                 86393   IN      NS      ns-de.1and1-dns.de.
bar.de.                 86393   IN      NS      ns-de.1and1-dns.org.
bar.de.                 86393   IN      NS      ns-de.1and1-dns.biz.
bar.de.                 86393   IN      NS      ns-de.1and1-dns.com.

;; ADDITIONAL SECTION:
ns-de.1and1-dns.de.     2943    IN      A       217.160.80.1
ns-de.1and1-dns.de.     3339    IN      AAAA    2001:8d8:fe:53:0:d9a0:5001:100
ns-de.1and1-dns.biz.    2943    IN      A       217.160.81.1
ns-de.1and1-dns.biz.    3339    IN      AAAA    2001:8d8:fe:53:0:d9a0:5101:100
ns-de.1and1-dns.com.    2943    IN      A       217.160.82.1
ns-de.1and1-dns.com.    3339    IN      AAAA    2001:8d8:fe:53:0:d9a0:5201:100
ns-de.1and1-dns.org.    2943    IN      A       217.160.83.1
ns-de.1and1-dns.org.    3339    IN      AAAA    2001:8d8:fe:53:0:d9a0:5301:100

;; Query time: 2 msec
;; SERVER: 15.8.243.101#53(15.8.243.101)
;; WHEN: Thu Jun 26 09:15:15 PDT 2014
;; MSG SIZE  rcvd: 363

PS: I changed my IP.

PPS: The queries for www.foo.eu and www.bar.de are equal to the ones above.

While I am by no means a DNS expert, I would suggest a few things.

  • If your foo.eu and sub.foo.eu are both pointing to the same IP address, simply erase your sub.foo.eu DNS information. Get rid of it entirely. Let Apache handle these requests. Pretty certain you can also use Apache to handle requests outside of your server too. Long story short... Don't use DNS records to set a sub domain.

  • If that breaks something take a look at CNAMEs for subdomains. You should be able to redirect foo.eu to www.foo.eu, and the same applies to var.eu.

  • You are not utilizing ServerAlias in most of your records. You are instead using two ServerNames. The second would override. This is probably the focal point of your issue. If Apache doesn't set ServerAlias, then it will be forced to guess. If this is reflective of your configuration then it would do a reverse DNS lookup. You can see Apache documentation on ServerAlias for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM