公司的网站有个需求,主站点上有两个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. 【Zoj 4061】Magic Multiplication

    [链接] 我是链接,点我呀:) [题意] [题解] /* for a[1] from 1~9 1*1=1 2*1=2 3*1=3 1*2=2 2*2=4 3*2=6 1*3=3 2*3=6 3*3=9 ...

  2. Syncfusion在WinPhone8.1实现统计图

    using Syncfusion.UI.Xaml.Charts; public static SfChart InitCompareChart(string fundName, double tenT ...

  3. hdu 4009 最小树形图模板题朱刘算法

    #include<stdio.h> /*思路:显然对于每个地方, 只有一种供水方式就足够了,这样也能保证花费最小, 而每个地方都可以自己挖井,所以是不可能出现无解的情况的, 为了方便思考, ...

  4. 【bzoj1025】【SCOI2009】【游戏】【dp】

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之相应.最開始windy把数字按顺序1,2.3.--,N写一排在纸上. 然后再在这一排以下写上它们 ...

  5. [Angular] Upgrading to RxJS v6

    This is just a learning blog post, check out the talk. 1. Custom pipeable operators: Custom pipeable ...

  6. 我想要得那块牌—记烟台大学第一届&quot;ACM讲堂&quot;

    2014年5月23日.烟台大学ACM实验室举办了第一届"ACM讲堂",演讲的主题是"我想要得那块牌",大二和大三的參赛队员以及三位指导老师都进行了演讲. 晚上七 ...

  7. 什么是鸭子类型(duck typing)

    "当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...

  8. Huffman编码实现压缩解压缩

    这是我们的课程中布置的作业.找一些资料将作业完毕,顺便将其写到博客,以后看起来也方便. 原理介绍 什么是Huffman压缩 Huffman( 哈夫曼 ) 算法在上世纪五十年代初提出来了,它是一种无损压 ...

  9. DNS通道检测 国内学术界研究情况——研究方法:基于特征或者流量,使用机器学习决策树分类算法居多

    http://xuewen.cnki.net/DownloadArticle.aspx?filename=BMKJ201104017&dbtype=CJFD<浅析基于DNS协议的隐蔽通道 ...

  10. hdu 1002(大数)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...