0x2267e877…b6c2sent to0x7944a0c3…ebfe·#8,912,686·view on Etherscan
How To Use Apache as a Reverse Proxy with mod_proxy on CentOS 7(1)proxy<h1 style="text-align: center;">How To Use Apache as a Reverse Proxy with mod_proxy on CentOS 7</h1><p style="text-align: right;">By <a href="https://www.digitalocean.com/community/users/mati">Mateusz Papiernik</a><br /> </p><h3 id="introduction">Introduction</h3><p>A <em>reverse proxy</em> is a type of proxy server that takes HTTP(S) requests and transparently distributes them to one or more backend servers. Reverse proxies are useful because many modern web applications process incoming HTTP requests using backend application servers which aren’t meant to be accessed by users directly and often only support rudimentary HTTP features.</p><p>You can use a reverse proxy to prevent these underlying application servers from being directly accessed. They can also be used to distribute the load from incoming requests to several different application servers, increasing performance at scale and providing fail-safeness. They can fill in the gaps with features the application servers don’t offer, such as caching, compression, or SSL encryption too.</p><p>In this tutorial, you’ll set up Apache as a basic reverse proxy using the <code>mod_proxy</code> extension to redirect incoming connections to one or several backend servers running on the same network. This tutorial uses a simple backend written with the with <a href="http://flask.pocoo.org/">Flask web framework</a>, but you can use any backend server you prefer.</p><h2 id="prerequisites">Prerequisites</h2><p>To follow this tutorial, you will need:</p><ul> <li>One CentOS 7 server set up with <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-centos-7">this initial server setup tutorial</a>, including a sudo non-root user.</li> <li>Apache 2 installed on your server by following Step 1 of <a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7">How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on CentOS 7</a>.</li> <li>Optionally, the <code>nano</code> text editor installed with <code>yum install nano</code>. CentOS comes with the <code>vi</code> text editor by default, but <code>nano</code> can be more user friendly.</li></ul><h2 id="step-1-—-introducing-necessary-apache-modules">Step 1 — Introducing Necessary Apache Modules</h2><p>The modules that are needed to use Apache as a reverse proxy include <code>mod_proxy</code> itself and several of its add-on modules, which extend its functionality to support different network protocols. Specifically, we will be using:</p><ul> <li><code>mod_proxy</code>, the main proxy module Apache module for redirecting connections; it allows Apache to act as a gateway to the underlying application servers.</li> <li><code>mod_proxy_http</code>, which adds support for proxying HTTP connections.</li> <li><code>mod_proxy_balancer</code> and <code>mod_lbmethod_byrequests</code>, which add load balancing features for multiple backend servers.</li></ul><p>All four modules are enabled by default on a fresh CentOS 7 installation. You can verify that they are enabled by running:</p><p>$ httpd -M</p><p>The command output will list all enabled Apache modules. The four lines you’re looking for are the aforementioned module names:</p><pre><code langs=""> proxy_module (shared). . . lbmethod_byrequests_module (shared). . . proxy_balancer_module (shared). . . proxy_http_module (shared)</code></pre><h2 id="step-2-—-creating-backend-test-servers">Step 2 — Creating Backend Test Servers</h2><p>Running some simple backend servers is an easy way to test if your Apache configuration is working properly. Here, we’ll make two test servers which respond to HTTP requests with printing a line of text. One server will say <strong>Hello world!</strong> and the other will say <strong>Howdy world!</strong>.</p><p><strong>Note:</strong> In non-test setups, backend servers usually all return the same kind of content. However, for this test in particular, having the two servers return different messages makes it easy to check that the load balancing mechanism uses both.</p><p>Flask is a Python microframework for building web applications. We’re using Flask to create the test servers because a basic app requires just a few lines of code. You don’t need to know Python to set these up, but if you’d like to learn, you can look at <a href="https://www.digitalocean.com/community/tags/python?type=tutorials">these Python tutorials</a>.</p><p>Let’s install the IUS package repository files first. IUS (Inline with Upstream Stable) is a community project that brings up-to-date versions of select software to CentOS, including Python 3.</p><h2 id="step-3-—-modifying-the-default-configuration-to-enable-reverse-proxy">Step 3 — Modifying the Default Configuration to Enable Reverse Proxy</h2><p>In this section, we will set up the default Apache virtual host to serve as a reverse proxy for single backend server or an array of load balanced backend servers.</p><p><strong>Note</strong>: In this tutorial, we’re applying the configuration at the virtual host level. On a default installation of Apache, there are no virtual hosts configured. We will be creating a single, default virtual host that will catch all traffic. However, you can use all those configuration fragments in other virtual hosts as well. To learn more about virtual hosts in Apache, you can read this <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-centos-7">How To Set Up Apache Virtual Hosts on CentOS 7</a> tutorial.</p><p>If your Apache server acts as both HTTP and HTTPS server, your reverse proxy configuration must be placed in both the HTTP and HTTPS virtual hosts. To learn more about SSL with Apache, you can read this <a href="https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-apache-for-centos-7">How To Create a SSL Certificate on Apache for CentOS 7</a> tutorial.</p><p>Create the new default virtual host by creating a new empty Apache configuration file in <code>/etc/httpd/conf.d</code> directory using <code>nano</code> or your favorite text editor.</p><pre>$<code langs="">sudo nano /etc/httpd/conf.d/default-site.conf</code></pre>