Apache单IP多域名部署与反向代理配置指南
前言
在网站发布实践中,往往难以保证每个站点都拥有独立的IP地址,尤其在IPv4环境下IP资源尤为稀缺。鉴于此,作为主流Web服务器的Apache也提供了相应的解决方案,以下将详细介绍具体实施步骤。
配置方法
本教程基于Apache 2.4.7版本,操作系统为Ubuntu 14.04.1 LTS。由于不同版本的配置文件路径可能存在差异,请注意环境匹配。
首先,进入apache2安装目录。

核心配置涉及sites-available和sites-enabled两个目录。顾名思义,前者存放可用的网站配置,后者存放已启用的配置。sites-enabled中的文件实际上是sites-available中文件的软链接。因此,我们主要修改sites-available目录下的文件。打开该目录可见默认存在000-default.conf文件,使用cat命令查看其内容。
# 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 seothert it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
# 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
这是默认的网址配置文件。需要修改的部分主要是被注释掉的ServerName(域名)和DocumentRoot(路径)。去除这两行的注释符号,保留有效配置即可。
ServerName #这里是域名地址
ServerAdmin webmaster@localhost
DocumentRoot /var/www/ #这里是路径
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
可以直接在现有配置下方添加新的ServerName和DocumentRoot设置,随后重启Apache服务生效。但需注意,上述直接添加的方式优先级较高,访问自定义域名时会跳转到新设置的路径,而直接访问IP地址仍会指向第一个设置的路径。若希望完全隔离,可以选择删除原有默认配置。
另一种推荐做法是新建一个配置文件,例如命名为xxx.conf,内容与前述类似。
ServerName #这里是域名地址
ServerAdmin webmaster@localhost
DocumentRoot /var/www/ #这里是路径
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
接着创建软链接 ln -s ../sites-avaiable/xxx.conf ../sites-enable/xxx.conf
这样同样可以实现多域名绑定,但此时000-default.conf的优先级依然高于新创建的xxx.conf。
操作过程相对简单,至此基本完成。
附:配置Apache2.4.7反向代理的方法
1. 设置httpd.conf
打开Apache24/conf文件夹下的httpd.conf配置文件,找到以下几行代码,删除前面的注释符号‘#'。
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_modulemodules/mod_proxy_connect.so
LoadModule proxy_ftp_modulemodules/mod_proxy_ftp.so
LoadModule proxy_http_modulemodules/mod_proxy_http.so
(注:许多人习惯注释LoadModuleproxy_balancer_modulemodules/mod_proxy_balancer.so,这是用于负载均衡的功能。若仅做反向代理则无需启用此项,且若取消注释但未进行相应设置,可能导致Apache服务无法启动)。
然后找到Include conf/extra/httpd-vhosts.conf
将该行前面的注释‘#'也删除,以引入该配置文件。
2. 设置httpd-vhosts.conf
打开Apache24/conf/extra文件夹下的httpd-vhosts.conf文件,找到相关配置区域。
#ServerName www.example.com:80
DocumentRoot "${SRVROOT}/htdocs"
在适当位置添加如下配置。
ProxyRequests Off
ProxyPass /***(你想要访问的地址) http://*******(想要代理的地址)
ProxyPassReverse /***(你想要访问的地址) http://*******(想要代理的地址)
例如,若希望在浏览器输入localhost时实际获取www.baidu.com的内容,可设置为ProxyPass /***(目标地址) http://*******(代理地址)。第二个指令ProxyPassReverse用于处理域名重定向,如果代理的目标地址发生跳转,配置此项可实现相应跳转;否则可能会报错。
若希望其他电脑通过外网地址访问自己的服务器,需设置httpd.conf中的部分。
将 Require all denied改为Require all granted,以允许所有请求和访问。
完成以上设置后即可正常使用。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对本站的支持。