HAProxy can use the definition of acl to determine which backend the request should direct to. This article will introduce some settings of this site. Here, through the acl of path_beg, the sent request will be directed to other services instead of the backend specified by the domain name. .
acl www_url hdr_beg(host) -i www.ccc.tc ccc.tc cn.ccc.tc en.ccc.tc
acl fastapi_rembg path_beg -i www_url /api/rembg
The full name of acl is Access Control List, which allows us to determine which backend the front-end request should direct to through a series of access controls.
After the acl is the name of the condition. If the condition is related to url, I usually add _url. For example, I named it www_url above.
It is not difficult to see that hdr_beg(host) is used here, that is, this conditional rule will be applied at the beginning of the URL, so you can also simply type www cn en and other subdomain names.
I named the second rule fastapi_rembg. It is not difficult to see that path_beg is used here, that is, the beginning of the path matches /api/rembg. In addition, I use www_url after -i, which means that the required prename also matches.
The big one can be guessed by guessing, let us continue to read, the designation of backend
use_backend fastapi_cv2_server if fastapi_rembg
use_backend ccc_server if www_url
Here, it is obvious that after use_backend is the rule name of the backend host, my personal habit will be represented by _server.
In the first line, you should be able to guess that if the URL path starts with /api/rembg, the backend fastapi_cv2_server will be used.
If the URL is www.ccc.tc, cn.ccc.tc or en.ccc.tc, use the backend of ccc_server, which is very clear.
Finally, let's take a look at the definition of backend.
backend fastapi_cv2_server
mode http
balance roundrobin
http-request set-path "%[path,regsub(^/api/rembg/,/)]"
server cv2 192.168.99.130:8000 check cookie cv2
backend ccc_server
mode http
balance roundrobin
server ccc 192.168.99.130:1056 check cookie cc
Here, basically I am connected to the same host, of course it can be different hosts or multiple hosts.
I split the fastapi and nginx hosts through the port.
In the backend setting of fastapi_cv2_server, there is an additional interesting setting, set-path.
It should not be difficult to see from the grammar, that is, change the path /api/rembg of the original request to / and then hit the backend.
In this way, the path of the backend does not need to be adjusted additionally. For FastAPI, the setting of root_path needs to be added, so that the path of docs will be correct.
Root_path can be set through parameters when starting up, or it can be set in a program way, here is how to set it in the program
app = FastAPI(root_path="/api/rembg")
@app.get("/")
def DemoAPI(request: Request):
return {"message": "Running on Synology Nas phpenv Container", "root_path": request.scope.get("root_path")}
With the help of HAProxy's set-path, you can see that the get("/") here is only in the root directory, and there is no need to adjust the program to /api/rember/, you can click this URL to see
https://www.ccc.tc/api/rembg/
And when the path is not /api/rembg, the request will be sent to the nginx service of port 1056 instead of fastapi.
The function of HAProxy is really quite powerful. When you study it carefully, you will find that there is nothing in it.
This article is just to start with, and provide a small part of the function introduction and description.
No Comment
Post your comment