CentOS Stream 9
Sponsored Link

Apache httpd : Configure mod_proxy2022/03/16

 
Enable [mod_proxy] module to configure reverse proxy settings.
This example is based on the environment like follows.
-----------+---------------------------+-----------
           |                           |
           |10.0.0.31                  |10.0.0.51
+----------+-----------+   +-----------+----------+
|   [ www.srv.world ]  |   | [ node01.srv.world ] |
|      Web Server#1    |   |      Web Server#2    |
+----------------------+   +----------------------+

[1] [mod_proxy] is included in [httpd] package and it is enabled by default, so it's possible to configure quickly.
# modules are enabled by default

[root@www ~]#
grep "mod_proxy" /etc/httpd/conf.modules.d/00-proxy.conf

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
.....
.....

[root@www ~]#
vi /etc/httpd/conf.d/revers_proxy.conf
# create new

<IfModule mod_proxy.c>
    ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>
    # backend server and forwarded path
    ProxyPass / http://node01.srv.world/
    ProxyPassReverse / http://node01.srv.world/
</IfModule> 

[root@www ~]#
systemctl reload httpd

[2] If SELinux is enabled, change policy.
[root@www ~]#
setsebool -P httpd_can_network_connect on

[3] Access to frontend server to verify backend server responses like follows.
[4]
It's possible to configure load balancing settings.
-----------+---------------------------+--------------------------+------------
           |                           |                          |
           |10.0.0.31                  |10.0.0.51                 |10.0.0.52
+----------+-----------+   +-----------+----------+   +-----------+----------+
|   [ www.srv.world ]  |   | [ node01.srv.world ] |   | [ node02.srv.world ] |
|      Web Server#1    |   |      Web Server#2    |   |      Web Server#3    |
+----------------------+   +----------------------+   +----------------------+

[root@www ~]#
vi /etc/httpd/conf.d/revers_proxy.conf
# create new

<IfModule mod_proxy.c>
    ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>
    # specify the way of load balancing with [lbmethod]
    # also possible to set [bytraffic] which means httpd balances requests by traffic
    ProxyPass / balancer://cluster lbmethod=byrequests
    <proxy balancer://cluster>
        BalancerMember http://node01.srv.world/ loadfactor=1
        BalancerMember http://node02.srv.world/ loadfactor=1
    </proxy>
</IfModule>

[root@www ~]#
systemctl reload httpd

[5] Access to frontend server to verify backend servers response like follows.
Matched Content