Noober
Noober

Reputation: 1626

nginx requests going to same server

I am using nginx for load balancing. I have tried both round_robin and least_conn. I have three servers running locally on ports 3001, 3002 and 3003. But all the requests always goes to only 3001 server. Here is my configuration:

worker_processes 1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream my_http_servers {
        least_conn;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }
    server {
        listen       3000;
        server_name  localhost;

        location / {
            proxy_pass         http://my_http_servers;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

I am not sure why connections are always going to only one server.

Upvotes: 0

Views: 412

Answers (1)

Luuk
Luuk

Reputation: 14958

when adding this to /etc/hosts:

127.0.0.1       localhost
127.0.0.2       localhost1
127.0.0.3       localhost2
127.0.0.4       localhost3

this config works:

worker_processes 1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream my_http_servers {
        least_conn;
        server localhost1:3001;
        server localhost2:3002;
        server localhost3:3003;
    }
    server {
        listen       3000;
        server_name  localhost;

        location / {
            proxy_pass         http://my_http_servers;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3001;
        server_name  localhost1;
        root html/3001;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3002;
        server_name  localhost2;
        root html/3002;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3003;
        server_name  localhost3;
        root html/3003;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

I have added the 3 servernames whit a differen root, just to be able to see which server does do the reply.

opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3002
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3002
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx #

Upvotes: 1

Related Questions