Linux server setups for traffic redirection - apache/php/bind
An easy to manage server setup for traffic from numerous domains is different than most web hosts are used to as they are used to completely different sites for each domain/client.
This thread explains a simple setup that allows new domains to easily be redirected to ppc park pages - with the server acting as both nameserver and webserver. It is not meant to be a line-by-line setup, but goes into more detail than a basic overview would - it should be enough to give an insight into how these things hang together
It is supplied "as is" with no warranty - explicit or implied - for suitability, completeness or errors.
introduction to how the dns system works (ignoring isp caches etc):
When someone types a domain into their address bar their system checks the registry to get the location of nameservers that have the authority for the domain. these nameservers are what actually hold the ip address (location) that the domain should resolve to. so when you register a domain and assign it nameservers, those nameservers are the ones that will then need to hold the correct IP address details for that domain name. Once the browser has the ip address from the nameserver it can then send its request to the webserver at that ip address. the webserver looks at the domain name in the request and issues the relevant data which will usually be a html webpage for a type-in.
So in summary:- Address typed into browser
- Authoritative nameservers for that domain obtained from registry
- Those nameservers queried with the domain name to get an ip address
- The webserver at that ip address is sent the page request
- The webserver then issues content relative to that domain name
The use of the word "server" can get confusing, this might help to clarify it:
A server is a piece of hardware with an operating system on it that listens to network traffic
A nameserver is a program that runs on a server and issues the IP addresses of where a domain should resolve to in response to being given a domain name.
A webserver is a program that runs on a server and issues content based on the domain name. Traffic gets to the webserver by being directed by a nameserver
A server can run a webserver and nameserver(s) on the same piece of hardware and can have more than one IP address that routes to it.
--
Basic setup
Nameserver - Initial setup:
For the sake of this exercise, lets assume the server host setup the server to resolve on ip address: 111.222.333.1 and they configured it so that ns1.domain.tld resolves to that ip address and that the secondary nameserver was also configured to be run from the one server as ns2.domain.tld on address 111.222.333.2. Note: whilst its possible to run 2 nameservers (primary and secondary) on one server, if the server fails they both fail - this isn't an issue if you only run the webserver on this server as if the nameservers have failed its likely the webserver has failed too.
For the nameserver to issue IP addresses when queried for domains it needs to know which domains it should handle and what it should return when queried. There are 2 main file types used for this:
1) zone db files - usually located in /var/named - these contain the specific configuration that is returned when queried.
For traditional hosting, each domain/client on the server might have its own zone db file - but for traffic its easier to just set up one file. this is what a zone db file might look like for traffic conversion:
Code:
$TTL 900
@ IN SOA ns1.domain.tld. root.ns1.domain.tld. (
2005030501 ;serial number YYYYMMDDxx
10800 ;refresh time in seconds
3600 ;retry time in seconds
604800 ;time in seconds
900 ) ;minimum time to live in seconds
IN NS ns1.domain.tld.
IN NS ns2.domain.tld.
;host information
IN MX 5 mail.domain.tld.
IN A 111.222.333.1
* IN A 111.222.333.1
breaking it down for the important stuff:
TTL is the "time to live", when a nameserver is queried for a domain the TTL tells the recipient how long they should consider the reply as "current" in their cache (a cache a memory, which is used to save them looking it up every time) - the reason for this shorter ttl is that if a change of ip is needed for some reason, the change is picked up quickly (15 minutes in this case if their cache adheres to the ttl).
the serial number has to be incremented each time a change to the zone db file is made, this way a running nameserver knows if it has changed since the last time it loaded it in.
the IN A records tell the querying browser which IP address to send their request for content to. the blank line is to handle domain.tld and issues 111.222.333.1 in response, the "*" is a wildcard and this allows anything.domain.tld to be resolved to 111.222.333.1 too. So if a user mistyped www.domain.tld as ww.domain.tld it will still resolve - likewise if they typed in wibble.domain.tld that would resolve too. a lot of zone db's are set up with explicit subdomains so instead of "*" the host might set it up with just blank and "www" - that is why a lot of sites don't resolve if ww.domain.tld is typed in and people may assume the site is down not realizing they made a typo of "www".
So lets take it that we have the above zone db file saved as /var/named/ppc.zone
2) /etc/named.conf is the file that contains which domain names the nameserver will have authority for.
to add domains to the server, all that needs to be done is to add an entry like this for each domain to /etc/named.conf:
Code:
zone "somedomain.tld" {
type master;
file "ppc.zone";
};
and then to reload the nameserver (that tells the nameserver to pick use the additional changes) - this can usually be done with:
/etc/rc.d/init.d/named reload
[ If you want to make it easier each time, you can create a file with that as content called something like /usr/bin/nameload and then chmod +x /usr/bin/nameload - this way just typing the easier shortcut "nameload" will reload the nameserver]
Webserver:
Apache listens to requests sent to its IP address. It looks at the domain name and checks its configuration file to find where the files it should return are stored. The configuration file is called httpd.conf - the location of this file will depend on how the server was setup (you can find it by typing in "locate httpd.conf").
Normally, the httpd.conf file would be set up for every domain the server manages as normally each domain on a server would issue different content. But for traffic, there will be just one main location to use - we can use the file at that location to determine what to do based on the domain name being looked up. This makes management much easier.
Apache can be setup to handle "virtualhosts" - that means it issues different data for different domains. As mentioned above, these would normally be explicitly configured - but apache has a useful feature in that for domains not explicitly setup it will use the first one. this means that not every domain has to be set up.
To set up the defaul virtualhost requires adding something like this to the end of httpd.conf file (assuming it has no other virtualhost settings in there).
Code:
NameVirtualHost 111.222.333.1
<VirtualHost domain.tld>
ServerName domain.tld
ServerAdmin webmaster@domain.tld
DocumentRoot /www/htdocs/redirect
ServerAlias *.domain.tld
DirectoryIndex index.php
ErrorDocument 404 /index.php
</VirtualHost>
The above includes the ErrorDocument 404 line which handles pages that no longer exist or are mistyped and redirects them to the "home" page (these are known as 404 errors)- which is set to be found at /www/htdocs/redirect/index.php in the above example (a combination of the root and the directoryindex value which is the file to be used if no file is given ie domain.tld vs domain.tld/page.ext).
once added the webserver needs to be restarted to take effect, this can be done using apachectl (which can again be found using "locate apachectl") type in "/path/apachectl restart" where path is the path to apachectl.
All that remains is to set up the actual file that handles it all. this is the file at /www/htdocs/redirect/index.php using the above example.
referer redirects
the easiest type of redirect is if the ppc company can determine the domain name from the referer, as this means you don't have to work out what the domain was yourself - the ppc company does by looking at what the domain was that sent the person to them.
for this type of redirect, the content for index.php can simply be the following to retain the domain in the address bar:
Code:
<html>
<head>
<title>Find It Here</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<frameset rows='*' frameborder='NO' border='0' framespacing='0' cols='*'>
<frame name='content' src='http://ppc_companies_referer_detect_url></frameset>
<noframes>
<a href='http://ppc_companies_referer_detect_url'><font size='+2'>
Click Here To Continue
</font></a></noframes>
</html>
or, if you aren't bothered about the address in the address bar a header redirect, index.php can be just:
this tells the browser to go to that url and displays the new url in the address bar.