大网数据广告活动
服务器运维问答

haproxy - Route Request and keep same base URL

· 52CE 我爱测

I'm new to haproxy and I'm struggling a little bit trying to configure it. What I want: Type in the browser: proxy.company.com/Test/App Have haproxy balance the request between webapp01.company.com/Test/App and webapp02.company.c…

问题原文

原帖链接 · ornstai · CC BY-SA 3.0

I'm new to haproxy and I'm struggling a little bit trying to configure it.

What I want:

  1. Type in the browser: proxy.company.com/Test/App
  2. Have haproxy balance the request between webapp01.company.com/Test/App and webapp02.company.com/Test/App
  3. Keep the base URL proxy.company.com/Test/App

What I have so far: I have two websites setup:

  1. webapp01.company.com/Test/App
  2. webapp02.company.com/Test/App

Both work and when I browse to each individually I get a screen with the name of the server that served the request (WebApp01 or WebApp02).

However, when I go to proxy.company.com/Test/App I get a 404 error. I expect the request to be routed to webapp01 or webapp02 and I expect to see the name of the server serving the request.

Here's my current configuration:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend proxy.company.com
   bind :80
acl test_app path_end -i /Test/App

use_backend srvs_test    if test_app

backend srvs_test
   balance roundrobin
   server webapp01 webapp01.company.com:80 check
   server webapp02 webapp02.company.com:80 check

If I change the backend configuration to this:

backend srvs_test
   balance roundrobin
   redirect location http://webapp01.company.com/Test/App

It works, however, if I type proxy.company.com/Test/App I'm redirected to webapp01.company.com/Test/App. The base URL is changed and I want it to remain proxy.company.com/Test/App

Is it possible? How chan I change the configuration to allow what I want?

已采纳解答

原帖链接 · DavidGamba · CC BY-SA 3.0

A reverse proxy will by default rewrite the Location header so there is no need for to to do that.

Use path_beg instead of path_end.

I think your config is too minimal. What happens when you hit proxy.company.com? You are missing a default_backend.

Also, the whole idea of the proxy is that you don't have to expose webapp01 and 02 to the world. No need to give them external IPs as your example shows with those DNS entries. Just use the internal ones reachable from your proxy.

Try something like:

frontend proxy.company.com *:80
  acl test_app path_beg -i /Test/App
  use_backend srvs_test if test_app
  default_backend default

backend srvs_test
   balance roundrobin
   server webapp01 webapp01.company.com:80 check
   server webapp02 webapp02.company.com:80 check

backend default
   server localhost:80 check

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。