title: Using HAProxy to route based on domain path
body: <p>I need to load balance requests based on the requested URI. E.g.:</p>
  
  <ul>
  <li>requests to <a href="http://website.com/" rel="nofollow noreferrer">http://website.com/</a>*   should go to web-server1, 2 and 3.</li>
  <li>requests to <a href="http://website.com/api" rel="nofollow noreferrer">http://website.com/api</a> should go to api-server1, 2 and 3.</li>
  </ul>
  
  <p>Currently no matter the path/URI all requests go to web-server1-3. This is how it is setup in all my 3 haproxy hosts:</p>
  
  <pre><code>frontend fe
      default_backend web-servers
  backend web-servers
      balance leastconn
      server web-server-1 1.1.1.1:80 check weight 1
      server web-server-2 1.1.1.2:80 check weight 1
      server web-server-3 1.1.1.3:80 check weight 1
  </code></pre>
  
  <p>Both web and api services are running in the same host (i.e., web-server-1 to 3), with JBoss. Recently, I decided to split the web and api services so I could load balance according to the URI, as I mentioned in the begining.
  So, now I have a total of 6 servers:</p>
  
  <ul>
  <li>web-server-1 to 3 (1.1.1.1-3:80)</li>
  <li>api-server-1 to 3 (1.1.1.4-6:8088)</li>
  </ul>
  
  <p>To do this I came up with 2 different options:</p>
  
  <p>1) add 3 nginx hosts. The haproxy configuration would look like this:</p>
  
  <pre><code>backend nginx-servers
      balance leastconn
      server nginx-1 1.1.1.7:80 check weight 1
      server nginx-2 1.1.1.8:80 check weight 1
      server nginx-3 1.1.1.9:80 check weight 1
  </code></pre>
  
  <p>And now each nginx host routes based on the URI, such as:</p>
  
  <pre><code>upstream web-servers {
      server 1.1.1.1:80;
      server 1.1.1.2:80;
      server 1.1.1.3:80;
  }
  upstream api-servers {
      server 1.1.1.4:8088;
      server 1.1.1.5:8088;
      server 1.1.1.6:8088;
  }
  server {
      location ~ "/" {
          proxy_pass http://web-servers;
          proxy_set_header Host $host;
      }
      location ~ "/api" {
          proxy_pass http://api-servers;
      }
  }
  </code></pre>
  
  <p>2) the alternative using only haproxy would be:</p>
  
  <pre><code>frontend fe
      acl website_domain req.hdr(host) -i website.com
      acl route_api path -i -m beg /api
      use_backend api-servers if route_api
      use_backend web-servers if website_domain !route_api
  backend web-servers
      balance leastconn
      server web-server-1 1.1.1.1:80 check weight 1
      server web-server-2 1.1.1.2:80 check weight 1
      server web-server-3 1.1.1.3:80 check weight 1
  backend api-servers
      balance leastconn
      server api-server-1 1.1.1.4:8088 check weight 1
      server api-server-2 1.1.1.5:8088 check weight 1
      server api-server-3 1.1.1.6:8088 check weight 1
  </code></pre>
  
  <p>However, with this second option when I access <a href="http://website.com/" rel="nofollow noreferrer">http://website.com/</a> all my api requests return http/404. How is this second approach different from the first one (that actually works)?</p>