代码之家  ›  专栏  ›  技术社区  ›  Khadijah Celestine

如何在nginx中对localhost上的3个端口进行负载均衡?

  •  0
  • Khadijah Celestine  · 技术社区  · 1 年前

    我有3个端口加载不同的内容,我想从8080端口对它们进行负载平衡。我是nginx的新手,在nginx上提供内容时,我觉得有一些基本的东西我不理解。我的3个端口可以单独工作,但不能进行负载平衡。如何编辑我的配置以在这3个端口之间实现负载平衡?

    upstream backend {
        server localhost:8081;
        server localhost:8082;
        server localhost:8083;
    }
    
    server {
        listen 8080;
        listen [::]:8080;
    
        server_name _;
    
        root /var/www/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    
    # Default server configuration
    #
    server {
        listen 8081 default_server;
        listen [::]:8081 default_server;
    
        root /var/www/n81;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name _;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }
    
    }
    
    server {
        listen 8082 default_server;
        listen [::]:8082 default_server;
    
        root /var/www/n82;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name _;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }
    
    }
    
    server {
        listen 8083 default_server;
        listen [::]:8083 default_server;
    
        root /var/www/n83;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name _;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }
    
    }
    
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Reza Gharabaghi    1 年前

    看看 Nginx 官方文件 here . 我认为你误解了负载平衡的概念。

    我假设你有4个VPS Nginx 安装在每一个。其中一个用于负载平衡,另外三个用于提供文件。

    你的期末考试 Nginx 配置文件可能如下所示:

    负载平衡器

    http {
        upstream myapp {
            server srv1.example.com;
            server srv2.example.com;
            server srv3.example.com;
        }
    
        server {
            listen 80;
    
            location / {
                proxy_pass http://myapp;
            }
        }
    }
    

    内容服务器1

    http {
        server {
            server_name srv1.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }
    

    内容服务器2

    http {
        server {
            server_name srv2.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }
    

    内容服务器3

    http {
        server {
            server_name srv3.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }
    
    推荐文章