一.用APXS编译Apache模块,配置反向代理
./configure --prefix=/usr/local/apache2 --enable-so
make && make install
/usr/local/apache2/bin/httpd -l 查看是否有mod_so.c
配置apache反向代理,需要用到apache的mod_rewrite和mod_proxy模块组,使用apxs来安装新的模块,在安装mod_proxy模块的时候,需要将proxy_util.c加入进来一起编译;同样,如果要支持mod_proxy_ajp 需要把ajp的相关c文件包含进来,具体命令:
查找模块路径
find / -name mod_rewrite.c
/root/src/httpd-2.2.17/modules/mappers/mod_rewrite.c
find / -name mod_proxy.c
/root/src/httpd-2.2.17/modules/proxy/mod_proxy.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy.c modules/proxy/proxy_util.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy_ajp.c modules/proxy/ajp*.c
/usr/local/apache2/bin/apxs -i -a -c modules/mappers/mod_rewrite.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy_http.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy_ftp.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy_balancer.c
/usr/local/apache2/bin/apxs -i -a -c modules/proxy/mod_proxy_connect.c
在/usr/local/apache2/conf/httpd.conf添加了如下内容
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
正向代理
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Deny from all
Allow from 192.168.1 //允许1网段的地址从代理出去
</Proxy>
反向代理
<VirtualHost *:80>
ServerName 192.168.1.180
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /test http://www.163.com/
ProxyPassReverse /test http://www.163.com/
</VirtualHost>
重新启动apache
访问 http://192.168.1.180/Test/ 时,会重新反向代理访至 http://www.163.com
<VirtualHost *:80>
ServerName www.wellpan.cn
DocumentRoot /home/wellpan/html
<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /hhsClient/ http://192.168.1.174/hhsClient/
ProxyPassReverse /hhsClient/ http://192.168.1.174/hhsClient/
</IfModule>
</VirtualHost>
/home/wellpan/html/index.php
<?php
header("Location: /hhsClient/");
?>
外网访问www.wellpan.cn 时 自动反向代理至http://192.168.1.174/hhsClient/
二.源码编译安装
./configure --prefix=/usr/local/apache2 --enable-so --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-scgi --enable-proxy-ajp --enable-proxy-balancer
/usr/local/apache2/bin/httpd –l 查看
mod_proxy.c
mod_proxy_connect.c
mod_proxy_ftp.c
mod_proxy_http.c
mod_proxy_scgi.c
mod_proxy_ajp.c
mod_proxy_balancer.c
添加如下
<VirtualHost *:80>
ServerName 192.168.15.124
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /hhsClient/ http://192.168.1.174/hhsClient/
ProxyPassReverse /hhsClient/ http://192.168.1.174/hhsClient/
</VirtualHost>
访问http://192.168.15.124/hhsClient/ 自动跳转至http://192.168.1.174/hhsClient/