Ticker

6/recent/ticker-posts

How To Configure Apache as a Reverse Proxy For Ubuntu

Apache2 proxy module’s ProxyPass and ProxyPassReverse functions provide a reverse proxy. To use ProxyPass and ProxyPassReverse, you must first know that where you want to direct site traffic. 


Proxy Pass

We have changed the ProxyPass and ProxyPassReverse settings on the apache server configuration file. 

 

Firstly, check and verify whether below modules are enabled on the server; if not, kindly enable the below module for proxy and ssl. 

 

  • Enable the Apache2 Proxy command  

 

#sudo a2enmod proxy      

#sudo a2enmod proxy_http 

 

  • Enable the SSL module command 

#sudo a2enmod ssl 


Run below command to create a proxy VirtualHost file, here we are taking an example of test project (Replace the name of your project in naming of config file)  

 

  #sudo nano /etc/apache2/sites-available/test.com.conf 

 

Then add the following block of codes in the apache config file (test.com.conf) and then save it. 

<VirtualHost *:80> 

      ServerName test.com

        ServerAlias test.com, www.test.com

       ErrorLog ${APACHE_LOG_DIR}/error.log 

        CustomLog ${APACHE_LOG_DIR}/access.log combined 

        RewriteEngine on 

        RewriteCond %{HTTPS} off 

        RewriteRule ^(.)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

        ProxyRequests Off 

        <Proxy *> 

        Order deny,allow 

        Allow from all 

        </Proxy> 

        ProxyPass / http://localhost:3001/ 

        ProxyPassReverse / http://localhost:3001/  

</VirtualHost>  

 

<VirtualHost *:443> 

       ServerName test.com

        ServerAlias test.com, www.test.com 

        ErrorLog ${APACHE_LOG_DIR}/error.log 

        CustomLog ${APACHE_LOG_DIR}/access.log combined 

        SSLEngine on 

        SSLCertificateFile    CertificatePathOnServer 

        SSLCertificateKeyFile   CertificateKeyPathOnServer 

        SSLCertificateChainFile  CACertFilePathOnServer 

        ProxyRequests Off 

        <Proxy *> 

        Order deny,allow 

        Allow from all 

        </Proxy> 

        ProxyPass / http://localhost:3001/ 

        ProxyPassReverse / http://localhost:3001/ 

</VirtualHost>   

  • Note: - Yellow Highlighted settings have been changed on the apache configuration file which was required to be added and which caused a redirection issue.   

  • Enable VirtualHost site with the a2ensite command 

 #sudo a2ensite test.com.conf 

  • Restart/Reload Apache2 Server 

#sudo systemctl restart apache2 (For Ubuntu instance) 

#sudo systemctl reload apache2 (For Live Production instance) 

Apache should now be serving your domain name. Now, let’s try to access your website in web browser.  

Reactions

Post a Comment

0 Comments