Traffic Flow
First of all, we need to understand how traffic will through the website, through our server, to the remote server and back again and for that, we need a diagram:
- User's Machine -> Redwire Servers -> Apache Proxy -> External Servers
DNS Records
The next thing to set up is to tell our Redwire server where to find the external machine to proxy from, as website's DNS address points to the external client servers. We need to override DNS for this host, which we can do by adding the following lines to hosts file:
www.dummyexample.com 195.149.56.12
This is set up so that we can (when we're ready) change the DNS records for the rest of the world to point to our servers, so that the proxying and redirects can happen. If we can't do this, we can't provide a part of our contracted services.
NOTE: Once these entries are in place, they will not need to be changed unless the IP address of dummyexample.com changes so bear this is mind.
Apache 2 Configuration
If you are using the Windows install, enable the module in your php.ini file - mod_rewrite is the module you'll need, and in Linux you can install this as an RPM or by source, which is how I've done it:
1. You'll need to change to the modules directory from your source code:
/usr/local/src/httpd/httpd-2.2.3/modules/proxy
2. Once there, compile and install the required modules:
/usr/local/apache2/bin/apxs -c -i mod_proxy.c proxy_util.c
/usr/local/apache2/bin/apxs -c -i mod_proxy_http.c proxy_util.c
This is a one-shot process; the APXS system compiles and installed two modules, which you will need in addition to mod_rewrite, which you should install in a similar manner.
Loading these into your Apache httpd.conf is critical, as is the ordering. The LoadModule lines for these entries should be in the correct sequence, as follows:
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Once these are entered, you need to check your Apache configuration; in my example, I am using a Linux server so these are the commands I enter, as root:
[root@server proxy]# apachectl configtest
Syntax OK
[root@server proxy]# apachectl stop
[root@server proxy]# apachectl start
Configuring the Site
Once the Apache modules are loaded in, we can now set up the site to proxy requests to another location, which we can do by changing the configuration file for the site we are working on, www.dummyexample.com so that its primary virtual host directive reads as follows:
ServerName www.dummyexample.com
RewriteEngine On
RewriteRule (.*) http://www.dummyexample.com/$1 [P,L]
As you can see, we still keep all of the normal ServerName and logging information but instead we enable URL rewriting for all paths in this virtual host, and by specifying the [P] option (mod_rewrite options are specified in square brackets) we tell mod_rewrite to proxy the request via a different URL. This is significant, because the web server then uses its host file to look up www.dummyexample.com and, as we have already set this up, this points to the external servers. So now, by default, all traffic for dummyexample.com is now proxied by our server, once the external DNS change is published.
Handling Redirects
This is a key requirement of the project; without this we cannot fulfil our obligations so I have to find a way to make the original mod_alias Redirect rules work. The original rules look like this:
# Original redirects
Redirect permanent /news/content.html http://www.dummysite.com
Redirect permanent /news/brochure.html http://www.dummysite.com/marketing.html
These will not work on our server now and if implemented as shown would cause a redirect loop as the site sends browsers back to itself. So they have to be enacted as part of the proxy and rewritten:
# Equivalent rewrite rules
RewriteRule /news/content.html http://www.dummysite.com [R=301,L]
RewriteRule /news/brochure.html http://www.dummysite.com/marketing.html [R=301,L]
Why the extra characters on the end? Well, mod_rewrite allows you to add some HTTP headers to response that's sent back, so we're sending back a 301: Redirect Permanent code to the browser.
We did some other cool stuff, specifically making part of the site non-proxied (i.e. served by us) but I will add this a bit later.
No comments:
Post a Comment
Feel free to add any thoughts on here; comments are not moderated, so please take as much care with your words as I do.