公司的网站有个需求,主站点上有两个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. Sperner定理及其证明

    额,最近看到了一个十分有趣的定理--Sperner定理.其实这个定理在OI中没什么用处,因此我都没把这篇文章放到我的OI标签里(不知道在MO中是否有用?)但是觉得它很有趣于是就过来写一下. 由于博主太 ...

  2. 【hihocoder 1329】 平衡树·Splay(set做法)

    [题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 因为一开始是空的树,所以; n其实就代表了树中的最多元素个数; 则最坏的情况 ...

  3. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  4. HDU - 1723 - Distribute Message

    先上题目: Distribute Message Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  5. POJ 3080 Blue Jeans (后缀数组)

    题目大意: 求出这些DNA序列中的最长且字典序最小的公共子串. 思路分析: 二分长度的答案,去height中扫描这个长度是否满足,一旦满足就立即输出.这样就能够保证字典序最小了. #include & ...

  6. Scrapy源代码分析-经常使用的爬虫类-CrawlSpider(三)

    CrawlSpider classscrapy.contrib.spiders.CrawlSpider 爬取一般站点经常使用的spider.其定义了一些规则(rule)来提供跟进link的方便的机制. ...

  7. Converter实现Date类型转换

    1.springmvc-config.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  8. 各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言)

    各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言) 总结 a.一个语言或者一个东西能火是和这种语言进入某一子行业的契机有关.也就是说这个语言有没有解决社会急需的问题. ...

  9. I NEED A OFFER!(hdoj--1203--01背包)

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. 大数据攻城狮之Linux基础------rpm软件管理

    rpm的英文名称为: Redhat package manager 常用的命令加组合: i 安装 rpm -ivh 软件包名 当然我们的rpm也可以支持多包同时操作 rpm -ivh 软件包1 软件包 ...