How to install mod_proxy and setup reverse proxy in Apache Ubuntu
Today in this tutorial we will learn about installing apache proxy module and how to setup reverse proxy.
The module name is mod_proxy ,it is core module which supports Forward and Reverse Proxy settings in Apache WebServer. There are other modules also which is required in Apache webserver as per the requirement for example
mod_proxy_http, mod_proxy_ftp, mod_proxy_ajp, mod_proxy_balancer, and mod_proxy_connect
Reverse proxy is widely used to access the webserver which is behind the firewall.Reverse Proxy has the capability to load balance the request among the backend-server as well caching can also be done.Mainly, reverse proxies can be used simply to bring several servers into the same URL space.
For eg. lets see this scenario –
Scenario 1:
We have One Web server which has public ip address and DNS record means it can be accessible by using the url www.example.com or http://example.com
We have some internal webserver lets assume, internalwebserver1,internalwebserver2 and internalwebserver3. Internal web servers can not be access directly from public internet because they have private IP as well as they are behind the firewall. Means if someone try to access these internal server it will get error No such host.
But there are some important web application or webpages are serving from internal web server which is required to map with example.com .
So in this case what we will do we will setup the Reverse Proxy server. If any request come to main URL i.e example.com the Web Server will check which path is mapped to its internal web server in configuration file.
OK have this another Scenarios –
Scenario 2:
I have one OpenERP server which runs in port no. 8069 eg. http://openrp.example.com:8069 but I would like to open the website openerp.example.com in URL means port no. 80 bydefault. In this case also we can setup reverse proxy in single openerp server.
Install mod_proxy and setup reverse proxy in Apache Webserver
Follow the given below steps.
Note: In all given below example replace the value of ServerAdmin,ServerName & ServerAlias as per your server information
Step 1: Install the module
sudo apt-get install libapache2-mod-proxy-html
Step 2: Installing the dependency libxml2-dev
apt-get install libxml2-dev
Step 3: Load the module
a2enmod proxy proxy_http
Step 4: Create the Virtual Host in apache configuration file . If your configuration is located in conf.d you have to do changes in that file. I am giving example with default setting in Apache Webserver in Ubuntu
vi /etc/apache2/sites-enabled/000-default
Paste the below two lines within VirtualHost tag
ProxyPass /foo http://internalwebserver1.example.com/give/path/of-requested-location ProxyPassReverse /foo http://internalwebserver1.example.com/give/path/of-requested-location
Lets see the configuration of my file,it will clear the doubt
ServerAdmin sharad@example.com ServerName example.com ServerAlias example.com ProxyPass /foo http://internalwebserver1.example.com/files/webapp ProxyPassReverse /foo http://internalwebserver1.example.com/files/webapp
Step 5: Now restart the apache
/etc/init.d/apache2 restart
Step 6: Now Openbrowser in client machine and check the website. Done
Now for Scenario 2:
Follow the all above given steps except in Step 4 you have to do below given setting
Below is the reference of my configuration file
ServerAdmin sharad@example.com ServerName openerp.example.com ServerAlias openerp.example.com ProxyPass / http://openerp.example.com:8069 ProxyPassReverse / http://openerp.example.com:8069
Restart the apache2 and open the browser and check it
/etc/init.d/apache2 restart
Which version of Ubuntu is this for? I tried with 16 and some of the required components fail
Hello Karl,
This is practically verified till Ubuntu 14.04 LTS server . Soon we will publish the tutorial on Ubuntu 16.06 LTS also.
Regards
Sharad
thank’s for yur example
i when i do like this, i access to website without port and context name, but the css and javascript files doesnt work
what must i add ?
thank’s
Hello Faycal,
It should work.
Can you check with developers that does it require “CORS” on Apache or js/css files called from remote server then check the connectivity.
Regards
Sharad
It’s my personnal projet, i’m the only developper.
what’s “CORS” ? i’m using Ubuntu server 14.04 and apache 2
Hello Faycal,
May I see the code where you are using the style.css and js file.
To keep it private , you can send email to admin@sharadchhetri.com
Regards
Sharad
hello,
i’ve sent an email with simple code
thank’s
I am facing same problem my page is not loaded just the page name is shown no CSS and java script loaded. When I access my application from CF VM Box it works fine but when accessing outsite the method u mentioned it gives me issue as mentioned above.
Thanks for the great tutorial!
I was able to forward openerp’s 8069 port to 80 except the CSS and images are blocked… Would you know how to fix this? Is it a permission issue?
Hello,
1. It may be permission issue.
2. or the image url is not correct in code. May be image url still using port 8069 for eg.
3. Recommend to install firbug on firefox and debug the issue once.
Regards
Sharad
To load the module , below does not work..
a2enmod mod_proxy mod_proxy_http
Correct one is,
a2enmod proxy proxy_http
Thanks Kumar,
I did typo by using mod as prefix(mod_proxy mod_proxy_http module term is used in CentOS and Redhat ). In Ubuntu and debian while enabling module only proxy and proxy_http term can be used.