HAProxy-如果第一台机器返回404,则从第二台机器提供URL [英] HAProxy - Serving a URL from 2nd machine if the 1st returns a 404

查看:78
本文介绍了HAProxy-如果第一台机器返回404,则从第二台机器提供URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我的网站的一部分(某些URL路径)是从一个后端服务器提供的,而所有其他URL是从HAProxy中的另一个默认后端提供的.

I have a situation where I have part of a website (certain URL paths) being served from one backend server while all other URLs are being served from a different default backend in HAProxy.

现在,由于编写应用程序逻辑的方式不同,因此可以在两个物理服务器计算机中的任何一个上的同一路径上创建要在同一URL路径下提供服务的文件.我希望能够提供这些文件,而不管文件所在的计算机.因此,简而言之,如何将请求转发到一个后端,如果响应是404(文件在那里不存在),则将请求转发给另一后端?

Now, because of the way the application logic was written, the files to be served under the same URL path could be created at the same path on either of the two physical server machines. I would like to be able to serve these files regardless of which machine the file exists on. So in a nutshell, how can I forward the request to one backend and if the response is a 404 (the file does not exist there), forward the request to be served from the other backend?

对于HAProxy,我是一个完全菜鸟,因此将不胜感激.谢谢.

I am a complete noob to HAProxy so any help will be appreciated. Thanks.

我的 haproxy.conf 的相关部分:

<代码>前端前端0...acl de path_beg/path1acl de path_beg/path2ACL de path_beg/path3如果使用deend,请使用backend1default_backend bakend后端后端1...服务器server_name 127.0.0.1:8000后端后端2...服务器server_name 192.168.11.1:8000

根据文件所在的位置,这两台计算机都需要提供路径/path4 .

There is a path /path4 that needs to be served from both of these machines depending on where the file exists.

推荐答案

感谢@ Michael-sqlbot的提示和

Thanks to @Michael-sqlbot for the hint and this question on ServerFault for the outline of the answer. The setup I finally ended up using is as follows:

  • 我设置了一个单独的URL/_path4,该URL/_path4从一个后端提供,而原始URL/path4从另一个后端提供.
  • 从/path4的第一个后端收到404响应后,我重定向到URL/_path4

我的配置文件现在如下所示:

My configuration file now looks as follows:

<代码>前端前端0...acl de path_beg/path1acl de path_beg/path2ACL de path_beg/path3acl de path_beg/_path4use_backend backend2如果是default_backend backend1...后端后端1...http-request set-var(txn.path)路径服务器server_name 127.0.0.1:8000http响应重定向位置%[var(txn.path),regsub('^/path4','/_ path4')]代码303如果{状态404} {var(txn.path)-m乞求'/path4'}...后端后端2...服务器server_name 192.168.11.1:8000

正如@ Michael-sqlbot对链接问题的出色注释中所述,需要设置事务变量,因为在 http-response 阶段,缓冲区已释放,因此正常<前端使用的code> path 变量不可用.

As explained in @Michael-sqlbot's excellent comments to the linked question, the setting of a transaction variable is required because at the http-response stage, the buffer has been released and so the normal path variable used in the frontend is not available.

这篇关于HAProxy-如果第一台机器返回404,则从第二台机器提供URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆