Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a Ubuntu 12.04.3 server set up acting as DNS server (BIND9), web server (Apache2) and a reverse proxy server (haproxy). My goal is to have haproxy redirect to some of the other servers on the network, some of which I want to redirect to a 'sub URL' (Not sure that's the right term. Please see the example below)

user goes to "monitor.example.com" > haproxy redirects to > "1.1.1.1:80/nagios"

My DNS server is set up using wildcard subdomains, and right now if I go to monitor.example.com it goes to the default apache page on the monitor server, but it would look a lot nicer and be a lot shorter to type if I could get it to direct to the /nagios page automatically. If I go to monitor.example.com/nagios it works as expected, but it's a little redundant.

I spent a while searching for a solution, but I'm not having any luck finding the answer to my question. Does anyone have any idea if this is possible and if so how I might solve it? Here is my haproxy.conf file:

global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    user haproxy
    group haproxy
    daemon
    #debug
    #quiet

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    maxconn 2000
    contimeout      5000
    clitimeout      50000
    srvtimeout      50000

frontend http-in
    bind 1:80

    acl host_apache hdr(host) -i example.com
    acl host_monitor hdr(host) -i monitor.example.com
    acl host_cloud hdr(host) -i cloud.example.com

    use_backend apache if host_apache
    use_backend monitor if host_monitor
    use_backend cloud if host_cloud


backend apache
    server web3 127.0.0.1:81

backend monitor
    server monitor 1.1.1.1:80/monitor

backend cloud
    server cloud 2.2.2.2:80

If anyone has any other suggestions or a different program I could use to achieve my goal, I'm open to suggestions. I'm not using haproxy for any particular reason. I've tried pound too, but could not get it configured either.

Thanks for reading!

Brigzzy

share|improve this question
    
I've not played with haproxy for a long time (if ever), but why not simply use Apache + modproxy to do the reverse proxying you are looking to do ? –  davidgo Jan 31 at 7:01
    
I've tried it once or twice before but could not get it working. Could you recommend a guide, or a configuration example? –  Brigzzy Feb 1 at 6:11

1 Answer 1

While I love HAProxy, I'd agree with davidgo that it's probably not the best tool for this job. I also think that Apache with multiple named vhosts and mod_rewrite to proxy requests to the necessary backend are the way to go.

Listen 80
NameVirtualHost *:80

<VirtualHost *:80>
  ServerName example.com
  RewriteEngine on
  ProxyPreserveHost On
  RewriteRule ^/(.*)$ http://127.0.0.1:81/$1 [P]
</VirtualHost>

<VirtualHost *:80>
  ServerName monitor.example.com
  RewriteEngine on
  ProxyPreserveHost On
  RewriteRule ^/(.*)$ http://1.1.1.1:80/monitor/$1 [P]
</VirtualHost>

Depending on your applications on the backend, the rewrites may or may not work exactly like you are expecting. For example, if nagios is expecting that it's resources are located under a root dir of /monitor, and it links to things like /monitor/someurl, then your rewriting will cause the URL to end up at /monitor/monitor/someurl by the time it hits your server. That could probably be overcome by another RewriteRule, or a little bit of regex in the current one.

You could also just use straight mod_proxy with ProxyPass directives instead of mod_rewrite (which, in this case, is also utilizing mod_proxy)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.