Httpd服务入门知识-正向代理和反向代理

         Httpd服务入门知识-正向代理和反向代理

                                                作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.正向代理和反向代理

启用反向代理
  ProxyPass "/" "http://www.example.com/"
  ProxyPassReverse "/" "http://www.example.com/"

特定URL反向代理   ProxyPass
"/images" "http://www.example.com/"   ProxyPassReverse "/images" http://www.example.com/
示例: <VirtualHost *>   ServerName www.yinzhengjie.org.cn   ProxyPass / http://localhost:8080/   ProxyPassReverse / http://localhost:8080/ </VirtualHost>

 

 

二.反向代理配置实战

1>.试验节点角色分配

node101.yinzhengjie.org.cn
  客户端

node105.yinzhengjie.org.cn:
  提供"/"路径的服务

node106.yinzhengjie.org.cn:
  提供"/blog"路径的服务

node107.yinzhengjie.org.cn:
  反向代理服务器

2>.node105.yinzhengjie.org.cn节点配置

[root@node105.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.jdcloud.com
 * extras: mirror.jdcloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                            | 3.6 kB  00:00:00     
extras                                                                                          | 2.9 kB  00:00:00     
updates                                                                                         | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                                     | 5.8 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                      Arch                    Version                              Repository             Size
=======================================================================================================================
Installing:
 httpd                        x86_64                  2.4.6-90.el7.centos                  base                  2.7 M
Installing for dependencies:
 apr                          x86_64                  1.4.8-5.el7                          base                  103 k
 apr-util                     x86_64                  1.5.2-6.el7                          base                   92 k
 httpd-tools                  x86_64                  2.4.6-90.el7.centos                  base                   91 k

Transaction Summary
=======================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                          |  92 kB  00:00:00     
(2/4): apr-1.4.8-5.el7.x86_64.rpm                                                               | 103 kB  00:00:00     
(3/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                     | 2.7 MB  00:00:02     
(4/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                               |  91 kB  00:00:05     
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  508 kB/s | 3.0 MB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                              1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                         2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                              3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                    4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                              1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                              2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                         3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                    4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7        apr-util.x86_64 0:1.5.2-6.el7        httpd-tools.x86_64 0:2.4.6-90.el7.centos       

Complete!
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# yum -y install httpd
[root@node105.yinzhengjie.org.cn ~]# echo "<h1>node105.yinzhengjie.org.cn<h1>" > /var/www/html/index.html
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /var/www/html/index.html
<h1>node105.yinzhengjie.org.cn<h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# systemctl start httpd
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      128                             *:80                                          *:*                  
LISTEN      0      128                             *:22                                          *:*                  
LISTEN      0      128                            :::22                                         :::*                  
[root@node105.yinzhengjie.org.cn ~]# 

3>.node106.yinzhengjie.org.cn节点配置

[root@node106.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                    | 3.6 kB  00:00:00     
extras                                                                                  | 2.9 kB  00:00:00     
updates                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                           | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                       | 153 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                         | 6.0 MB  00:00:02     
(4/4): updates/7/x86_64/primary_db                                                      | 5.8 MB  00:00:07     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================
 Package                    Arch                  Version                            Repository           Size
===============================================================================================================
Installing:
 httpd                      x86_64                2.4.6-90.el7.centos                base                2.7 M
Installing for dependencies:
 apr                        x86_64                1.4.8-5.el7                        base                103 k
 apr-util                   x86_64                1.5.2-6.el7                        base                 92 k
 httpd-tools                x86_64                2.4.6-90.el7.centos                base                 91 k

Transaction Summary
===============================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                  |  92 kB  00:00:00     
(2/4): apr-1.4.8-5.el7.x86_64.rpm                                                       | 103 kB  00:00:00     
(3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                       |  91 kB  00:00:00     
(4/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                             | 2.7 MB  00:00:00     
---------------------------------------------------------------------------------------------------------------
Total                                                                          2.6 MB/s | 3.0 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                      1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                 2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                      3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                            4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                      1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                      2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                 3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                            4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                           

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7     apr-util.x86_64 0:1.5.2-6.el7     httpd-tools.x86_64 0:2.4.6-90.el7.centos    

Complete!
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# yum -y install httpd
[root@node106.yinzhengjie.org.cn ~]# echo "<h1>https://www.cnblogs.com/yinzhengjie/</h1>" > /var/www/html/index.html
[root@node106.yinzhengjie.org.cn
~]# [root@node106.yinzhengjie.org.cn ~]# cat /var/www/html/index.html <h1>https://www.cnblogs.com/yinzhengjie/</h1> [root@node106.yinzhengjie.org.cn ~]# [root@node106.yinzhengjie.org.cn ~]# systemctl start httpd [root@node106.yinzhengjie.org.cn ~]# [root@node106.yinzhengjie.org.cn ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node106.yinzhengjie.org.cn ~]# [root@node106.yinzhengjie.org.cn ~]#

4>.node107.yinzhengjie.org.cn节点配置

[root@node107.yizhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.jdcloud.com
 * extras: mirror.jdcloud.com
 * updates: mirrors.aliyun.com
base                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                | 2.9 kB  00:00:00     
updates                                                                                                               | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                         | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                     | 153 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                                                       | 6.0 MB  00:00:03     
(4/4): updates/7/x86_64/primary_db                                                                                    | 5.8 MB  00:00:06     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================
 Package                           Arch                         Version                                     Repository                  Size
=============================================================================================================================================
Installing:
 httpd                             x86_64                       2.4.6-90.el7.centos                         base                       2.7 M
Installing for dependencies:
 apr                               x86_64                       1.4.8-5.el7                                 base                       103 k
 apr-util                          x86_64                       1.5.2-6.el7                                 base                        92 k
 httpd-tools                       x86_64                       2.4.6-90.el7.centos                         base                        91 k

Transaction Summary
=============================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                                                     |  91 kB  00:00:00     
(2/4): apr-1.4.8-5.el7.x86_64.rpm                                                                                     | 103 kB  00:00:00     
(3/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                |  92 kB  00:00:00     
httpd-2.4.6-90.el7.centos.x86_ FAILED                                                                      ]  1.6 B/s | 668 kB 434:12:11 ETA 
http://mirrors.nju.edu.cn/centos/7.7.1908/os/x86_64/Packages/httpd-2.4.6-90.el7.centos.x86_64.rpm: [Errno 12] Timeout on http://mirrors.nju.e
du.cn/centos/7.7.1908/os/x86_64/Packages/httpd-2.4.6-90.el7.centos.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.
(4/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                                           | 2.7 MB  00:00:01     
---------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                         66 kB/s | 3.0 MB  00:00:46     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                                                    1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                                               2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                    3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                                          4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                                                    1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                    2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                               3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                                          4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                                                         

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7               apr-util.x86_64 0:1.5.2-6.el7               httpd-tools.x86_64 0:2.4.6-90.el7.centos              

Complete!
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# yum -y install httpd
[root@node107.yizhengjie.org.cn ~]# cat /etc/httpd/conf.d/proxy.conf
#ProxyPass "/" "http://node105.yinzhengjie.org.cn/"              #此处我将配置注释掉了,此时无法访问node105.yinzhengjie.org.cn,但是可以访问node106.yinzhengjie.org.cn,想要同时启用则不行,还是推荐使用nginx或者haproxy来做反向代理吧。
#ProxyPassReverse "/" "http://node105.yinzhengjie.org.cn/"

ProxyPass "/blog" "http://node106.yinzhengjie.org.cn/"
ProxyPassReverse "/blog" "http://node106.yinzhengjie.org.cn/"
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# httpd -t
Syntax OK
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# systemctl start httpd
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                         Local Address:Port                                        Peer Address:Port              
LISTEN      0      128                                        *:80                                                     *:*                  
LISTEN      0      128                                        *:22                                                     *:*                  
LISTEN      0      128                                       :::22                                                    :::*                  
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 

5>.客户端访问

[root@node101.yinzhengjie.org.cn ~]# curl http://node107.yinzhengjie.org.cn/blog
<h1>https://www.cnblogs.com/yinzhengjie/</h1>
[root@node101.yinzhengjie.org.cn ~]# 

 

三.httpd服务的应用

  httpd主要应用在于web服务器,比如常见的lamp架构。

  httpd可以支持反向代理的功能,但和nginx,haproxy相比httpd显得有点逊色了。

  生产环境中httpd做反向代理的案例特别的少,因此不推荐大家使用httpd做反向代理,推荐使用专业的nginx或者haproxy来做反向代理。
posted @ 2019-12-09 21:07  尹正杰  阅读(468)  评论(0编辑  收藏  举报