公司的网站有个需求,主站点上有两个URL,没有在本地nginx上配置,而是在另一台主机的nginx上配置的站点。如果使用nginx作为反向代理,可以使用proxy_pass指令转发对这两个URL的请求到另一台主机。

    那么在haproxy作为反向代理的情况下,该如何配置呢?下边来说一下。
 

1. nginx的主机上为了安全,关闭了空主机头
server {
       listen 80 default;
       return 500;
}
 
2.haproxy的配置
frontend main
    bind *:80
    acl web hdr(host) -i www.abc.com
    acl webapp path_beg -i /investApp/ln/
    acl investapp path_beg -i /investApp/
    use_backend webapp_bk if web webapp
    use_backend investapp_bk if web investapp
    use_backend webserver if web
    default_backend webserver
 
backend webserver
    mode http
    balance roundrobin
    server nginx01 192.168.27.131:80
 
backend webapp_bk
    http-request set-header Host img.abc.com
    reqirep ^([^\ :]*)\ /investApp/ln/(.*)  \1\ /webApp/ln/\2
    server nginx02 192.168.27.132:80
 
backend investapp_bk
    http-request set-header Host img.abc.com
    server nginx02 192.168.27.132:80
这里的重点配置有几处:
 
    acl web hdr(host) -i www.abc.com
    acl webapp path_beg -i /investApp/ln/
    acl investapp path_beg -i /investApp/
 
这里声明了3条acl
acl web匹配 www.abc.com主机头
acl webapp匹配路径: /investApp/ln/
acl investapp匹配路径:  /investApp/
可以看到,investapp匹配的路径包含了webapp匹配的路径。
 
因为haproxy的acl是按照顺序执行,第一个acl匹配到以后就不再向下遍历,所以我们必须把acl webapp放到acl investapp之前执行,否则acl webapp永远也不会被执行到。即如下配置:
 
    use_backend webapp_bk if web webapp
    use_backend investapp_bk if web investapp
    use_backend webserver if web
 
因为严格限制了主机头,所以转发到nginx02上的request必须使用正确的img.abc.com的主机头,另外还需要做路径替换。
backend webapp_bk
    http-request set-header Host img.abc.com
    reqirep ^([^\ :]*)\ /investApp/ln/(.*)  \1\ /webApp/ln/\2
 
reqirep用来匹配HTTP请求,“GET /investApp/ln/ HTTP/1.1 ”,然后将前后两部分保存到变量中,在后边引用。
 
参考文档:
https://blog.haproxy.com/2014/04/28/howto-write-apache-proxypass-rules-in-haproxy/
http://thread.gmane.org/gmane.comp.web.haproxy/4598
http://serverfault.com/questions/647479/haproxy-use-backend-match-order
http://stackoverflow.com/questions/22219479/haproxy-backend-with-subdirectory-subpath-subfolder
http://stackoverflow.com/questions/30256571/haproxy-path-to-host-path
https://linux-tips.com/t/routing-urls-to-different-backends-in-haproxy/24
https://www.digitalocean.com/community/tutorials/how-to-use-haproxy-as-a-layer-7-load-balancer-for-wordpress-and-nginx-on-ubuntu-14-04
https://www.claudiokuenzler.com/blog/554/haproxy-forward-based-on-string-in-url-combine-existing-acl
http://blog.defsdoor.org/a-note-on-haproxys-acl-matching/

用haproxy实现nginx的proxy_pass转发功能的更多相关文章

  1. Nginx配置proxy_pass转发的/路径问题

    Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...

  2. Nginx配置proxy_pass转发/路径问题

    proxy_ignore_client_abort on; #不允许代理端主动关闭连接 upstream的负载均衡,四种调度算法 #调度算法1:轮询.每个请求按时间顺序逐一分配到不同的后端服务器,如果 ...

  3. nginx的四层转发功能

    架构图 配置过程 配置web服务器 # 1.配置web01,更改配置文件 [root@web01 /etc/nginx/conf.d]# vi test1.conf server { listen 8 ...

  4. Nginx配置proxy_pass转发的/路径

    请求原地址 :http://servername/static_js/test.html location ^~ /static_js/ { proxy_cache js_cache; proxy_s ...

  5. 烂泥:haproxy与nginx、zabbix集成

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx.za ...

  6. nginx实现请求转发

    反向代理适用于很多场合,负载均衡是最普遍的用法. nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理. nginx 反向代理官方文档: NGINX REVERSE PROXY 当在 ...

  7. CDN调度器HAProxy、Nginx、Varnish

    http://www.ttlsa.com/web/the-cdn-scheduler-nginx-haproxy-varnish/ CDN功能如下:1.将全网IP分为若干个IP段组,分组的依据通常是运 ...

  8. Nginx支持Socket转发过程详解

    序言 一网友在群中问,nginx支持socket转发吗? 实话说,我没做过socket转发,但是我知道socket跟http一样都是通过tcp或者udp通信的,我猜测啦一下nginx应该支持吧,然后又 ...

  9. nginx的反向代理功能和缓存功能

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

随机推荐

  1. HRBUST 1214 方格取数

    方格取数 Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HRBUST. Original ID: 12 ...

  2. mongodb--group聚合运算

    mongodb本质就是要做一个高性能,能简单则简单,不要把mongodb中的运算做的太复杂 count 最简单的一个聚合方法 distinct 选择结果中剔除重复的一个键值, 跟sql语句的效果是一样 ...

  3. [bzoj2529][Poi2011]Sticks_贪心

    Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n ...

  4. HDU 4502

    直接贪心就好. #include <iostream> #include <cstdio> #include <algorithm> #include <cs ...

  5. 万众创业,互联网+,WTO

    WTO的保护期,发展的非常繁荣.但大部分的资源都配置在了房地产这个支柱产业, 而被保护的行业小日子过得不错, 研发再投入?那是傻子才做的事情,别墅.豪车.美女.这才是生活. 但突然有一天,发现保护期要 ...

  6. Lua 与C/C++ 交互系列:注冊枚举enum到Lua Code中

    在Lua Code中注冊C/C++的枚举很easy,就像注冊全局变量一样.我们使用枚举名称作为命名空间,来避免注冊的枚举发生冲突.注冊的枚举存储在全局环境(线程环境)中. 当在Lua Code中訪问枚 ...

  7. 【cl】本地安装maven的jar包报错Artifact is already in the local repository

    原因是我直接把jar包放在了仓库里面 解决办法:将jar办放在不是仓库路径位置,在进行install就okle

  8. 【待解决】使用JUnit时报错java.lang

    编写selenium自动化代码,代码逻辑应该都没有错,运行时老是报如下错误 java.lang.NoClassDefFoundError: org/json/JSONException 解决办法:导入 ...

  9. C#调用C++回调函数的问题

    C++的回调函数中有一个参数是,是返回一个字符串,原则如下: typedef   void   (*TDataEvent)(char   *AData   ,int   ALen); 其中char   ...

  10. python实现高速排序算法(两种不同实现方式)

    # -*- coding: utf-8 -*- """ Created on Fri May 16 17:24:05 2014 @author: lifeix " ...