HAProxy 如何將某二集目錄下的所有請求轉發都某一特定後端?

2015-10-25 00:35:38 +08:00
 auzeonfung

這幾天被 HAProxy 弄得有點糾結⋯⋯

需求是這樣的:
有幾個域名:
- example.com
- img.example.com
- api.example.com

由於歷史遺留問題,現在需要將 *.example.com/data/下的所有請求 指向後端一台服務器,地址為 http://192.168.254.2:8080

比如 訪問 http://api.example.com/data/img/000.png
實際上是請求 http://192.168.254.2:8080/data/img/000.png

上網找了一下相關的文章,寫了下面的配置文件,但是規則並沒有生效??
(體現為訪問 img.example.com/data/img/000.png 正常,但是 example.com/data/img/000.png 是出錯的)

下面是現在的配置文件:

global
    daemon
    maxconn 65535
    pidfile /usr/local/haproxy/haproxy.pid
defaults
    mode http
    option httpclose
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend All
    bind :80
    acl web_image_rw path_beg -i /data
    use_backend img_s2 if web_image_rw

    acl img hdr_beg(host) -i img.example.com  
    use_backend img_s2 if img

    acl example_1 hdr_beg(host) -i example.com
    use_backend s1 if example_0

backend s1
    option httpclose
    option forwardfor
    server server1 192.168.254.1


backend img_s2
    option httpclose
    option forwardfor
    server server2_hdd_img 192.168.254.2:8080

先謝謝各位了!

1740 次点击
所在节点    程序员
2 条回复
fangpeishi
2015-10-25 06:55:16 +08:00
用 Nginx 不是更方便吗?
znoodl
2015-10-25 10:52:20 +08:00
acl example_1 hdr_beg(host) -i example.com
use_backend s1 if example_0
前面是 example_1 后面是 if example_0

还有
frontend All
bind :80
acl web_image_rw path_beg -i /data
use_backend img_s2 if web_image_rw

default_backend s1
这样不行吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/230825

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX