Hello everyone,

I am not sure what I am wanting to do is possible, but thought I would ask here to find out.

I want to use a PHP redirect script to load balance my website as it is starting to get hammered so I want to redirect say 1 out of every 5 visitors to a mirror site just with different domain name. Anyone have an idea how to do this?

You'd actually want to use Apache itself to do the load balancing, not PHP. Using things like mod_proxy_balancer

This is assuming you're using Apache

This is a very complicated topic and there are many possible solutions, each one would have different requirements.

First of all - is your site database driven? If yes, then do you want to load-balance database server too or just keep one backend database server and several front-end web servers?

The most simple solution is NOT doing this from apache or php, but from DNS server. User round robin dns to resolve the domain to different IP addresses, where different IP address is returned for a DNS query from the list of IP addresses in circles or randomly.

See if your dns server has this feature, if not, then loop for DNS service provider (google DNS server or DNS hosting) that offers this.

Interesting way to “simulate” load balancing, we’ve all seen this in progress were we first hit www.testdomain.com and then all of a sudden the url becomes www1.testdomain.com

PHP one liner for basic redirection, make sure to stay away from 301 as I am guessing you don’t want permanent redirection.
------------------------------
redirect('http://www.domain.com', 302);
------------------------------

Drawbacks to this approach:

1. You still have to run PHP on your server to get them off, so your cost is CPU, Memory, etc. and it’s tightly coupled to your application.
2. Primary Server’s PHP engine dies so does the load balancer
3. You now have people book marking a mirror or secondary site, which you won’t be able to take down for maintenance
4. Very costly if you provide HTTPS, SSL negotiations aren’t cheap

Benefits
1. Free
2. You’re redirection logic can be very very sophisticated

A better approach is DNS load balancing, especially if your site is stateless and its basically free.

The best approach is a dedicated decoupled load balancer that works at L4 and L7. If you shop around you’ll be amazed at how cheap a quality purpose built dedicated hardware load balancing appliance really is, just make sure to find the best feature/cost ratio, and keep a eye out for HTTP L7 features like cache/compression/security.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.