Nginx - 是否可以将负载均衡器与外部 URL 一起使用?

Nginx - Is it possible to use load balancer with external urls?

我的问题如下:

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

这些应用程序是用 Gunicorn + Django 制作的,并通过 Nginx 的套接字提供服务。 (没有 Docker 或奇怪的东西,最多是一个主管来保持活力)

当前系统运行完美,但对于某些任务它可以运行得更快,这就是负载平衡器的原因。但是,我无法使用不在同一台机器上的服务器地址来使负载平衡器工作(没有 localhost:port、x.x.x.x、x.x.x.x:port 或 [=35 中包含的网址=])

这是一个 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;
}

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.comtest.url.com 不在同一台机器上。他们有不同的 public IP。在 external.machine.com 中,我已经配置了一个 Nginx,可以正确地为“昂贵”应用提供服务。

我找不到任何相关内容或尝试过此操作的人,所有单个 post 或我找到的文档都与本地 IP 相关或使用本地 IP,而不是外部 IP 的常规 URL。

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

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

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

upstream balancer {
     # least_conn;
     server external.machine.com:<CUSTOM_PORT>;
}

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

    return 301 https://$server_name;
}

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


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