HAProxy:與前端位於同一子網域上的 API

HAProxy:與前端位於同一子網域上的 API

我的文件中有這樣的配置haproxy.cfg

    acl is_api url_beg -i /api
    acl is_frontend hdr(host) -i subdomain.mydomain.com

    use_backend api if is_api
    use_backend nginx-frontend if is_frontend

我的目標:

除非以( ) 開頭,否則它應該使用我的subdomain.mydomain.com後端。nginxpath/apisubdomain.mydomain.com/api/*api

現在,使用目前配置,當我重新啟動haproxy服務時,有兩種情況:

  1. 我去subdomain.mydomain.com——服務很好nginx-frontend——很棒。然後我轉到subdomain.mydomain.com/api- 404(nginx已使用)。

  2. 我去吃subdomain.mydomain.com/api它的服務api——太棒了。然後我轉到subdomain.mydomain.com-404(api使用了後端)。

因此,根據我第一次訪問任一頁面的順序,對我的子網域的所有請求都分配給任一後端...

我是一個新手,如果這是一個愚蠢的問題,我很抱歉。

有什麼幫助嗎?

答案1

好吧,我找到了一個方法...但是在nginx配置中(/etc/nginx/sites-enabled/default):

server {
    listen 8080 default_server;
    listen [::]:8080 default_server ipv6only=on;

    server_name subdomain.mydomain.com;
    location /api {
            proxy_pass http://127.0.0.1:4001;
    }

    location / {
            try_files $uri $uri/ =404;
    }
}

並在haproxy.cfg文件中:

backend nginx
    server nginx-1 127.0.0.1:8080 check

frontend all
    bind *:80
    mode http

    acl is_frontend hdr(host) -i subdomain.mydomain.com
    use_backend nginx if is_frontend

    # other frontends...

如果有人可以評論這是否是一個好方法,我將不勝感激!

答案2

它給出 404 因為url_beg.這使用完整的 URL,而不是網域後的尾隨部分,即它"http..."進入http://subdomain.mydomain.com/api

網址請求 當 URL 以字串之一開頭時傳回 true。這可用於檢查 URL 是否以斜線或協定方案開頭。

代替使用path_beg

frontend fe
  ...
  acl is_api path_beg -i /api
  acl is_frontend hdr(host) -i subdomain.mydomain.com

  use_backend api if is_api
  use_backend nginx-frontend if is_frontend !is_api

相關內容