{"id":858,"date":"2017-12-02T05:05:21","date_gmt":"2017-12-02T00:05:21","guid":{"rendered":"https:\/\/www.blueangel.host\/blog\/?p=858"},"modified":"2017-12-02T05:25:35","modified_gmt":"2017-12-02T00:25:35","slug":"apache-reverse-proxy","status":"publish","type":"post","link":"https:\/\/www.blueangel.host\/blog\/apache-reverse-proxy\/","title":{"rendered":"How To Use Apache Reverse Proxy with mod_proxy on Ubuntu 16.04"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-864\" src=\"https:\/\/www.blueangel.host\/blog\/wp-content\/uploads\/2017\/12\/reverse_proxy-1.png\" alt=\"apache reverse proxy\" width=\"630\" height=\"531\" srcset=\"https:\/\/www.blueangel.host\/blog\/wp-content\/uploads\/2017\/12\/reverse_proxy-1.png 630w, https:\/\/www.blueangel.host\/blog\/wp-content\/uploads\/2017\/12\/reverse_proxy-1-300x253.png 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/p>\n<p>Reverse Proxy refer to a kind of proxy server, which takes HTTP requests and distributes them transparently to many backend servers. These reverse proxies are valuable because lots of contemporary web apps are processing incoming HTTP requests with backend apps servers. These are not intended to be accessed by users openly and often just support basic HTTP features.<\/p>\n<p>A reverse proxy can be used to avoid underlying app servers from being openly accessed. They can be utilized to disseminate the load from the incoming requests to many diverse app servers. This improved performance at scale and provide fail-safeness. A reverse proxy can also bridge the gap with great features the app server does not provide, like compression, caching and SSL encryption.<\/p>\n<h2>This is the step on How to Use Apache Reverse Proxy with mod_proxy on Ubuntu 16<\/h2>\n<p><strong>Step One: Allowing Essential Apache Modules <\/strong><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Apache_HTTP_Server\" target=\"_blank\" rel=\"noopener\">Apache<\/a> has lots of modules packed with it, which are accessible but now allowed in a new installation. First, you will need to facilitate the ones utilize in this guide, which is the mod_proxy. Many of its affix modules, which expands its functionality in order to support diverse network protocols. Purposely, we will utilize:<\/p>\n<p>mod-proxy: Main proxy module for redirecting connection, it enables Apache to serve as an entry point to the primary app servers.<\/p>\n<p>mod-proxy balancer: add load balancing features for many backend servers<\/p>\n<p>mod-proxy http: adds assistance for proxying HTTP connection.<\/p>\n<p>These 4 modules can be enabled by means of following these commands in sequence.<\/p>\n<ul>\n<li>sudo a2enmod proxy<\/li>\n<li>sudo a2enmod proxy_http<\/li>\n<li>sudo a2enmod proxy_balancer<\/li>\n<li>sudo a2enmod lbmethod_byrequests<\/li>\n<\/ul>\n<p>In order to put these modifications into effect, you have to restart Apache by following this command:<\/p>\n<p>sudo systemctl restart apache2<\/p>\n<p>Now apache\u00a0 is set to serve as a HTTP requests to apache reverse proxy. The next step, you will have to make to basic backend servers. This is essential for you to identify whether or not the configuration is working properly.<\/p>\n<p><strong>Step 2 \u2014 Making Backend Test Servers<\/strong><\/p>\n<p>Operating simple backend servers is a very simple method to try if the configuration of Apache module is functioning correctly.<\/p>\n<p>Here, we will make 2 test servers the take action to HTTP requests. One server will state\u00a0<strong>Hello world!<\/strong>\u00a0while the other one will state\u00a0<strong>Howdy world!<\/strong>.<\/p>\n<p>We will be going to use Flask to make the test servers as an underlying application needs only some lines of codes.<\/p>\n<p>First update the package list; <em>sudo apt-get update<\/em><\/p>\n<p>Afterwards install Pip, the suggested Python package manager: <em>sudo apt-get -y install python3-pip<\/em><\/p>\n<p>When installing Flask, use Pip: <em>sudo pip3 install flask<\/em><\/p>\n<p>All the needed components are now installed, next start through making a fresh file which will have the code for the initial backend server in home directory of the existing user. <em>nano ~\/backend1.py<\/em><\/p>\n<p>Copy the code and then save:<\/p>\n<p><em>~\/backend1.py<\/em><\/p>\n<p><em>from flask import Flask<\/em><\/p>\n<p><em>app = Flask(__name__)<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>@app.route(&#8216;\/&#8217;)<\/em><\/p>\n<p><em>def home():<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 return &#8216;Hello world!&#8217;<\/em><\/p>\n<p>The 2<sup>nd<\/sup> backend server is precisely similar as the first, apart from going back to a diverse line of text, therefore begin through duplicating the primary line.<\/p>\n<p><em>cp ~\/backend1.py ~\/backend2.py<\/em><\/p>\n<p>Then open the recently copied file, modify the message and then save the file.<\/p>\n<p><em>~\/backend2.py<\/em><\/p>\n<p><em>from flask import Flask<\/em><\/p>\n<p><em>app = Flask(__name__)<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>@app.route<\/em>(&#8216;\/&#8217;)<\/p>\n<p><em>def home():<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 return &#8216;Howdy world!&#8217;<\/em><\/p>\n<p>Utilize this command <em>FLASK_APP=~\/backend1.py flask run &#8211;port=8080 &gt;\/dev\/null 2&gt;&amp;1 &amp;<\/em> to begin the initial background server. Also, this redirects Flask output to \/dev\/null\u00a0as it will cloud the console result further on.<\/p>\n<p>We are heading the <strong>flask<\/strong> command through setting FLASK_App background variable in similar line. These environment variables provide a simple way to transfer details into processes which are spawned. When using these variables, ensure the setting only applied to the command that\u2019s being run and will not keep available later on.<\/p>\n<p>The same way, use this command to begin the 2<sup>nd<\/sup> server. Keep in mind the diverse value for the Flask Application environment variable.<\/p>\n<p><em>FLASK_APP=~\/backend2.py flask run &#8211;port=8081 &gt;\/dev\/null 2&gt;&amp;1 &amp;<\/em><\/p>\n<p><strong>Step Three: Modify the Default Configuration to Allow Reverse Proxy<\/strong><\/p>\n<p>This step we are using the configuration at virtual host stage.\u00a0 On apache\u2019s default installation, there\u2019s only one, default virtual host allowed. But, you can utilize the entire configuration fragments in other hosts too.<\/p>\n<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>\n<p>Open Apache configuration file with the use of nano\u00a0or any preferred text editor. This should be like this:<\/p>\n<p><em>sudo nano \/etc\/apache2\/sites-available\/000-default.conf<\/em><\/p>\n<p>Here, you will see the &lt;VirtualHost *:80&gt;\u00a0block beginning on the primary line. The sample below shows how to organize the block to reverse proxy for one backend server, and the other one is for many backend servers:<\/p>\n<p><strong>Single Backend Server on Apache Reverse Proxy<\/strong><\/p>\n<p>Restore the entire contents in <strong><em>Virtual Host <\/em><\/strong>so you can organize file. The configured file must be appear this way:<\/p>\n<p><em>\/etc\/apache2\/sites-available\/000-default.conf<\/em><\/p>\n<p><em>&lt;VirtualHost *:80&gt;<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPreserveHost On<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPass \/ http:\/\/127.0.0.1:8080\/<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPassReverse \/ http:\/\/127.0.0.1:8080\/<\/em><\/p>\n<p><em>&lt;\/VirtualHost&gt;<\/em><\/p>\n<p>Once you choose to followed together with the sample servers mentioned in step two, utilize 127.0.0.1:8080<\/p>\n<p><strong>Here, there are 3 directives involve\u201d<\/strong><\/p>\n<ul>\n<li>ProxyPreserveHost which makes Apache module pass the novel host header. This is valuable, because it makes backend server to be aware of address utilized to access the app.<\/li>\n<li>ProxyPass the main proxy configuration directive<\/li>\n<li>ProxyPass Reverse must have similar arrangement as ProxyPass.<\/li>\n<\/ul>\n<p>Restart the Apache to put the modifications into effect.<\/p>\n<p><em>sudo systemctl restart apache2<\/em><\/p>\n<p>Accessing\u00a0http:\/\/your_server_ip\u00a0in website browsers, you\u2019ll witness your backend server reply instead of normal Apache welcome page. Once you followed the step 2, it means you will witness <strong>Hellow world!<\/strong>.<\/p>\n<p><strong>Load Balancing In Many Backend Servers<\/strong><\/p>\n<p>In case you have many backend servers, the best technique to hand out the traffic when proxying is through using mod-proxy load balancing features.<\/p>\n<p>Replace the entire contents in Virtual Host with the subsequent, the arrange must be like this:<\/p>\n<p><em>\/etc\/apache2\/sites-available\/000-default.conf<\/em><\/p>\n<p><em>&lt;VirtualHost *:80&gt;<\/em><\/p>\n<p><em>&lt;Proxy balancer:\/\/mycluster&gt;<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 BalancerMember http:\/\/127.0.0.1:8080<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 BalancerMember http:\/\/127.0.0.1:8081<\/em><\/p>\n<p><em>&lt;\/Proxy&gt;<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPreserveHost On<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPass \/ balancer:\/\/mycluster\/<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0 ProxyPassReverse \/ balancer:\/\/mycluster\/<\/em><\/p>\n<p><em>&lt;\/VirtualHost&gt;<\/em><\/p>\n<p>The arrangement is the same to the preceding one, but rather than specifying only one backend server openly. We have utilized an extra Proxy block in order to define multiple backend servers.<\/p>\n<p>balancer:\/\/mycluster\u00a0 is the name of the block. The name can be changed and it has single or multiple balance members that specify the basic backed server address.<\/p>\n<p>once you choose to follow the examples server in Step #2, utilize 127.0.0.1:8080\u00a0and\u00a0127.0.0.1:808. In case you have a personal app server, use its address instead.<\/p>\n<p>Restart Apache to see changes:\u00a0 sudo systemctl restart apache2<\/p>\n<p>Accessing <em>http:\/\/your_server_ip on your <\/em>apache reverse proxy, you will witness your backend servers&#8217; replies rather than typical Apache page.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>Now you already know how to install Apache module as your reverse proxy to single or multiple basic app servers. Keep in mind that mod_proxy could be utilized efficiently to arrange reverse proxy to app servers that are written in a huge range of technologies and languages.<\/p>\n<p>While mod_proxy_http with mod_proxy is the most utilized combination of Apache modules, there are others which support diverse network protocols.<\/p>\n<p>Thank you for reading our apache reverse proxy article. Have a wonderful day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reverse Proxy refer to a kind of proxy server, which takes HTTP requests and distributes them transparently to many backend servers. These reverse proxies are valuable because lots of contemporary web apps are processing incoming HTTP requests with backend apps servers. These are not intended to be accessed by users openly and often just support&#8230;<\/p>\n","protected":false},"author":2,"featured_media":864,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[243],"class_list":["post-858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-apache-reverse-proxy"],"_links":{"self":[{"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":6,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/posts\/858\/revisions\/871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/media\/864"}],"wp:attachment":[{"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blueangel.host\/blog\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}