Warm tip: This article is reproduced from serverfault.com, please click

Proxy entire url path to haproxy from nginx

发布于 2021-01-13 23:28:19

I have nginx in entry point to server. which handle SSL connection and send HTTP request in /api/v1/ to HAproxy for balancing. this is my nginx config:

    location /api/v1/ {
        proxy_pass http://127.0.0.1:8585/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

and HAproxy config:

frontend haproxy_entry
    bind *:8585
    mode http
    log global
    acl is_api url_beg /api/v1/
    use_backend api if is_api

the problem I have is that I should pass /api/v1/ path twice. if I request https://servername.com/api/v1/ haproxy would not handle this properly but for :https://servername.com/api/v1/api/v1/ it's ok. how can i fix this?

Questioner
amir
Viewed
22
Richard Smith 2019-01-25 19:22:11

The proxy_pass statement includes an optional URI which is used to modify the URL before passing it upstream.

In your question, the proxy_pass statement includes a URI of /, which in conjunction with the location value, causes the /api/v1/ part of the original URL to be substituted for /.

For example: https://example.com/api/v1/foo is translated to http://127.0.0.1:8585/foo.

It seems that you do not need this. To turn off the feature, simply remove the URI part.

For example:

location /api/v1/ {
    proxy_pass http://127.0.0.1:8585;
    ...
}

See this document for details.