Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the max value that I can set for weight in NGINX upstream server configuration?

When configuring NGINX with multiple server entries, one can configure a round-robin algorithm for distributing load. NGINX offers the weight to indicate how weight is distributed. For ex.:

upstream backend {
 server backend1.example.com       weight=5;
 server backend2.example.com:8080  weight=1;

}
server {
    location / {
        proxy_pass http://backend;
    }
}

Question is: What is the maximum value assignable to weight?

My problem is that I have encountered a configuration with 2 server entries one with a weight value of 2000000000 (2 billion) and one with a value of 1. The intention was to have all traffic directed on the first server as temporarily the second one was down. However after far less than 2 bil requests users got error because they were directed to the second server.

like image 575
coding-dude.com Avatar asked Oct 27 '25 09:10

coding-dude.com


1 Answers

You should use health checking for that usage, not weight.

You have 2 options:

  1. Using built in nginx layer 3 healthchecks:

    Using max_fails, fail_timeout and even backup directive.

    backup: marks the server as a backup server. It will be passed requests when the primary servers are unavailable.

  2. Using community modules like nginx_upstream_check_module

    This module enables layer 7 healthchecks, This is the recommended way.

like image 95
Farhad Farahi Avatar answered Oct 30 '25 13:10

Farhad Farahi