Apache, virtual hosts and refferers...

Discussion in 'Apache' started by DN667, Jan 7, 2007.

  1. #1
    Hi all,

    I'm setting up a small web company and recently aquired a server, which will be colocated in little time. For now, my websites are hosted at some shared server at some hosting providers. As I want to host those sites myself, on my own server, I can of cousre, put my sites there by changing the DNS records for my domains and let my Apache (with virtual hosts) handle it. That's fine and easy. But... if possible, I'd like to create some redundancy. So what I figured, is that it'd be nice to leave my websites on my (already paid for) hosters server and use some kind of forwarding to redirect all traffic to my own server (saving bandwith eh..), without making that obvious to visitors (so no meta-tag... for HTML redirect). That would enable me, if the server goes down, to redirect all traffic to the servers at the webhosting company, without changing the DNS.

    Can this be done? Does Apache support a construction like that?

    Thanks in advance, Robin
     
    DN667, Jan 7, 2007 IP
  2. dallas

    dallas Peon

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think it's almost impossible.

    You'll need to have identical content on both servers (both static and dynamic content should be synced frequently) and TTL (DNS caching time) should be set to almost 0 (to switch servers fast) + DNS server should be located somewhere esle (not on these 2 servers)

    my 2c :)
     
    dallas, Jan 8, 2007 IP
  3. sgugal

    sgugal Peon

    Messages:
    183
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can keep your main site on old server. Just use 301 Redirect for it to transfer old server .
     
    sgugal, Jan 8, 2007 IP
  4. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes you can definately do this. I had basically the same setup you are describing for my stuff. I had 1 really fast server with a dedicated IP address, and then I had 20 shared hosting sites.

    Using rewrites proxy capability to redirect all images to your other server
    RewriteEngine On
    RewriteBase /
    RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
    Code (markup):
    That is definately the best method, but some hosts dont allow the P flag.


    Transparent Internal Rewriting
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/(.*) http://www.new-fast-server.com/$1 [L,QSA]
    Code (markup):
    That works for me but may not for you. You may have to use a 301 redirect, but I would suggest using a 302 redirect so that you can change the URL in case of a problem and your clients won't have a problem with that.

    302 Redirect
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/(.*) http://www.new-fast-server.com/$1 [R,L]
    Code (markup):

    Implement a Dynamic Mirror of another server

    Description:
    Assume there are nice webpages on remote hosts we want to bring into our namespace. For FTP servers we would use the mirror program which actually maintains an explicit up-to-date copy of the remote data on the local machine. For a webserver we could use the program webcopy which acts similar via HTTP. But both techniques have one major drawback: The local copy is always just as up-to-date as often we run the program. It would be much better if the mirror is not a static one we have to establish explicitly. Instead we want a dynamic mirror with data which gets updated automatically when there is need (updated data on the remote host).

    Solution:
    To provide this feature we map the remote webpage or even the complete remote webarea to our namespace by the use of the Proxy Throughput feature (flag [P]):

    RewriteEngine  On
    RewriteBase    /~quux/
    RewriteRule    ^hotsheet/(.*)$  http://www.askapache.com/hotsheet/$1  [P]
    Code (markup):
    RewriteEngine  On
    RewriteBase    /~quux/
    RewriteRule    ^usa-news\.html$   http://www.askapache.com/news/index.html  [P]
    Code (markup):



    Redirect Failing Requests to alternate server

    Description:
    A typical FAQ about URL rewriting is how to redirect failing requests on webserver A to webserver B. Usually this is done via ErrorDocument CGI-scripts in Perl, but there is also a mod_rewrite solution. But notice that this performs more poorly than using an ErrorDocument CGI-script!

    Solution:
    The first solution has the best performance but less flexibility, and is less error safe:

    RewriteEngine On
    RewriteCond   /your/docroot/%{REQUEST_FILENAME} !-f
    RewriteRule   ^(.+)  http://askapache.com/$1
    Code (markup):
    The problem here is that this will only work for pages inside the DocumentRoot. While you can add more Conditions (for instance to also handle homedirs, etc.) there is better variant:
    RewriteEngine On
    RewriteCond   %{REQUEST_URI} !-U
    RewriteRule   ^(.+) http://askapache.com/$1
    Code (markup):

    This uses the URL look-ahead feature of mod_rewrite. The result is that this will work for all types of URLs and is a safe way. But it does a performance impact on the webserver, because for every request there is one more internal subrequest. So, if your webserver runs on a powerful CPU, use this one. If it is a slow machine, use the first approach or better a ErrorDocument CGI-script.
     
    apachehtaccess, Jan 8, 2007 IP
  5. Thibaut

    Thibaut Well-Known Member

    Messages:
    886
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Thibaut, Jan 9, 2007 IP
  6. sgugal

    sgugal Peon

    Messages:
    183
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sgugal, Jan 9, 2007 IP
  7. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Load Balancing

    Description:
    Suppose we want to load balance the traffic to www.foo.com over www[0-5].foo.com (a total of 6 servers). How can this be done?

    Solution:
    There are a lot of possible solutions for this problem. We will discuss first a commonly known DNS-based variant and then the special one with mod_rewrite:

    1. DNS Round-Robin

    The simplest method for load-balancing is to use the DNS round-robin feature of BIND. Here you just configure www[0-9].foo.com as usual in your DNS with A(address) records, e.g.

              www0   IN  A       1.2.3.1
              www1   IN  A       1.2.3.2
              www2   IN  A       1.2.3.3
              www3   IN  A       1.2.3.4
              www4   IN  A       1.2.3.5
              www5   IN  A       1.2.3.6
    Code (markup):
    Then you additionally add the following entry:
    
          www    IN  CNAME   www0.foo.com.
                 IN  CNAME   www1.foo.com.
                 IN  CNAME   www2.foo.com.
                 IN  CNAME   www3.foo.com.
                 IN  CNAME   www4.foo.com.
                 IN  CNAME   www5.foo.com.
                 IN  CNAME   www6.foo.com.
    Code (markup):
    Notice that this seems wrong, but is actually an intended feature of BIND and can be used in this way. However, now when www.foo.com gets resolved, BIND gives out www0-www6 - but in a slightly permutated/rotated order every time. This way the clients are spread over the various servers. But notice that this not a perfect load balancing scheme, because DNS resolve information gets cached by the other nameservers on the net, so once a client has resolved www.foo.com to a particular wwwN.foo.com, all subsequent requests also go to this particular name wwwN.foo.com. But the final result is ok, because the total sum of the requests are really spread over the various webservers.
     
    apachehtaccess, Jan 9, 2007 IP