Apache2 configuration for proxy

Proxy Modules:

  • mod_proxy: The main proxy module for Apache that manages connections and redirects them.
  • mod_proxy_http: This module implements the proxy features for HTTP and HTTPS protocols.
  • mod_proxy_ftp: This module does the same but for FTP protocol.
  • mod_proxy_fcgi: FastCGI
  • mod_proxy_connect: This one is used for SSL tunnelling.
  • mod_proxy_ajp: Used for working with the AJP protocol.
  • mod_proxy_wstunnel: Used for working with web-sockets (i.e. WS and WSS).
  • mod_proxy_balancer: Used for clustering and load-balancing.
  • mod_proxy_hcheck: Dynamic health check of Balancer members (workers) for mod_proxy
  • mod_cache: Used for caching.
  • mod_headers: Used for managing HTTP headers.
  • mod_deflate: Used for compression.

Enable proxy:

sudo a2enmod proxy

sudo a2enmod proxy_http

sudo a2enmod proxy_html

sudo a2enmod xml2enc

sudo systemctl restart apache2

Example virtual host #1

<VirtualHost *:80>

    ServerName localhost

    ProxyPreserveHost On

    ProxyPass "/" "http://192.168.0.0:8080/"

    ProxyPassReverse "/" "http://192.168.0.1:8080/"

</VirtualHost>

Example virtual host #2

<VirtualHost *:80>

    ServerName localhost

    ProxyPreserveHost On

    ProxyPass "/subdir/" "http://192.168.0.221:8000/"

    ProxyPassReverse "/subdir/" "http://192.168.0.221:8000/"

     <Location "/subdir">

         ProxyPassReverse    /

         ProxyHTMLURLMap     / /subdir/

     </Location>

</VirtualHost>

Example virtual host #3 (Don’t proxy /static)

<VirtualHost *:80>

    ServerName localhost

    ProxyPreserveHost On

    ProxyPass "/" "http://192.168.0.221:8000/"

    ProxyPassReverse "/" "http://192.168.0.221:8000/"

    Alias /static/ /var/www/html/static/

    <Directory /var/www/html/static>

        Require all granted

    </Directory>

    <Location /static>

        ProxyPass "!"

    </Location>

</VirtualHost>

Enable load balancing:

sudo a2enmod proxy_balancer

sudo a2enmod proxy_hcheck

sudo systemctl restart apache2

Example load balancing #1

<Proxy balancer://myset>

    BalancerMember http://www2.example.com:8080

    BalancerMember http://www3.example.com:8080

    ProxySet lbmethod=bytraffic

</Proxy>

ProxyPass "/images/"  "balancer://myset/"

ProxyPassReverse "/images/"  "balancer://myset/"

Example load balancing #2 (www3 handles 3 times traffic and timeout is 1)

<Proxy balancer://myset>

    BalancerMember http://www2.example.com:8080

    BalancerMember http://www3.example.com:8080 loadfactor=3 timeout=1

    ProxySet lbmethod=bytraffic

</Proxy>

ProxyPass "/images"  "balancer://myset/"

ProxyPassReverse "/images"  "balancer://myset/"

Example failove

<Proxy balancer://myset>

    BalancerMember http://www2.example.com:8080

    BalancerMember http://www3.example.com:8080 loadfactor=3 timeout=1

    BalancerMember http://hstandby.example.com:8080 status=+H

    BalancerMember http://bkup1.example.com:8080 lbset=1

    BalancerMember http://bkup2.example.com:8080 lbset=1

    ProxySet lbmethod=byrequests

</Proxy>

ProxyPass "/images/"  "balancer://myset/"

ProxyPassReverse "/images/"  "balancer://myset/"
~~~

Balancer Manager (Don’t enable it in production)

<Location "/balancer-manager">

    SetHandler balancer-manager

    Require host localhost

</Location>
~~~

Controlling access proxy

~~~bash
<Proxy "*">

  Require ip 192.168.0

</Proxy>
~~~

Reference:

https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension
  
https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
  
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html
Written on November 30, 2016