by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

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.

Tags: fastapi haproxy

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


ssl,haproxy,certbot

Perfect SSL certificate automatic update environment (HAProxy plus certbot)

HAProxy's reloading speed is very fast, and I don't feel that there is a restart. It is really convenient for all credentials to be handled by HAProxy. Host environment requirements, please confirm that you have the following two instructions (How to install Ubuntu? apt-get install -y haproxy cerbot, I guess, if not, please Google)

laravel,letsencrypt,haproxy

PHPENV's HAProxy environment is set up with free SSL certificate application

PHPENV has added the support of haproxy.yml. If you have an external IP and domain name, it should be quite simple to get HAProxy and certificate application through deviny/phpenv. In this article, let us see how to use HAProxy in the HAProxy environment setting in PHPENV.

fastapi,phpenv,docker

How do I create a fastapi runtime environment with phpenv

phpenv php laravel runs HAProxy, and automatically applies for and updates free certification, now let him run Python's fastapi. There is no limit to phpenv, the limit is your idea.😆