【问题标题】:Nginx - Is it possible to use load balancer with external urls?Nginx - 是否可以将负载均衡器与外部 URL 一起使用?
【发布时间】:2020-07-17 18:43:13
【问题描述】:

我的问题如下:

我有 2 个 Web 应用程序,一个“普通”和一个“昂贵”。对于昂贵的任务,“普通”与“昂贵”通信。为了提高速度和减少瓶颈,计划在 2 台不同的机器上部署至少几个“昂贵”应用程序,并使用负载平衡器来拆分请求(而不是拥有 NASA PC,拥有 2 台或更多普通 PC) .

这些应用程序是用 Gunicorn + Django 制作的,并通过带有 Nginx 的套接字提供服务。 (没有 Docker 或奇怪的东西,在很大程度上是一个主管来维持生命)

当前系统运行良好,但对于某些任务它可能会更快,这就是负载平衡器的原因。但是,我无法使用不在同一台机器中的服务器地址(没有 localhost:port、x.x.x.x、x.x.x.x:port 或 /etc/hosts 中包含的 url)使负载均衡器工作

这是一个balancer.conf,它使用本地应用在我的本地工作

upstream balancer {
     # least_conn;
     server 192.168.22.200:8000;
     server 192.168.22.200:8001;
}


server {
    listen 80;
    server_name localhost;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 120;
        proxy_redirect off;

        proxy_pass http://balancer;
    }
}
 

这是我最后一次尝试使它与远程服务器一起工作(我需要 SSL 的东西,因为它是强制在它们上的)

upstream balancer {
     # least_conn;
     server external.machine.com;
}

server {
    listen 80;
    server_name test.url.com;

    return 301 https://$server_name$1;
}

server {
    listen 443 ssl http2;
    server_name test.url.com;

    # Turn on SSL
    ssl on;
    <exactly the same stuff I have in the others .conf for the ssl>

    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    location / {
        # proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # proxy_set_header X-Forwarded-Protocol $scheme;
        # proxy_set_header X-Real-IP $remote_addr;
        # proxy_read_timeout 120;
        # proxy_redirect off;

        proxy_pass http://balancer;
    }
}

澄清并记住:external.machine.com 和 test.url.com 不在同一台机器上。他们有不同的公共 IP。在 external.machine.com 中,我已经配置了一个 Nginx,它可以正确地为“昂贵”的应用程序提供服务。

我找不到任何相关的或尝试过的人,我发现的所有单个帖子或文档都与本地 IP 相关或完成,而不是外部 IP 的常规 URL。

所以我现在有一个问题,是否可以将 Nginx 负载均衡器与远程 IP 或仅与本地 IP 一起使用

【问题讨论】:

    标签: nginx load-balancing


    【解决方案1】:

    是的,您可以使用外部网址,但您需要指定端口。或者至少我是这样做的。

    也就是说,nginx的配置文件会是这样的:

    upstream balancer {
         # least_conn;
         server external.machine.com:<CUSTOM_PORT>;
    }
    
    server {
        listen 80;
        server_name test.url.com;
    
        return 301 https://$server_name$1;
    }
    
    server {
        listen 443 ssl http2;
        server_name test.url.com;
    
        # Turn on SSL
        ssl on;
        <exactly the same stuff I have in the others .conf for the ssl>
    
        resolver 8.8.8.8 8.8.4.4 valid=300s;
        resolver_timeout 5s;
    
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Protocol $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_read_timeout 120;
            proxy_redirect off;
    
            proxy_pass http://balancer;
        }
    }
    

    显然你需要在机器上打开那个端口

    在指定的机器上,你的 nginx 文件必须是这样的

    upstream wsgi_socket {
        server unix:/tmp/socket.sock fail_timeout=0;
    }
    
    server {
        # listen [::]:80 ipv6only=on;
        listen 80;
        server_name test.url.com;  # same server name as is the balancer.conf
    
        return 301 https://$server_name$1;
    }
    
    
    server {
    
        listen <CUSTOM POST> ssl http2;
    
        server_name test.url.com;  # same server name as is the balancer.conf
        root <path to your proejct root>;
        client_max_body_size 15M;
        # You can configure access_log and error_log too
    
        # Turn on SSL
        ssl on;
        <all the ssl stuff>
    
        resolver 8.8.8.8 8.8.4.4 valid=300s;
        resolver_timeout 5s;
    
        location /static  {
            alias <path to your static if you have statics>;
        }
    
        location / { 
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        } 
    
        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Protocol $scheme;
            proxy_read_timeout 120;
            proxy_redirect off;
    
            proxy_pass http://unix:/tmp/socket.sock;
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多