Learning Apache (b)

Reverse proxy load balancing of APACHE

A reverse proxy
1.1 Introduction repercussions agent

  Reverse proxy (Reverse Proxy) mode refers to the proxy server to accept connection requests on the internet, and then forwards the request to the server on the internal network, and returns the result obtained from the server to the client on request internet connection, At this point the external proxy server on the performance of a reverse proxy server.

1.2 reverse proxy works

  The proxy server is usually only used for proxy internal network connection requests to the Internet, the client must specify the proxy server, and would have to be sent directly to http requests on the Web server sends to the proxy server. Because the host on the external network will not configure and use this proxy server, common proxy server is also designed to search for more uncertainty on the Internet, instead of accessing a request for a fixed multiple clients on the Internet server, so ordinary Web proxy server does not support external access requests to the internal network. When a host on a proxy server to proxy external network to access the internal network, this proxy service is called reverse proxy services. At this point the external proxy server on the performance of a Web server, the external network can simply treat it as a standard Web server without the need for specific configuration. The difference is that the server does not save any page of the real data, all the static Web page or CGI programs are stored on an internal Web server. Therefore, attacks on the reverse proxy server and does not cause the destruction of pages of information, thus enhancing the security of the Web server.
  Reverse proxy mode and packet filtering proxy mode or normal mode does not conflict, two methods can be used simultaneously in the firewall device, wherein the reverse proxy for accessing the internal network using an external network, or forward the packet filtering agent It denies other external access mode and provides access to the internal network to external networks. Therefore, these methods can be combined to provide the best security access.

1.3 reverse proxy role


1.3.1 website security

Any request from the Internet must first go through a proxy server

 

Configuring Web caching feature acceleration request

可以缓存真实Web服务器上的某些静态资源,减轻真实Web服务器的负载压力

1.3.3 实现负载均衡

充当负载均衡服务器均衡地分发请求,平衡集群中各个服务器的负载压力

二、使用apache实现反向代理实战
2.1 环境准备:两台虚拟机

操作系统:centos7.X

1、备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# yum install -y gcc glibc gcc-c++ make screen tree lrzsz

 

node1:
以yum的方式安装apache修改http端口为8080

# yum install -y httpd
# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf

# echo 'node1' > /var/www/html/index.html 
[root@node2 ~]# curl http://192.168.3.200:8080/
node1

 

node2:
以yum的方式安装apache修改http端口为8080

# yum install -y httpd
# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf

# echo 'node2' > /var/www/html/index.html 
[root@node2 ~]# curl http://192.168.3.200:8080/
node2

在node1上再次编译安装apache2.4.25作为反向代理服务器

 
# cd /usr/local/src/ && wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.25.tar.gz

# tar zxf httpd-2.4.25.tar.gz
# cd httpd-2.4.25
# ./configure --prefix=/usr/local/httpd-2.4.25 --enable-so --enable-modules="all"
# make && make install
# ln -s /usr/local/httpd-2.4.25/ /usr/local/httpd
 

编辑linux-node1的apache作为反向代理的配置文件

 
# vim /usr/local/httpd/conf/extra/httpd-proxy.conf

LoadModule proxy_module modules/mod_proxy.so #proxy模块
LoadModule proxy_connect_module modules/mod_proxy_connect.so #链接的模块
LoadModule proxy_http_module modules/mod_proxy_http.so #给http做代理模块
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so #负载均衡模块
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so #算法模块,根据server的请求量
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so #算法模块,根据server流量
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so #算法模块,根据server繁忙程度
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so #
ProxyRequests Off #如果没有对服务器采取安全措施之前,请不要开启此项
<Proxy balancer://mycluster> #lb集群组的名称
BalancerMember http://192.168.3.140:8080 #集群组成员
BalancerMember http://192.168.3.200:8080 #集群组成员
</Proxy>
ProxyPass /demo balancer://mycluster #跳转,和lb集群组名称对应
ProxyPassReverse /demo balancer://mycluster
 

 

在apache的主配置文件include上述配置文件,并启动apache

# vim /usr/local/httpd/conf/httpd.conf

Include conf/extra/httpd-proxy.conf

 

# 写入测试文件

# echo 'node1' > /usr/local/httpd-2.4.25/htdocs/index.html

/usr/local/httpd/bin/apachectl -t # 测试apache语法是否正确
/usr/local/httpd/bin/apachectl -k start # 启动apache

 

通过访问代理服务器,可以看到负载均衡的效果

http://192.168.3.140:8080/demo


对apache增加一个管理,并重启

 
# cat /usr/local/httpd/conf/extra/httpd-proxy.conf

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 slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://192.168.3.140:8080
BalancerMember http://192.168.3.200:8080
</Proxy>
ProxyPass /demo balancer://mycluster
ProxyPassReverse /demo balancer://mycluster
<Location /manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
 

 

打开浏览器管理界面
http://192.168.3.140:8080/manager

增加虚拟主机

 
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 slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://192.168.3.140:8080
BalancerMember http://192.168.3.200:8080
</Proxy>
ProxyPass /demo balancer://mycluster
ProxyPassReverse /demo balancer://mycluster

<Location /manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/opt"
ServerName www.chinasoft.com
ServerAlias chinasoft.com
ErrorLog "logs/www.chinasoft.com-error_log"
CustomLog "logs/www.chinasoft.com-access_log" common
ProxyPass / balancer://mycluster
ProxyPassReverse / balancer://mycluster
</VirtualHost>
 

 

在本地电脑的host文件加入以下内容,过后要清理掉
192.168.3.140 www.chinasoft.com

http://www.chinasoft.com

反向代理负载均衡之APACHE

一、反向代理
1.1 介绍反响代理

  反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

1.2 反向代理的工作方式

  通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。由于外部网络上的主机并不会配置并使用这个代理服务器,普通代理服务器也被设计为在Internet上搜寻多个不确定的服务器,而不是针对Internet上多个客户机的请求访问某一个固定的服务器,因此普通的Web代理服务器不支持外部对内部网络的访问请求。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。此时代理服务器对外就表现为一个Web服务器,外部网络就可以简单把它当作一个标准的Web服务器而不需要特定的配置。不同之处在于,这个服务器没有保存任何网页的真实数据,所有的静态网页或者CGI程序,都保存在内部的Web服务器上。因此对反向代理服务器的攻击并不会使得网页信息遭到破坏,这样就增强了Web服务器的安全性。
  反向代理方式和包过滤方式或普通代理方式并无冲突,因此可以在防火墙设备中同时使用这两种方式,其中反向代理用于外部网络访问内部网络时使用,正向代理或包过滤方式用于拒绝其他外部访问方式并提供内部网络对外部网络的访问能力。因此可以结合这些方式提供最佳的安全访问方式。

1.3 反向代理的作用


1.3.1 保护网站安全

任何来自Internet的请求都必须先经过代理服务器

 

1.3.2 配置缓存功能加速Web请求

可以缓存真实Web服务器上的某些静态资源,减轻真实Web服务器的负载压力

1.3.3 实现负载均衡

充当负载均衡服务器均衡地分发请求,平衡集群中各个服务器的负载压力

二、使用apache实现反向代理实战
2.1 环境准备:两台虚拟机

操作系统:centos7.X

1、备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# yum install -y gcc glibc gcc-c++ make screen tree lrzsz

 

node1:
以yum的方式安装apache修改http端口为8080

# yum install -y httpd
# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf

# echo 'node1' > /var/www/html/index.html 
[root@node2 ~]# curl http://192.168.3.200:8080/
node1

 

node2:
以yum的方式安装apache修改http端口为8080

# yum install -y httpd
# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf

# echo 'node2' > /var/www/html/index.html 
[root@node2 ~]# curl http://192.168.3.200:8080/
node2

在node1上再次编译安装apache2.4.25作为反向代理服务器

 
# cd /usr/local/src/ && wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.25.tar.gz

# tar zxf httpd-2.4.25.tar.gz
# cd httpd-2.4.25
# ./configure --prefix=/usr/local/httpd-2.4.25 --enable-so --enable-modules="all"
# make && make install
# ln -s /usr/local/httpd-2.4.25/ /usr/local/httpd
 

编辑linux-node1的apache作为反向代理的配置文件

 
# vim /usr/local/httpd/conf/extra/httpd-proxy.conf

LoadModule proxy_module modules/mod_proxy.so #proxy模块
LoadModule proxy_connect_module modules/mod_proxy_connect.so #链接的模块
LoadModule proxy_http_module modules/mod_proxy_http.so #给http做代理模块
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so #负载均衡模块
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so #算法模块,根据server的请求量
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so #算法模块,根据server流量
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so #算法模块,根据server繁忙程度
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so #
ProxyRequests Off #如果没有对服务器采取安全措施之前,请不要开启此项
<Proxy balancer://mycluster> #lb集群组的名称
BalancerMember http://192.168.3.140:8080 #集群组成员
BalancerMember http://192.168.3.200:8080 #集群组成员
</Proxy>
ProxyPass /demo balancer://mycluster #跳转,和lb集群组名称对应
ProxyPassReverse /demo balancer://mycluster
 

 

在apache的主配置文件include上述配置文件,并启动apache

# vim /usr/local/httpd/conf/httpd.conf

Include conf/extra/httpd-proxy.conf

 

# 写入测试文件

# echo 'node1' > /usr/local/httpd-2.4.25/htdocs/index.html

/usr/local/httpd/bin/apachectl -t # 测试apache语法是否正确
/usr/local/httpd/bin/apachectl -k start # 启动apache

 

通过访问代理服务器,可以看到负载均衡的效果

http://192.168.3.140:8080/demo


对apache增加一个管理,并重启

 
# cat /usr/local/httpd/conf/extra/httpd-proxy.conf

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 slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://192.168.3.140:8080
BalancerMember http://192.168.3.200:8080
</Proxy>
ProxyPass /demo balancer://mycluster
ProxyPassReverse /demo balancer://mycluster
<Location /manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
 

 

打开浏览器管理界面
http://192.168.3.140:8080/manager

增加虚拟主机

 
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 slotmem_shm_module modules/mod_slotmem_shm.so
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://192.168.3.140:8080
BalancerMember http://192.168.3.200:8080
</Proxy>
ProxyPass /demo balancer://mycluster
ProxyPassReverse /demo balancer://mycluster

<Location /manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/opt"
ServerName www.chinasoft.com
ServerAlias chinasoft.com
ErrorLog "logs/www.chinasoft.com-error_log"
CustomLog "logs/www.chinasoft.com-access_log" common
ProxyPass / balancer://mycluster
ProxyPassReverse / balancer://mycluster
</VirtualHost>
 

 

在本地电脑的host文件加入以下内容,过后要清理掉
192.168.3.140 www.chinasoft.com

http://www.chinasoft.com

Guess you like

Origin www.cnblogs.com/wuhg/p/11447177.html