How to setup Apache Proxy for a small fleet of EC2 instances?

5,399

what you asked is a very common situation where in applications may not be running on Gateway and they may be running on some server in LAN but to the world it appears it is coming from requested_url.com

I run 4 websites with Apaches mod proxy and at least 15 different services on these 4 different websites depend upon them.I do not have an experience of Amazon EC2 but Amazon EC2 uses Xen as its backend. Let us say the main machine which faces internet and has a public IP be called dom0 now read in the following terminology

Dom0  with IP      192.168.0.100 (This will be our Server facing internet)
DomU1 with IP      192.168.0.11
DomU2 with IP      192.168.0.12
DomU3 with IP      192.168.0.13
DomU4 with IP      192.168.0.14

I am assuming you have a local dns on Dom0 (or some where in your network ) which knows the DomU IPs with names.(If not life will be difficult) then in apache2.conf ( httpd.conf on Fedora type systems)

NameVirtualHost *
    <VirtualHost *>
      ServerName mainserver
            DocumentRoot /var/www
      ProxyRequests Off
           <Proxy *>
     Order deny,allow
           Allow from all
     </Proxy>
         ProxyPass /domu1  http://192.168.0.11/
         ProxyPass /domu2  http://192.168.0.12/
         ProxyPass /domu3  http://192.168.0.13/
         ProxyPass /domu4  http://192.168.0.14/
         ProxyPassReverse /domu1  http://192.168.0.11/
         ProxyPassReverse /domu2  http://192.168.0.12/
         ProxyPassReverse /domu3  http://192.168.0.13/
         ProxyPassReverse /domu4  http://192.168.0.14/
    </VirtualHost>

This way is one way to achieve the things which you asked (how ever not very recommended) if you upgrade the server (i.e. Dom0) the update may over write apache2.conf or httpd.conf what ever be the case. So once you understand what the above entries mean (Check it for your requirements I have struggled a long time back with some thing similar you want hence I gave an easy solution but not recommended). Once you understand the above ProxyPass then create different Virtual Host configurations in sites-available directory if it is a Ubuntu/Debian system or in Red Hat based it will be still possible (you will have to search) I am showing you one so it helps you rest 4-5 you can make on your own
Call this /etc/apache2/sites-enabled/myinterna1.conf it looks as follows

<VirtualHost *:80 >

        ServerName myserver1.com
        ServerAdmin webmaster@localhost

        ProxyRequests off
        <Proxy *>
        Order deny,allow
        Allow from all
        </Proxy>
        ProxyPreserveHost On


        ProxyPass / http://192.168.1.3/
        ProxyPassReverse / http://192.168.1.3/

        ErrorLog /var/log/apache2/server1/server1_error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/server1/server1_access.log combined
        CustomLog /var/log/apache2/server1/server1-resp_log resp
        LogFormat "%a %{X-Forwarded-For}i  %h %D %t %f %p %>s  %U %v" resp

</VirtualHost>

The above virtualhost will be defined on Dom0 so that all requests are served from an internal server server1.com.The way it will work is some one on internet will type in their browser

 http://requested_url.com/domu1

Now this request reaches Dom0 and Apache Virtual Host on Dom0 checks the Proxy Configuration and finds that /domu1 is mapped to an internal server so it forwards that to the machine which actually has to serve.To the user on internet all this is hidden.

How ever one word of caution while doing so the Apache on Ubuntu system serves the virtual hosts in alphabetical name of vhost file (not server name I am referring to name of file like sorting.c the Apache virtual host file) (that is why there is a file 000-default) on Ubuntu systems so make sure that you have one host more than the number of websites you are to serve (which will serve as error page).This is a standard practise.

Share:
5,399

Related videos on Youtube

ks78
Author by

ks78

Updated on September 17, 2022

Comments

  • ks78
    ks78 over 1 year

    I've been setting up Amazon EC2 instances for an upcoming project which will involve hosting multiple websites on multiple web servers. They are all micro instances, running Ubuntu Server 64bit.

    Here's what I have so far:

    • Web Server -- Apache
    • Database Server -- MySQL
    • Development Server -- Apache & MySQL
    • File Server -- SVN & Bacula (backups are done to S3 buckets)

    Currently there's only one Web server, but I've made an image of it, so once the project starts I can launch as many instances as I need and configure them individually.

    Everything has been going smoothly, but I've hit some snags.

    My problem is I'd like to run multiple web server instances, but with Amazon's restriction to 5 Elastic IP address, I know that won't be enough. I was researching how to host websites on multiple web servers from a single IP address and ran across mod_proxy for Apache. I haven't tried it yet, but I think that's what I need. I'd just like someone to confirm that I'm on the right track.

    Has anyone used Apache with mod_proxy? Does it truly allow you to host multiple websites from multiple using a single IP address?

    Assuming I'm on the right track here, all I should need is one Elastic IP address pointing to an EC2 instance running Apache, is that right?

    • ks78
      ks78 about 13 years
      After more research and experimentation, it looks like I may not need to use an Apache proxy server after all. I'm going to award the correct answer to Bond, since his response was the most detailed and would probably be the most helpful if I was continuing on that path.