我有REST API。
为简单起见,我想说有两项服务:
如何将所有读取请求(GET方法)重写为 read.request.com 以及所有写入请求(POST,PUT,使用HAProxy将 write.request.com 删除方法?
答案 0 :(得分:8)
不完全确定哪些适用于您的情况,但希望有一个。
一个后端
我认为这是你的情况。
frontend http-in
bind *:80
acl is_post method POST
acl is_get method GET
http-request set-header Host write.request.com if is_post
http-request set-header Host read.request.com if is_get
default_backend api
backend api
server one localhost:8080 check
所有这一切都是检查正在使用的方法,并在将请求传递给Host
之前相应地设置localhost:8080
标头。
两个后端
在这个设置中,你只有一个代码实例只运行读取请求而另一个实例只运行写请求。在这种情况下,读取代码在localhost:8080
上运行,写入代码在localhost:8081
上运行。
frontend http-in
bind *:80
acl is_post method POST
acl is_get method GET
use_backend write if is_post
use_backend read if is_get
backend write
http-request set-header Host write.request.com #optional
server write_one localhost:8081 check
backend read
http-request set-header Host read.request.com #optional
server read_one localhost:8080 check
此选项通过检查正在使用的方法开始与前一个选项相同,但不是使用一个HAProxy后端,而是分成两个。每个后端内的http-request
行对于此配置是可选的。
希望有所帮助。