V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
auzeonfung
V2EX  ›  程序员

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

  •  
  •   auzeonfung · 2015-10-25 00:35:38 +08:00 · 1732 次点击
    这是一个创建于 3120 天前的主题,其中的信息可能已经有所发展或是发生改变。

    這幾天被 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
    

    先謝謝各位了!

    第 1 条附言  ·  2015-10-26 12:53:51 +08:00
    問題解決了:
    HAProxy 是自上而下匹配規則的,把 example.com 的那幾行規則放到最後面就可以了((
    2 条回复    2015-10-25 10:52:20 +08:00
    fangpeishi
        1
    fangpeishi  
       2015-10-25 06:55:16 +08:00
    用 Nginx 不是更方便吗?
    znoodl
        2
    znoodl  
       2015-10-25 10:52:20 +08:00   ❤️ 1
    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
    这样不行吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1022 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:04 · PVG 06:04 · LAX 15:04 · JFK 18:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.