前言
工作中使用过各种代理软件,有基于4层代理的,有基于7层代理的,现在大多的企业都上云了,云上有成熟的产品供我们使用,如阿里云SLB、华为云的ELB,腾讯云的CLB等都可以实现4层、7层的代理,由于云厂商成本的考虑,我们没法在云上使用4层代理软件,因此我们使用7层代理的产品比较多。软件比较多,配置繁杂,在这里做个总结备忘。有些步骤忘记了或者命令太长了,参考了不少文章和视频,感谢你们。
机器环境:
一、httpd
由于生产中通常使用nginx,以下步骤仅供参考:
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器软件,可以在大多数电脑操作系统中运行。由于其跨平台和安全性[注 1],被广泛使用,是最流行的Web服务器软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。----《维基百科》
1.安装
1.1 源码编译安装
参考1: https://blog.csdn.net/carefree2005/article/details/122652021
参考2: https://blog.csdn.net/leshami/article/details/50144179
# 创建必要的目录
mkdir -p /apps/httpd
# 下载必要的软件
wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz
wget https://archive.apache.org/dist/apr/apr-1.7.4.tar.gz
wget http://archive.apache.org/dist/apr/apr-util-1.1.2.tar.gz
# 编译安装
# 安装apr:
tar xf apr-1.7.4.tar.gz
cd apr-1.7.4
./configure --prefix=/apps/apr
make
make install
# 说明:编译时报错提示 rm: cannot remove `libtoolT': No such file or directory
# 解决:直接打开 configure,把 "cfgfile" 那行删除掉,重新再运行 ./configure 就可以了。
# 安装apr-util
tar xf apr-util-1.1.2.tar.gz
cd apr-util-1.1.2
./configure --prefix=/apps/apr-util --with-apr=/apps/apr
make
make install
# 编译安装
tar xf httpd-2.4.58.tar.gz
# 以下为几个主要的配置项
--sysconfdir=/etc/httpd24 指定配置文件路径
--enable-so 启动模块动态装卸载
--enable-ssl 编译ssl模块
--enable-cgi 支持cgi机制(能够让静态web服务器能够解析动态请求的一个协议)
--enable-rewrite 支持url重写 --Author : Leshami
--with-zlib 支持数据包压缩 --Blog : http://blog.csdn.net/leshami
--with-pcre 支持正则表达式
--with-apr=/apps/httpd/apr 指明依赖的apr所在目录
--with-apr-util=/apps/httpd/apr-util 指明依赖的apr-util所在的目录
--enable-modules=most 启用的模块
--enable-mpms-shared=all 以共享方式编译的模块
--with-mpm=prefork 指明httpd的工作方式为prefork
cd httpd-2.4.58
./configure \
--with-apr=/apps/httpd/apr \
--with-apr-util=/apps/httpd/apr-util \
--prefix=/apps/httpd/apache \
--sysconfdir=/apps/httpd/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-mpm=prefork \
--enable-modules=most \
--enable-mpms-shared=all
make
make install
vim /etc/httpd/httpd.conf
# DocumentRoot "/usr/local/apache/htdocs" # 注释掉DocumentRoot关闭主服务配置段
Include /apps/httpd/httpd24/httpd-vhosts.conf # 启用虚拟主机功能
- 配置文件参考yum安装
- httpd相关操作
# 配置测试
/apps/httpd/apache/bin/apachectl configtest
# 启动/停止
/apps/httpd/apache/bin/apachectl -k start
/apps/httpd/apache/bin/apachectl -k stop
- systemd参考
[Unit]
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)
[Service]
Type=notify
Environment=LANG=C
ExecStart=/apps/httpd/apache/bin/apachectl -f /apps/httpd/httpd24/httpd.conf start
ExecReload=/apps/httpd/apache/bin/apachectl -f /apps/httpd/httpd24/httpd.conf -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1.2 yum安装
参考: https://blog.51cto.com/u_15294888/5107871
[root@centos8 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@centos8 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@centos8 ~]# yum clean all && yum makecache
[root@centos8 ~]# yum install httpd -y
- 配置负载均衡代理
在主配置文件/etc/httpd/conf
开头添加:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
配置文件示例(在最后添加):
ProxyRequests Off
<Proxy balancer://javacluster>
BalancerMember http://a:8080 loadfactor=7 connectiontimeout=5 timeout=5
BalancerMember http://b:8080 loadfactor=2 connectiontimeout=5 timeout=5
</Proxy>
ProxyPass / balancer://javacluster
# bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
ProxySet lbmethod=bytraffic
# 警告:以下这段配置仅用于调试,绝不要添加到生产环境中!!!
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from localhost
</Location>
- 检查配置文件并启动
[root@centos8 ~]# apachectl configtest
[root@centos8 ~]# systemctl start httpd && systemctl enable httpd