首页 > 解决方案 > nginx请求去同一台服务器

问题描述

我正在使用nginx负载平衡。我都试过了round_robinleast_conn。我在端口上本地运行了三台服务器30013002并且3003. 但是所有请求总是只发送到3001服务器。这是我的配置:

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;
        }
    }
}

我不确定为什么连接总是只连接到一台服务器。

标签: nginx

解决方案


将其添加到/etc/hosts

127.0.0.1       localhost
127.0.0.2       localhost1
127.0.0.3       localhost2
127.0.0.4       localhost3

此配置有效:

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;
        }
    }
}

我添加了 3 个不同的服务器名称root,只是为了能够查看哪个服务器确实进行了回复。

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 #

推荐阅读