简体   繁体   中英

Nginx reverse proxy: try multiple upstream servers in parallel and return the first successful response

I've configured nginx as a reverse proxy for downloading software artifacts (eg JAR files) and want to speed it up by proxying each request to multiple upstream servers in parallel.

The following currently works: with nginx running at localhost:8080, I request localhost:8080/com/foo/bar.jar , nginx checks serially for the file com/foo/bar.jar at each host in a pre-configured list (eg repo1.org/com/foo/bar.jar , repo2.org/com/foo/bar.jar , etc.), and returns the first matching artifact (ie first to return status 200).

The config below makes this work for repos at maven.org and osgeo.org:

events {}
http {
    server {
        listen 8000;
        location / {
            proxy_pass https://repo1.maven.org/maven2/;
        }
    }
    server {
        listen 8001;
        location / {
            proxy_pass https://repo.osgeo.org/repository/release/;
        }
    }
    upstream repositories {
        server localhost:8000;
        server localhost:8001;
    }
    server {    
        location / {
            proxy_next_upstream error timeout http_404;
            proxy_pass http://repositories;
        }
    }
}

To decrease latency, I'd like for nginx to run the requests in parallel, return the first one that gives a 200, and otherwise return error code 404.

I'm not sure which directives (if any) can enable this kind of behavior. I appreciate any tips.

According to nginx documentation it is possible using nginx plus. As stated in nginx plus documentation , the load balancing method you are searching for is the Least Time method.

If you are using the default nginx you can use documentation nginx

tldr;

  • Default load balancing configuration (Round Robin)
  • Least connected load balancing
  • Weighted load balancing
  • Session persistence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM