Reverse Proxy Server 2: Basics

From The Uniform Server Wiki
Jump to navigation Jump to search

 

Uniform Server 5.0-Nano
Reverse Proxy.

The previous page looked at reverse proxy architecture and introduced front and back-end servers. This page shows how to run a proxy server using the standard modules distributed with Apache. Before looking at any examples an understanding of the proxy commands is essential.

Proxy commands - directives

The most important command for running a reverse proxy has been covered Proxy Requests Off you may be surprised to learn only two other commands are required however there are others available these tend to be more application specific.

ProxyPass path url

ProxyPass: This command, maps a remote server into your proxy server’s name space.

path: Is the folder or folders that you tack onto the end of your domain for example http://fred.com/info/

url: URL to the remote server for example http://localhost:82/ (you can also map a folder on this server)

ProxyPassReverse path url

ProxyPassReverse: This command masquerades one server as another. Apache adjusts URL's (location, Content-Location and URI headers) from the reverse-proxied server so they look as if they came from the server running the proxy engine.

path: Is the folder or folders that you tack onto the end of your domain for example http://fred.com/info/

url: URL to the remote server for example http://localhost:82/

Net result, when a user types the following http://fred.com/info/ into a browser the contents from server http://localhost:82/ will seamlessly appear in folder info.

Top

Preparation

All examples use three very short test pages as follows.

In folder C:\server_b\UniServer\www create a new file index.html with the following content:

<html>
<head>
<title>INDEX TEST</title>
</head>
<body>
<h2>INDEX TEST</h2>

<ul>
<li><a href="test/page1.html">Page 1</a></li>
<li><a href="test/page2.html">Page 2</a></li>
</ul>
<img src="images/logo.jpg" align="left" alt="Uniform Server" />
</body>
</html>

Create a new folder C:\server_b\UniServer\www\test inside this create two new files as follows:

page1.html - with following content:

<html>
<head>
<title>PAGE 1</title>
</head>
<body>
<h2>PAGE 1 Relative</h2>

<ul>
<li><a href="page2.html">Page 2</a></li>
<li><a href="../index.html">Back to Index Test</a></li>
</ul>
<img src="../images/logo.jpg" align="left" alt="Uniform Server" />
</body>
</html>

page2.html - with following content:

<html>
<head>
<title>PAGE 2</title>
</head>
<body>
<h2>PAGE 2 Server root relative</h2>

<ul>
<li><a href="/test/page1.html">Page 1</a> - Server root relative</li>
<li><a href="/index.html">Back to Index Test</a> - Server root relative</li>
<li><a href="http://localhost:82/index.html">Back to Index Test</a> - Absolute URL</li>

</ul>
<img src="/images/logo.jpg" align="left" alt="Uniform Server" />
</body>
</html>

Test

Run server_b and type the following into a browser: http://localhost:82/

Follow each link, check pages are displayed each containing a logo image, confirms correct operation..

Top

Example 1

Best way to understand the proxy process is to run a few examples.

Edit Apache configuration

Edit file C:\server_a\UniServer\usr\local\apache2\conf\httpd.conf

Navigate to bottom of page locate the Vhost section and edit to look like the following:

NameVirtualHost *

<VirtualHost *>
 ServerName localhost:80
 DocumentRoot C:/server_a/UniServer/www

ProxyRequests off
<Proxy *>
  Order deny,allow
  Deny from all
  Allow from 127.0.0.1
</Proxy>

ProxyPass         /info/  http://localhost:82/
ProxyPassReverse  /info/  http://localhost:82/

</VirtualHost>

Run Example

  1. Save the configuration file
  2. Re-start both servers
  3. Type following http://localhost/info/ into your browser

The index page (INDEX TEST) from server_b is displayed.

Top

Testing

Explore the two pages, you will quickly discover first page works fine however links on the second page fail.

In one of my write-ups to avoid problems I recommended all links shall be server root relative when creating a site using clean URLs. Page 2 uses server root relative links this clearly has introduced problems when running through a proxy.

Problem 1

Apache’s proxy engine does not rewrite links within a served page it only manipulates what is typed or would be typed in a users browsers address bar.

When Apache serves a page from a remote server (back-end) it adjusts the returned page headers to match that of a user request (browser).

Problem 2

The last link on page 2 is an absolute link and works! However notice it breaks out of our proxy server hence a failure with respect to the proxy.

Problem 3

Apache’s proxy engine does not rewrite links. Why is the image displayed after all it is using a root relative path?

Resource request

To explain problems 1 and 3, browsers use links to creates a resource request using the following rules:

  • If the link starts with a forward slash (/info) it adds only the domain name to complete the Url http://localhost/info.
  • If the link starts with two periods (../info) or no forward slash (info) it considers this a relative link.
    It checks the currently displayed page (in the address bar) and constructs a new request relative to this.
    For example current page http://localhost/fred/news/ link ../info gives http://localhost/fred/info.
  • If the link is a fully qualified URL (http://www.someother.com) its just uses that (Note: This breaks out of any reverse proxies.

Page 1 not found error is the result of a browser mapping resource request to the proxy to give http://localhost/test/page1.html.

Above also explains why the image is being picked up. Image path is /images/logo.jpg a browser adds only the domain localhost to give a final resource request of http://localhost/images/logo.jpg mapping it to the proxy. There exists an identical logo image hence why it appears to work correctly.

Replace logo image on the proxy server, run test again and it will clearly show which image is being picked up.

Note 1:

Your test image must be a jpeg with name logo.jpg

C:\server_a\UniServer\www\images\logo.jpg

Note 2:

When you breakout of the proxy you will see domain http://localhost:82

Hence remember to type http://localhost/info/

Top

Solution

One solution is to install and use mod_proxy_html this was specifically designed for rewriting links in a served page.

Top

Summary

The above has attempted to show the power of using Apache as a reverse proxy. It’s not overly difficult; you can find more tutorials on the Internet that explains reverse proxying better. What I have provided is a working example allowing you to hack code around and follow any examples provided.

The above proxy example is suitable for simple sites however you will at some time need to rewrite page links on-the-fly. This can be achieved using the mod_proxy_html module describe on the next page.

Top


Ric