如何在 HAProxy 中重写 URL?

问题描述 投票:0回答:1

使用的HAProxy版本:v2.6.6

我想要实现的是,当用户访问 http://haproxyserver.domain.com/application1 时,HAProxy 会在 https://appserver1.otherdomain.com/ 获取页面并将其发送回http://haproxyserver.domain.com/application1

所以,我想将前面的“/application1”重写为后面的“/”,并将响应发送回“/application1”。 例如。后端的图像如 https://appserver1.otherdomain.com/images/test.jpg 应以 http://haproxyserver.domain.com/application1/images/test.jpg 的形式发回,依此类推.

我的 haproxy.cfg 文件中没有任何重写配置:

#---------------------------------------------------------------------
# PRD http frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  PRD-http-front
bind \*:80
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http

    mode http
    option httplog
    option forwardfor       except 127.0.0.0/8
    maxconn 10240
    
    # define acl's
    acl host_haproxy-stats hdr(host) -i haproxy-stats.domain.com
    
    # decide which backend to use
    # HAProxy statistics page
    use_backend haproxy-stats if host_haproxy-stats
    
    # Other
    use_backend application1 if { path_beg /application1 }
    
    # default backend
    default_backend PRD-local-httpd

#---------------------------------------------------------------------
# haproxy-stats backend
#---------------------------------------------------------------------
backend haproxy-stats
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server haproxy-stats 127.0.0.1:8888 check

#---------------------------------------------------------------------
# PRD-local-httpd backend
#---------------------------------------------------------------------
backend PRD-local-httpd
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server HAProxy-Server haproxyserver.domain.com:8081 check

#---------------------------------------------------------------------
# application1 backend
#---------------------------------------------------------------------
backend application1
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server appserver1 appserver1.otherdomain.com:443 check ssl verify none

此配置不起作用,因为 HAProxy 将尝试在后端加载 https://appserver1.otherdomain.com/application1,而该后端不存在。 我已经在后端配置部分尝试了几种重写设置,但没有成功。 一些例子:

http-request replace-path ^([^\ ])\ /application1/(.) \1\ /\2
http-request replace-path /application1(.*) / \1

我什至在前端尝试过这个:

acl app1 path_beg -i /application1/
http-request set-path  /%[path] if app1

我尝试了很多东西,但我无法弄清楚。 非常感谢每一个帮助!

url-rewriting haproxy
1个回答
0
投票

在您的

backend application1
中添加主机重写:
http-request set-header host appserver1.otherdomain.com
和路径重写,就像您在问题中所做的那样。我不确定您是否需要重写任何内容作为响应,但如果您这样做,那么它将与请求中的相同,只需使用
http-response
代替即可。

© www.soinside.com 2019 - 2024. All rights reserved.