zoukankan      html  css  js  c++  java
  • Nginx作为负载均衡——实战演练

    配置语法

    Syntax:upstream name {...}

    Default:——

    Context:http

    演示

    准备两台虚拟主机192.168.96.188、192.168.96.188

    在188这台主机上创建3个html静态文件

    vi /opt/app/code1/index.html

    <html>
    <head>
        <meta charset="utf-8">
        <title>server1</title>
    </head>
    <body style="">
        <h1>Server 1<h1>
    </body>
    </html>
    
    

    vi /opt/app/code2/index.html

    <html>
    <head>
        <meta charset="utf-8">
        <title>server2</title>
    </head>
    <body style="">
        <h1>Server 2<h1>
    </body>
    </html>
    

    vi /opt/app/code3/index.html

    <html>
    <head>
        <meta charset="utf-8">
        <title>server3</title>
    </head>
    <body style="">
        <h1>Server 3<h1>
    </body>
    </html>
    

    在188这台主机上创建3个.conf配置文件。配置如下

     vi /etc/nginx/conf.d/server1.conf

    server {
        listen       8001;        #请求的8001端口 
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server1.access.log  main;
    
        location / {
            root   /opt/app/code1;        #静态文件目录
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504 404  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }
    

    vi /etc/nginx/conf.d/server2.conf

    server {
        listen       8002;        #请求的8002端口 
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server1.access.log  main;
    
        location / {
            root   /opt/app/code2;        #静态文件目录
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504 404  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }
    

     vi /etc/nginx/conf.d/server3.conf

    server {
        listen       8003;        #请求的8003端口 
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server1.access.log  main;
    
        location / {
            root   /opt/app/code3;        #静态文件目录
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504 404  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }
    

    在135这台主机上配置负载均衡

    添加配置文件

    vi /usr/local/nginx/conf/hosts/upstream.conf

     upstream test {
            server 192.168.96.188:8001;        # 添加 test 配置监听的ip+端口
            server 192.168.96.188:8002;
            server 192.168.96.188:8003;
    
            }
    
    server {
        listen       80;
        server_name  localhost jeson.t.imooc.io;
    
        #charset koi8-r;
        #access_log  /usr/local/nginx/logs/test_proxy.access.log  main;
        resolver  8.8.8.8;
    
        location / {
            proxy_pass http://test;      # 添加需要监听 test
            include proxy_params;     # 存储语法的文件名称
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }
    
    

    在/usr/local/nginx/conf/目录新建文件

    vi proxy_params   #对应楼上配置文件中的文件名称

    proxy_redirect default;
    
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    
    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    
    proxy_buffer_size 32k;
    proxy_buffering on;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 256k;
    proxy_max_temp_file_size 256k;
    
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    
    

    配置好两台主机的nginx。记得检查语法、重启nginx。

    使用本地浏览器访问192.168.96.135,按F5刷新,会循环访问server1、server2、server3、

    这是关闭主机188的8002端口

     iptables -I INPUT -p tcp --dport 8002 -j DROP

    再次本地浏览器访问192.168.96.135,按F5刷新。这时候就不会有server2访问页面了

    现实场景中,三台主机有一台主机宕机了,没有做负载均衡,会导致其他主机也无法访问。

    有做负载均衡,一台主机宕机了,其他主机照样也能访问

  • 相关阅读:
    独木舟上的旅行
    会场安排问题
    喷水装置(二)
    喷水装置(一)
    款待奶牛
    整理书本
    贪心算法基本思想和典型例题(转)
    贪心算法
    太乱了,不要了
    Runtime Error:Floating point exception 不知道拿错了
  • 原文地址:https://www.cnblogs.com/joy-sir/p/12162677.html
Copyright © 2011-2022 走看看