Apache default VirtualHost

[亡魂溺海] 提交于 2019-12-17 16:27:03

问题


how can I set a default VirtualHost in apache? Preferably, I want the default host not to be the same as the ip address host. Now I have something like this:

NameVirtualHost *

<VirtualHost *>
ServerAdmin admin@domain.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@domain.com
DocumentRoot /someroot/
ServerAlias domain.com *.domain.com
</VirtualHost *>

If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the ip-address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?

edit: corrected my code a bit, so it looks more like the actual vhost.conf file


回答1:


I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias at the top of the blocks:

NameVirtualHost *

<VirtualHost *>
DocumentRoot /defaultdir/
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@domain.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@domain.com
DocumentRoot /someroot/
ServerAlias domain.com *.domain.com
</VirtualHost>



回答2:


If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in httpd.conf or apache.conf (or what ever is your main configuration file).

# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.site.com.conf

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf



回答3:


Obligatory - none of the above worked for me. I inherited strange combination of ip based virtual hosts and * vhosts (not assigned/catch all ips) based virtual hosts in this Apache config messed by ISPConfig.

I wanted Apache to serve not configured hosts with same page. What I had was - not configured hosts went to first vhost after 000-default.conf. No matter I had *:80 catch all defined as first vhost, instead of default Apache would load first defined site:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
</VirtualHost>

Although it's not completely valid config, what finally worked was adding ip based virtualhost without ServerName/ServerAlias defined:

<VirtualHost 192.168.10.10:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost 192.168.10.10:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        SSLEngine On
        ...
</VirtualHost>

$ apachectl -S outputs ip based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80        is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)

...

46.23.86.103:443       is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)

...

*:80                   is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)

Word of notice - in config like this, * vhosts won't work, so you need to apply ip's to all vhosts.




回答4:


An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.

Example:

NameVirtualHost *:80
Listen 80

...

<VirtualHost *:80>
        ServerName host1
        DocumentRoot /someDir
</VirtualHost>

<VirtualHost *:80>
        ServerName host2
        DocumentRoot /someOtherDir
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot /defaultDir
</VirtualHost>



回答5:


The other answers here didn't work for me, but I found a pretty simple solution that did work.

I made the default one the last one listed, and I gave it ServerAlias *.

For example:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.secondwebsite.com
    ServerAlias secondwebsite.com *.secondwebsite.com
    DocumentRoot /home/secondwebsite/web
</VirtualHost>

<VirtualHost *:80>
    ServerName www.defaultwebsite.com
    ServerAlias *
    DocumentRoot /home/defaultwebsite/web
</VirtualHost>

If the visitor didn't explicitly choose to go to something ending in secondwebsite.com, they get the default website.




回答6:


I will say what worked for me, the others answers above didn't help to my case at all. I hope it can help someone.

Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 ec2 instance to serve many sites (domains).

Considering that you already have Virtual Host installed and working. In my folder /etc/httpd/sites-available, I have some files with domain names (suffix .conf), for example: domain.com.conf. Create a new file like that.

sudo nano /etc/httpd/sites-available/domain.com.conf

<VirtualHost *:80>
    ServerName www.domain.com
    ServerAlias domain.com
    DocumentRoot /var/www/html/domain
</VirtualHost>

For each file.conf in sites-available, I create a symbolic link:

sudo ln -s /etc/httpd/sites-available/domain.com.conf /etc/httpd/sites-enabled/domain.com.conf

This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (.conf) in sites-available folder, sorted by filename.

To have a default DocumentRoot folder when access by IP, you have to create a file named 0a.conf, then apache will serve this site because this new file will be the first in sites-available folder.

You must create a symbolic link:

sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf

To check serving order, use it:

sudo apachectl -S

Now, restart apache, and check out it.

Be happy =)




回答7:


I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.

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

Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost




回答8:


NameVirtualHost option would be a good option.



来源:https://stackoverflow.com/questions/5427379/apache-default-virtualhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!