1

I've got such a configuration in my haproxy.cfg file:

    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

My goal:

Hitting subdomain.mydomain.com will serve files using nginx unless the path starts with /api (subdomain.mydomain.com/api/*) - then it should use my api backend.

Now, using the current configuration, when I restart haproxy service, there are two scenarios:

  1. I go to subdomain.mydomain.com - it's serving nginx-frontend - great. Then I go to subdomain.mydomain.com/api - 404 (nginx was used).

  2. I go to subdomain.mydomain.com/api it's serving api - great. Then I go to subdomain.mydomain.com - 404 (the api backend was used).

So depending on the order of my first visit on either page, all the requests to my subdomain are assigned to either backend...

I'm a total newbie, I'm sorry if that's a stupid question.

Any help?

0

OK, I figured out a way... but in nginx configuration (/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;
    }
}

and in haproxy.cfg file:

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...

If somebody could comment on if it's a good approach, I'd be grateful!

| improve this answer | |
0

It gives 404 because of url_beg. This uses the full URL and not the trailing part after the domain i.e. it gets "http..." in http://subdomain.mydomain.com/api

url_beg Returns true when the URL begins with one of the strings. This can be used to check whether a URL begins with a slash or with a protocol scheme.

Use path_beg instead.

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.