Nginx-有可能使用带有外部url的负载均衡器吗



我的问题如下:

我有两个网络应用程序;"正常";和一个";昂贵的";。";"正常";与";昂贵的";对于昂贵的任务。为了提高速度和减少瓶颈;昂贵的";应用程序在2台不同的机器中,并使用负载均衡器来分割请求(而不是拥有一台NASA PC,而是拥有2台或更多的普通PC(。

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

当前的系统运行良好,但对于某些任务,它可能会运行得更快,这就是负载均衡器的原因。但是,我无法使用不在同一台机器中的服务器地址(没有localhost:port、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.comtest.url.com不在同一台机器上。他们有不同的公共IP。在external.machine.com中,我配置了一个Nginx,它为;昂贵的";应用程序正确。

我找不到任何相关的东西,也找不到尝试过这种方法的人,我发现的所有单个帖子或文档都与本地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$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;
}
}

最新更新