我想使用 Apache2 进行托管,我需要知道如何让两个不同的域指向相同的静态 IP 但指向不同的网站,例如我可以拥有 bill.com 和 bob.com。它们都托管在同一台服务器上,使用相同的端口,但属于不同的网站,我该怎么做呢?
编辑:添加站点特定etc/apache2/sites-available/
文件:
测试.conf:
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.turingwebs.tk
ServerAlias turingwebs.tk
ServerAdmin [email protected]
DocumentRoot /var/www/test/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
vim: syntax=apache ts=4 sw=4 sts=4 sr noet
现在我实际使用的是:
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName turingweb.co.uk
ServerAdmin [email protected]
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
vim: syntax=apache ts=4 sw=4 sts=4 sr noet
答案1
简单的方法:
首先,将您的000-default.conf
文件复制到两个新的 .conf 文件中:
sudo cp /etc/apache2/sites-available/000-default.conf site1.conf
sudo cp /etc/apache2/sites-available/000-default.conf site2.conf
编辑每个文件,根据哪个 URL 应该重定向到哪个目录来更改ServerName
、ServerAlias
和指令。DocumentRoot
现在,只需启用网站并重新加载它们:
sudo a2ensite site1 site2
sudo service apache2 restart
想要证明它有效吗?这正是我为我的疯狂网站集群所做的。你可以使用以下命令进行确认:
dig +short www.techyteen.tk
dig +short www.brownvpn.tk
dig +short www.browntech.tk
dig +short www.comic-cult.tk
dig +short www.nelida.tk
从 的输出可以看出,所有域名都指向同一个 IP dig
。但是,在浏览器中访问这些页面,你会看到非常不同的东西。(不过,不要批评最后一个。这个家伙真的想用它给他的女朋友留下深刻印象 :P)