apache反向代理
程序员文章站
2022-04-29 17:23:38
...
一般apache用来做web服务比较多,很少听说可以做反向代理,最近听到,抱着尝试的心态就试了试,配置不是很复杂
环境:
系统Centos7
[aaa@qq.com extra]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Ip地址 | 角色 |
192.168.253.137 | 反向代理服务器 |
192.168.253.132 | 后端webA |
192.168.253.142 | 后端webB |
0.三台机器都关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
1.先把WebA和WebB装上web服务
WebA和WebB:
yum install httpd
WebA:
echo "i am A" > /var/www/html/index.html
WebB:
echo "i am B" > /var/www/html/index.html
WebA和WebB:
systemctl start httpd
浏览器测试一下是否成功
WebA成功
WebB成功
2.在反向代理服务器192.168.253.137 编译安装apache2.4版本
下载的地址http://httpd.apache.org/download.cgi#apache24 相关的apr,apr-util和apache源码包都可以官网下载
cd /usr/local/src
mkdir apache
cd apache/
wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.33.tar.gz ##这个http2.4的下载地址 根据情况而定
wget http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gz
wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
tar -zxf apr-1.6.3.tar.gz
tar -zxf apr-util-1.6.1.tar.gz
tar -zxf httpd-2.4.33.tar.gz
mv apr-1.6.3 httpd-2.4.33/srclib/apr
mv apr-util-1.6.1 httpd-2.4.33/srclib/apr-util
cd httpd-2.4.33
yum install gcc gcc-devel pcre pcre-devel expat expat-devel openssl openssl-devel zlib-devel -y
./configure --prefix=/usr/local/httpd --enable-so --enable-modules="all" --with-included-apr
make && make install
PATH=PATH="/usr/local/httpd/bin/:$PATH"
然后测试一下
查看一下目录结构
[aaa@qq.com httpd]# tree -L 1
.
├── bin #apachectl相关命令
├── build
├── cgi-bin
├── conf #apache的配置文件目录
├── error
├── htdocs
├── icons
├── include
├── lib
├── logs #apache默认存放日志的目录
├── man
├── manual
└── modules
主要的几个配置文件
conf
├── extra
│ ├── httpd-autoindex.conf
│ ├── httpd-dav.conf
│ ├── httpd-default.conf
│ ├── httpd-info.conf
│ ├── httpd-languages.conf
│ ├── httpd-manual.conf
│ ├── httpd-mpm.conf
│ ├── httpd-multilang-errordoc.conf
│ ├── httpd-ssl.conf
│ ├── httpd-userdir.conf
│ ├── httpd-vhosts.conf
│ └── proxy-html.conf
├── httpd.conf
├── magic
├── mime.types
└── original
3.编写proxy配置文件,吧配置文件写在/usr/local/httpd/conf/extra/httpd-proxy.conf
[aaa@qq.com extra]# cat httpd-proxy.conf
#proxy
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off
<Proxy "balancer://mycluster"> #后端web服务的成员组
BalancerMember "http://192.168.253.142:80"
BalancerMember "http://192.168.253.132:80"
</Proxy>
ProxyPass "/proxy_demo" "balancer://mycluster"
ProxyPassReverse "/proxy_demo" "balancer://mycluster"
<Location /admin> #配置反向代理的web管理界面
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
修改主配置文件
echo "Include conf/extra/httpd-proxy.conf" >> /usr/local/httpd/conf/httpd.conf
echo "ServerName 127.0.0.1:80" >> /usr/local/httpd/conf/httpd.conf
启动
apachectl restart
查看端口是否监听
表示成功
4.测试反向代理功能
192.168.253.137是反向代理服务器,可以连接到WebA和WebB表示成功
5.测试反向代理的web管理界面
如果要把反向代理配置到虚拟主机配置文件可以改为
#proxy demo
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off #是否开启apache正向代理功能,如果设置了ProxyPass必须将ProxyRequests设置为Off
<Proxy "balancer://mycluster">
BalancerMember "http://192.168.253.142:80"
BalancerMember "http://192.168.253.132:80"
</Proxy>
<VirtualHost 192.168.253.137:80>
ServerAdmin aaa@qq.com
DocumentRoot "/opt"
ErrorLog "logs/192.168.253.137_error_log"
CustomLog "logs/192.168.253.137_access_log" common
ProxyPass "/" "balancer://mycluster"
ProxyPassReverse "/" "balancer://mycluster"
</VirtualHost>