Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to configure Nginx to broadcast incoming requests to multiple upstream servers simultaneously?

Tags:

nginx

The following fragment will pick one server at a time. Is there a way to hit them all at once?

upstream backend {
    server 17.0.0.1:8000;
    server 17.0.0.1:8001;
    server 17.0.0.1:8002;
    server 17.0.0.1:8003;

}

server {
    location / {
        proxy_pass http://backend;
    }
}
like image 255
swogger Avatar asked Apr 08 '14 20:04

swogger


1 Answers

Here is a solution using ngx_http_mirror_module (available since nginx 1.13.4):

server {
    location / {
        proxy_pass http://17.0.0.1:8000;
        mirror /s1;
        mirror /s2;
        mirror /s3;       
    }
    location /s1 { internal; proxy_pass http://17.0.0.1:8001$request_uri; }
    location /s2 { internal; proxy_pass http://17.0.0.1:8002$request_uri; }
    location /s3 { internal; proxy_pass http://17.0.0.1:8003$request_uri; }
}

nginx will:

  • send the same request to all servers
  • wait for all of them to finish
  • respond with the http://17.0.0.1:8000 response (and ignore the others)
like image 172
Pascal Pixel Rigaux Avatar answered Oct 26 '22 08:10

Pascal Pixel Rigaux