'nginx proxy with diffrent contents per upstream server

I have this Nginx configuration

user  root;
worker_processes auto;
worker_rlimit_nofile 1048576;
pid logs/nginx.pid;
thread_pool one threads=48 max_queue=0;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log off;

#pid        logs/nginx.pid;


events {
    worker_connections 1048576;
    multi_accept on;
    accept_mutex off;
}


http {


    upstream origins {
      hash $uri;
      server ip:36419;
      server ip:2525;
      server ip:36520;
      server ip:2525;
      server ip:2525;
      server ip:36419;
      server ip:2525;
      server ip:2525;
    }
        
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  off;

   sendfile on;
   sendfile_max_chunk 10m;
     keepalive_timeout 300s;
  
    #gzip  on;
     proxy_cache_path /var/cache/nginx/ramcache use_temp_path=off 
     keys_zone=hls_cache_temp:200m max_size=40g inactive=2m;
     proxy_cache hls_cache_temp;
     proxy_read_timeout 10s;
     proxy_send_timeout 10s;
     proxy_connect_timeout 10s;
      
     proxy_cache_methods GET HEAD;
     proxy_cache_valid 200 302 60s;
     proxy_cache_valid 404 3s;
     proxy_cache_key $uri;
     proxy_cache_lock on;
     proxy_cache_lock_age 5s;
     proxy_cache_lock_timeout 1h;
         proxy_redirect off;
     proxy_ignore_headers Cache-Control;
     proxy_ignore_headers Set-Cookie;
     proxy_http_version 1.1;

   server {
     listen 8080;
    
     
    location ~ \.(m3u8|mpd)$ {
            proxy_cache_valid 200 302 5s;
            proxy_pass http://origins;
            proxy_next_upstream error timeout http_502 http_404;
        } 

        location / {
            proxy_pass http://origins;
      proxy_next_upstream error timeout http_502 http_404;

        }
        
    }

}

the upstream servers have different content each when the content is not found it will go to the next backend server till it finds the content but this approach will take time for Nginx to check all servers one by one

is there any way to make it stick to a server that had HTTP 200 ok from the previous check ?? not to check all the servers one by one everytime ??



Solution 1:[1]

You can try using ip_hashload balancing algorithm, it will stick the client to same server except when the server is unavailable, as per documentation says

With ip-hash, the client’s IP address is used as a hashing key to determine what server in a server group should be selected for the client’s requests. This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.

ref https://nginx.org/en/docs/http/load_balancing.html

upstream origins {
    ip_hash;
    server ip:36419;
    server ip:2525;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nothinux