Apache as Reverse Proxy

Apache as Reverse Proxy

1. Introduction

Apache is the world’s most used web server software. Apache HTTP Server can host IntraWeb applications (as ISAPI application), but it can also be used as a reverse proxy for IntraWeb Stando Alone applications. Explaining how reverse proxy servers work is beyond the scope of this document, thus I recommend a quick introduction on reverse proxy servers: https://en.wikipedia.org/wiki/Reverse_proxy

Among other things, a reverse proxy allows you to run multiple IntraWeb Stand Alone servers (services) on the same machine, using different ports, but using the same external address and port. You can have something like this:

  • www.yourdomain.com/support -> points to IntraWeb application 1, running on port A, e.g. 8080
  • www.yourdomain.com/sales -> points to IntraWeb application 2, running on port B, e.g. 8081
  • and so on…

2. Pre-requisites

2.1. Apache HTTP Server 2.x (always use latest version)

3. Configuring Apache as a Reverse Proxy Server

First of all, we will need to configure Apache to act as a reverse proxy. All Apache configurations are done through modifications to the httpd.conf file (located in subfolder /conf, on Apache installation folder).

Open httpd.conf using any text editor (e.g. Notepad) and enable these modules:

  • mod_proxy.so
  • mod_proxy_connect.so
  • mod_proxy_http.so
  • mod_rewrite.so
  • mod_xml2enc.so

each module is enabled removing the # char from the beginning of the line which loads the module. So the lines referencing those modules should look like this:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule xml2enc_module modules/mod_xml2enc.so

Locate the line which contains the port used by Apache. It should be in the form:

Listen <port number>

where <port number> is the port used by Apache to receive incoming requests. This port is defined during Apache installation.

Add this section, just after the line above (“Listen”):

<VirtualHost *:[port number]>
ProxyPreserveHost On
ProxyPass [virtual path] [address:port]
ProxyPassReverse [virtual path] [address:port]
ServerName [server name]
</VirtualHost>

example:

<VirtualHost *:8080>
ProxyPreserveHost On
ProxyPass “/features” “http://127.0.0.1:8888/”
ProxyPassReverse “/features” “http://127.0.0.1:8888/”
ServerName localhost
</VirtualHost>

the above rule says to Apache server: take any incoming request on port 8080 addressed to /features path and redirect it to 127.0.0.1, port 8888. This way, a request to http://yourserver.com/features will be redirected to http://127.0.0.1:8888.

4. Application setup

In order to support the reverse proxy feature, your IntraWeb application needs to know the virtual path used by the reverse proxy server. This is done setting the RewriteURL property of the ServerController. In our example:

procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);begin Self.RewriteURL := ‘/Features’;end;

And that’s all. Your application is ready to go. Ideally, you should make this setting dynamic, preferably read it from an external configuration file, like an .INI file. The code below shows how to do that:

uses IniFiles, IW.Common.AppInfo;procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);const INISECTION = ‘ReverseProxy’;var IniFileName: string; IniFile: TIniFile; ReverseProxyEnabled: Boolean;begin IniFileName := ChangeFileExt(TIWAppInfo.GetAppFileName, ‘.ini’); if FileExists(IniFileName) then begin IniFile := TIniFile.Create(IniFileName); try ReverseProxyEnabled := IniFile.ReadBool(INISECTION, ‘Enabled’, False); if ReverseProxyEnabled then begin Self.RewriteURL := IniFile.ReadString(INISECTION, ‘VirtualPath’, ”); end; finally IniFile.Free; end; end;end;

This will require an .INI file like this:

[ReverseProxy]
Enabled=1
VirtualPath=/Features

5. Testing your application

After your setup is ready, you can now test your application. Note that we reference this application using this URL:

http://localhost:8080/Features/

where 8080 is our Apache port. You should change server name and/or port as appropriate for your setup.

5.1 Starting Apache server…

Apache Started

5.2 Testing Apache on port 8080 (basic test, just to check whether it is working)

Apache Test

5.3 Testing our application, through Apache Reverse Proxy, on port 8080:

Apache Works!

6. See also

  • Deploying your application as a Stand Alone Server