让我们说我在磁盘上有此文件夹结构:
/var/www/site1.domain.com
/var/www/site2.domain.com
/var/www/site3.domain.com
/var/www/site4.domain.com
/var/www/site5.anotherdomain.com
/var/www/site6.anotherdomain.com
/var/www/site7.anotherdomain.com
/var/www/site8.anotherdomain.com
这些都是以完全相同的方式配置的相似的纯html / css / js网站。
是否可以通过以下方式设置Apache:将每个域的传入请求定向到适当的目录,而无需配置数百个<VirtualHost..>
节点?我想我可以自动生成它们,但是我希望可以看一下请求域的文件夹类型的配置。
这可能吗?即使有可能,对这种安全性是否还有其他担忧?
如果不可能,是否还有其他网络技术(例如nginx)可以做到这一点?
答案 0 :(得分:1)
使用nginx超级简单:
location / {
root /var/www/$host;
}
再加上nginx的速度非常快。但是,如果您希望坚持使用Apache,则应该阅读以下内容:https://httpd.apache.org/docs/2.4/vhosts/mass.html
答案 1 :(得分:1)
是的,存在安全性和其他问题。每个站点最好有一个虚拟主机,并带有单独的日志记录和单独的SSL证书等。
如果它们是不同的站点,最好将它们分开,因为:
/var/www/site1.domain.com-backup
或/var/www/secret-logs
,就可以访问它们。最好自动生成它们。从简单的bash脚本(可以在envvar上接受一些参数)到更复杂的工具(如SaltStack / Ansible),您可以使用许多模板工具。
这里有一个简单的bash脚本可以做到,它应该适合您的需求:
#!/bin/bash
cat << EOF > /etc/nginx/sites-available/$SERVER_NAME.conf
server {
listen 80;
server_name $SERVER_NAME $ADDITIONAL_SERVER_NAMES;
access_log /var/log/nginx/$SERVER_NAME-access.log;
error_log /var/log/nginx/$SEVER_NAME-error.log;
location / {
root /var/www/$SERVER_NAME;
}
}
EOF
# Enable config on nginx
ln -s /etc/nginx/sites-available/$SERVER_NAME.conf /etc/nginx/sites-enabled
# Test configration and apply if no errors
nginx -t && service nginx reload
要执行脚本名称nginx-site-generator
,请授予执行权限,并:
SERVER_NAME=site1.com ADDITIONAL_SERVER_NAMES=www.site1.com ./nginx-site-generator
答案 2 :(得分:0)
因此,事实证明有一个apache模块:
sudo a2enmod vhost_alias
在此处了解有关该模块的更多信息:
http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html
这是对我有用的配置:
#Define SNAME 999
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domainname.com
ServerAlias *.domainname.com
VirtualDocumentRoot /var/www/domainname.com/%0
#RewriteEngine on
#This sets the variable to env:
#RewriteRule .* - [E=SNAME:%{SERVER_NAME}]
ErrorLog ${APACHE_LOG_DIR}/domainname.com.error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/domainname.com.access.log combined
</VirtualHost>