gpt4 book ai didi

apache - 设置动态虚拟主机(Ubuntu 上的 Apache2)

转载 作者:行者123 更新时间:2023-12-04 00:09:24 26 4
gpt4 key购买 nike

我想设置一个虚拟主机,它可以根据用于访问它的主机名动态处理所有请求。如果 %{HTTP_HOST} 可以在 DocumentRoot 中使用,这可能正是我想要的:

<VirtualHost *:80>
ServerAdmin me@example.com

DocumentRoot /var/www/live/%{HTTP_HOST}/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/live/%{HTTP_HOST}/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>

...不幸的是,DocumentRoot 中不允许使用 %{HTTP_HOST}(警告:DocumentRoot [/var/www/live/%{HTTP_HOST}/public] 不存在)。我还能如何实现我的目标?

更新:我想过将一个包罗万象的虚拟主机指向一个目录,并让 .htaccess 使用 mod_rewrite 来动态选择路径,但(老实说)我已经筋疲力尽了。我会在早上再试一次,但与此同时,如果有人有好的想法,我很想听听他们的意见!谢谢!

最佳答案

也许您可以尝试本文中的以下解决方案:Apache: Dynamic Virtual Hosts

A few months back I looked for a solution to overcome the problem of creating individual Virtual Hosts in Apache every time I wanted to configure a new site on a development machine (something that is a big issue in work where we have a lot of websites). Apache is able to support this functionality relatively easy using a module and a few lines in the configuration file. I set this up on Fedora 14, so results may be slightly different for other OS's (different paths, configuration file setup, etc)

Open up the main Apache conf (/etc/httpd/conf/httpd.conf), and ensure the module mod_vhost_alias is enabled. There should be a line in the configuration like

LoadModule vhost_alias_module modules/mod_vhost_alias.so 

Next, add the following lines to the bottom of this file. You'll need to edit the file with sudo privileges.

NameVirtualHost *:80

UseCanonicalName Off

<VirtualHost *:80>
VirtualDocumentRoot /var/www/html/domains/%0
</VirtualHost>

This sets up a catch all for any domain coming in over port 80 (the default port for http traffic, if your using https you will need to use 443 - alternatively you could remove the port restriction). The important line here is the VirtualDocumentRoot. The tells Apache where your files will reside on disk. The %0 part takes the whole domain name and inserts it into the path. To illustrate this if we went to a domain testing.com.dev the VirtualDocumentRoot would be:

/var/www/html/domains/testing.com.dev 

This type of configuration might be suitable for most situations, however I didn't want to have the .dev part of the domain in my folders on disk. I was able to achieve this by setting the VirtualDocumentRoot to:

VirtualDocumentRoot /var/www/html/domains/%-2+ 

The above example of testing.com.dev would now point to:

/var/www/html/domains/testing.com 

Remember to add the domain to your hosts file (/etc/hosts)

For a full list of options see the mod_vhost_alias documentation. Additional documentation can be found here.

关于apache - 设置动态虚拟主机(Ubuntu 上的 Apache2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com