Setting up a load balancer using Apache 2.4

6,667

Here's a complete balancer config that should work:

<Proxy balancer://pop>
    BalancerMember http://pop1.local/ loadfactor=1
    BalancerMember http://pop2.local/ loadfactor=1
    ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / balancer://pop/

As stated in documentation, if the first part of ProxyPass ends with a / the second part also needs to end with the /:

If the first argument ends with a trailing /, the second argument should also end with a trailing / and vice versa. Otherwise the resulting requests to the backend may miss some needed slashes and do not deliver the expected results.

You also need to make sure that ProxyRequests is set to off.

Share:
6,667

Related videos on Youtube

Mehran
Author by

Mehran

Updated on September 18, 2022

Comments

  • Mehran
    Mehran over 1 year

    I'm trying to setup a load balancer with Apache 2.4 but I can't make it work.

    Here's what I wrote in my http.conf file:

    <Proxy balancer://pop>
        BalancerMember http://pop1.local/ loadfactor=1
        BalancerMember http://pop2.local/ loadfactor=1
        ProxySet lbmethod=byrequests
    </Proxy>
    

    And this is the list of loaded Apache modules:

    core mod_so http_core event mod_authn_file mod_authn_core mod_authz_host mod_authz_groupfile mod_authz_user mod_authz_core mod_access_compat mod_auth_basic mod_watchdog mod_reqtimeout mod_filter mod_mime mod_log_config mod_env mod_headers mod_setenvif mod_version mod_proxy mod_proxy_connect mod_proxy_ftp mod_proxy_http mod_proxy_fcgi mod_proxy_scgi mod_proxy_wstunnel mod_proxy_ajp mod_proxy_balancer mod_proxy_express mod_slotmem_shm mod_lbmethod_byrequests mod_lbmethod_bytraffic mod_lbmethod_bybusyness mod_lbmethod_heartbeat mod_unixd mod_heartmonitor mod_status mod_autoindex mod_dir mod_alias mod_rewrite mod_php5

    To test, I've setup three VMs, one for load balancer and two for back-end webservers. Using hosts file I introduced three domains in all of the machines:

    192.168.0.100   pop.local
    192.168.0.101   pop1.local
    192.168.0.102   pop2.local
    

    pop.local is my load balancer and other two are the back-ends. Having a info.php file created on each of the back-ends, I can load them entering http://pop1.local/info.php and http://pop2.local. But when I type http://pop.local, a 404 File not found error returns.

    The Apache on pop.local loads normally as if there's no Proxy balancer is set. It can even serve its own local files without a problem.

    What's the problem? Am I missing something?

    [UPDATE]

    Here's the contents of my log file:

    error_log:

    [Wed Oct 02 02:40:55.530051 2013] [lbmethod_heartbeat:notice] [pid 2179:tid 140142625933120] AH02282: No slotmem from mod_heartmonitor
    [Wed Oct 02 02:40:55.541737 2013] [mpm_event:notice] [pid 2179:tid 140142625933120] AH00489: Apache/2.4.6 (Unix) PHP/5.5.1 configured -- resuming normal operations
    [Wed Oct 02 02:40:55.541768 2013] [core:notice] [pid 2179:tid 140142625933120] AH00094: Command line: '/usr/local/apache2/bin/httpd'
    [Wed Oct 02 02:42:04.170782 2013] [:error] [pid 2615:tid 140142199158528] [client 192.168.0.81:55732] script '/usr/local/apache2/htdocs/info.php' not found or unable to stat
    
    • Jenny D
      Jenny D over 10 years
      What do your logs say?
    • Mehran
      Mehran over 10 years
      I'll update the question.
    • Mehran
      Mehran over 10 years
      What's up with the negative vote!?
    • Jenny D
      Jenny D over 10 years
      Which of the servers was the logfile from?
    • Mehran
      Mehran over 10 years
      The log content belongs to the balancer. I've made sure that the request does not arrive at the back-ends by removing access_log and error_log files in them and sending a new request. No log file was created for the new request.
    • Jenny D
      Jenny D over 10 years
      I'm missing the ProxyPass line in your config.
    • Mehran
      Mehran over 10 years
      This is my first time setting up a load balancer. Is it OK to add ProxyPass / balander://pop?
    • Mehran
      Mehran over 10 years
      Having the ProxyPass / balander://pop added, now error_log says: No protocol handler was valid for the URL /info.php. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule. with a 500 Internal server error on browser.
    • user128296
      user128296 over 10 years
      You should try haproxy for load balancing it's easy to configure and have great functionality.
    • Jenny D
      Jenny D over 10 years
      Unless balander is a typo this may be your problem (in that it should be balancer :-)
    • Mehran
      Mehran over 10 years
      The issue you mentioned was real but even with ProxyPass / balancer://pop I still get If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule. error. Any more suggestions?
    • Mehran
      Mehran over 10 years
      Is mod_proxy_html needed? Because I don't have that and since I've compiled the Apache myself, it's not that easy to compile a new module. I just hope the lack of this module is not the case!
    • Mehran
      Mehran over 10 years
      I fixed it, it should have been ProxyPass / balancer://pop/ with the slash at the end!!
  • Mehran
    Mehran over 10 years
    Just don't forget the / at the end of ProxyPass. Thanks again.