Nginx负载均衡配置

l2osamch  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(155)

我在CentOS 9上有一个nginx.config文件,我想在其中配置两个上游http块之间的负载平衡。唯一的问题是位置。负载平衡在“presto_resource_managers”上执行得很好,但在presto_coordinators上却没有。我尝试了多种配置。
nginx版本:简体中文
尝试1:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    upstream presto_coordinators {
        server 192.168.33.10:8080;
        server 192.168.33.11:8080;
    }

    upstream presto_resource_managers {
        server 192.168.33.10:8083;
        server 192.168.33.11:8083;
    }

    server {
        listen 8085;
        location /coordinators/ { ## I have tried without the slashes and combinations.
            proxy_pass http://presto_coordinators;
        }
        location / {
            proxy_pass http://presto_resource_managers;
        }
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }
}

尝试2:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    upstream presto_coordinators {
        server 192.168.33.10:8080;
        server 192.168.33.11:8080;
    }

    upstream presto_resource_managers {
        server 192.168.33.10:8083;
        server 192.168.33.11:8083;
    }

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;

        location /coordinators {
            proxy_pass http://presto_coordinators;
        }

        location / {
            proxy_pass http://presto_resource_managers;
        }
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}

尝试3:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    upstream presto_coordinators {
        server 192.168.33.10:8080;
        server 192.168.33.11:8080;
    }

    upstream presto_resource_managers {
        server 192.168.33.10:8083;
        server 192.168.33.11:8083;
    }

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;

        location /coordinators {
            proxy_pass http://presto_coordinators;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
        location / {
            proxy_pass http://presto_resource_managers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;

这些尝试都没有对协调器的负载平衡起作用。
对于那些熟悉presto的人来说,这是协调器的配置文件:

coordinator=true
node-scheduler.include-coordinator=false
http-server.http.port=8080
query.max-memory=50GB
query.max-memory-per-node=1GB
discovery.uri=http://localhost:8085
resource-manager-enabled=true

uri指向Loadbalancer的presto_resource_manager,以便可以向ResourceManagers通告协调器。节点本身(服务器)暴露给192.168.33.10:8080,因此它可以被负载均衡器获取。

k75qkfdt

k75qkfdt1#

所以我找到了解决办法。这是一个语法错误这是我的文件现在的样子:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    upstream presto_coordinators {
        server 192.168.33.10:8080;
        server 192.168.33.11:8080;
    }

    upstream presto_resource_managers {
        server 192.168.33.10:8083;
        server 192.168.33.11:8083;
    }

    server {
        listen 8085;
        location /coordinators/ {
            proxy_pass http://presto_coordinators/;
    }
        location / {
            proxy_pass http://presto_resource_managers;
        }
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }
}

相关问题