Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

nginx - 如果ip_hash,nginx无法进行负载平衡(nginx not loadbalancing in case of ip_hash)

I am trying to use nginx for loadbalancing.

(我正在尝试使用nginx进行负载平衡。)

I have to use ip_hash because I work with websockets.

(我必须使用ip_hash因为我使用的是websockets。)

Following is the configuration:

(以下是配置:)

#user  nobody;
worker_processes 3;

events {
    worker_connections  1024;
}


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


    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream my_http_servers {
        ip_hash;
        server 127.0.0.1:3001;
        server 127.0.0.1:3004;
        server 127.0.0.1:3003;
    }
    server {
        listen       3000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;

          proxy_pass http://my_http_servers;

          # enable WebSockets
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";

        }
    }
}

Now I have all the 3 servers and nginx running locally on machine 1 (ip: 192.168.10.2) .

(现在,我在机器1 (ip: 192.168.10.2)上本地运行了所有3台服务器和nginx。)

I also have a frontend application which calls this backend server.

(我也有一个称为此后端服务器的前端应用程序。)

My frontend runs on 192.168.10.2:4200 .

(我的前端在192.168.10.2:4200运行。)
When I call the http://192.168.10.2:4200 from machine1, it goes to say server1 .

(当我从machine1调用http://192.168.10.2:4200时,它说的是server1 。)

From my machine2 which is connected to the same WIFI (ip: 192.168.10.23) , I call http://192.168.10.2:4200 , but it still goes to server1 .

(从连接到相同WIFI (ip: 192.168.10.23)我的machine2中,我呼叫http://192.168.10.2:4200 ,但是它仍然转到server1 。)

ip_hash is not correctly doing load balancing.

(ip_hash未正确执行负载平衡。)

I am not sure what I am doing wrong, I understand ip_hash will be a sticky connection, so all requests from machine1 should go to server1 but from machine2 it should go to some other servers?

(我不确定自己在做什么错,我知道ip_hash将是粘性连接,因此来自machine1的所有请求都应发送到server1,但是来自machine2的所有请求应发送到其他一些服务器?)

Edit:

(编辑:)

I even tried using hash $remote_addr;

(我什至尝试使用hash $remote_addr;)

instead of ip_hash , but still all reqests are going to same single server.

(而不是ip_hash ,但是仍然所有请求都将发送到同一台单服务器。)

  ask by Noober translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...