How to setup a reverse proxy
We are going to setup a reverse proxy today. Definition of reverse proxy as per Wiki is “In computer networks, a reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client as though they originated from the proxy server itself.”
The idea is explained below with an example scenario, It primarily considers a dev box for illustration purpose only. In real world we will have Web and App servers where Web server will hide presence of information served by one or more app servers.
Consider a website hosted on an Apache Server (Web server in this illustration). The web service(s) are hosted on a different server (App Server). I have added some details of both servers to illustrate settings and the actual url responses received by client of website.
Web Server: MAMP – Apache/2.2.26 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.6 PHP/5.5.14 mod_ssl/2.2.26 OpenSSL/0.9.8y DAV/2 mod_perl/2.0.8 Perl/v5.18.2) URL: http://localhost:8888 Application Folder Path: Project/UI
App Server: Tomcat 7 (Apache-Coyote/1.1) URL: http://localhost:8080/service Application Folder Path: Project/service
You will notice that i am using two different ports to host a Website and Web service in this example. Website is hosted using MAMP while the web service is on Tomcat 7.
Reverse Proxy Configuration
Add below given lines in Httpd.conf (Apache), These settings are to make a reverse proxy in Apache server running on mac using MAMP.
Reverse Proxy changes Apache
ProxyRequests Off ProxyPreserveHost on ProxyPass /api/ http://localhost:8080/service/ ProxyPassReverseCookiePath / /api/ ProxyPassReverseCookieDomain / localhost:8080/service <Location "/api/"> ProxyPassReverse / </Location>
As mentioned earlier the above changes are in Apache configuration on web server. The third line in this configuration listens for all incoming requests on web server with a /api/. If a request contains /api/ it is redirected to localhost:8080/service for execution. This url in example is depicting a web service.