Nginx负载均衡+动静分离+Nginx开机自启动

3 篇文章 0 订阅
1 篇文章 0 订阅

使用 nginx 实现动静分离的负载均衡集群*:

LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层) 客户端通过访问分发器的VIP来访问网站 现在应用更复杂,比如现在网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静态页面。静态页面一般是不变的* LVS是四层的,基于IP的。现在需要在应用层基于不同的应用进行分发, 七层LB , Nginx / Haproxy都可以支持7层LB

拓扑图:

在这里插入图片描述
工作中,希望这样:
静态文件处理:可以使用nginx 或apache
动文件处理: apache ,tomcat
图片文件处理: squid

Nginx 负载均衡基础知识以及部署步骤:

  • 基础知识:

    Nginx 的 upstream 负载的5种方式,目前最常用 前3 种方式

  1. 轮询(默认)

    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

  2. weight

    指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。

  3. ip_hash

    每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。

  4. fair(第三方)

    按后端服务器的响应时间来分配请求,响应时间短的优先分配。

  5. url_hash(第三方) url哈西

    按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效

部署安装:

  1. 安装 nginx 时必须先安装相应的编译工具 和相关依赖**

    [root@instance-atub3vww ~]# yum -y install gcc gcc-c++ autoconf automake  zlib zlib-devel openssl openssl-devel pcre pcre-devel
    

    Nginx源码下载请点击:http://nginx.org/en/download.html

    本次安装使用:nginx-1.19.1

  2. 下载源码包并解压:

    [root@instance-atub3vww ~]# wget "http://nginx.org/download/nginx-1.19.1.tar.gz" -O "nginx-1.19.1.tar.gz"
    [root@instance-atub3vww ~]# tar -zxvf nginx-1.19.1.tar.gz 
    

    在这里插入图片描述

  3. 查看Nginx源码目录

    [root@instance-atub3vww ~]# ll nginx-1.19.1
    

    在这里插入图片描述

  4. 为nginx设置安装目录和启用的模块

    [root@instance-atub3vww nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
    

    在这里插入图片描述
    查看参数:
    [root@xuegod63 nginx-1.8.0]# ./configure --help | grep mp4
    参数:
    –with-http_dav_module :
    启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启

    –with-http_stub_status_module :
    启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)

    –with-http_addition_module:
    启用ngx_http_addition_module支持(作为一个输出过滤器,学神 IT 教育,祝您早日成为技术牛人! Man 老师
    支持不完全缓冲,分部分响应请求)

    –with-http_sub_module :
    启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)

    –with-http_flv_module :
    启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)

    –with-http_mp4_module :
    启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)

  5. 编译和安装

    查看CPU逻辑数cat /proc/cpuinfo | grep processor | wc -l

    [root@instance-atub3vww nginx-1.19.1]# cat /proc/cpuinfo | grep processor | wc -l
    1
    
    [root@instance-atub3vww nginx-1.19.1]# make & make install 
    

    在这里插入图片描述

  6. 生成运行nginx的用户:

    [root@instance-atub3vww nginx-1.19.1]#  useradd -u 8000 -s /sbin/nologin nginx
    [root@instance-atub3vww nginx-1.19.1]# id nginx
    uid=8000(nginx) gid=8000(nginx) groups=8000(nginx)
    [root@instance-atub3vww nginx-1.19.1]# 
    

    [root@instance-atub3vww nginx-1.19.1]# ll /usr/local/nginx/

conf配置文件
html网站根目录
logs日志
sbinnginx启动脚本
  1. 备份conf,修改index.html,启动nginx

    [root@instance-atub3vww conf]# cp nginx.conf nginx.conf.bak
    [root@instance-atub3vww nginx]# ./sbin/nginx
    [root@instance-atub3vww html]# echo "Test Server:101.12.74.199" > index.html 
    [root@instance-atub3vww html]# ../sbin/nginx
    

    查看Nginx是否启动成功:

    [root@instance-atub3vww html]# netstat -ntlp
    

    在这里插入图片描述
    访问Nginx

    [root@instance-atub3vww html]# curl  "http://127.0.0.1:80"
    Test Server:101.12.74.199
    
  2. nginx服务日常操作:

    测试配置文件语法:

    [root@instance-atub3vww sbin]# ./nginx  -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
    

    重新加载配置文件:

    [root@instance-atub3vww sbin]# ./nginx  -s reload 
    

    关闭nginx:

    [root@instance-atub3vww sbin]# /usr/local/nginx/sbin/nginx -s stop
    

    开机自启动,请参考链接:https://www.jianshu.com/p/72603ec31c74

**

配置Nginx为分发器,实现动静分离:

  1. 修改nginx.conf,如以下配置
[root@instance-atub3vww conf]# pwd
/usr/local/nginx/conf
[root@instance-atub3vww conf]# vim nginx.conf

user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        if ($request_uri ~* \.html$){ 
                   proxy_pass http://htmlservers;
           } 
        if ($request_uri ~* \.php$){ 
                   proxy_pass http://phpservers; 
           }
              proxy_pass http://picservers; 
        }

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
   upstream htmlservers { #定义负载均衡服务器组名称 
         server 127.0.0.1:8080;
         server 127.0.0.1:8081; 
   } 
   upstream phpservers{
         server 127.0.0.1:8080; 
         server 127.0.0.1:8081; 
   }
   upstream picservers {
         server 127.0.0.1:8080;
         server 127.0.0.1:8081;
   }
}
  1. 定义负载均衡设备的 Ip

    在配置文件nginx.conf的最后一行}前,添加以下内容:
    upstream htmlservers { #定义负载均衡服务器组名称
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    }
    upstream phpservers{
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    }
    upstream picservers {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    }

    [root@instance-atub3vww conf]# vim nginx.conf
    [root@instance-atub3vww conf]# ../sbin/nginx  -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@instance-atub3vww conf]# ../sbin/nginx -s reload 
    

    查看端口有没有在监听:

    [root@instance-atub3vww conf]# netstat -ntlp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      507/rpcbind         
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12900/nginx: master 
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      979/sshd      
    
  2. 配置Web Server1:

    生成静态测试文件:

    [root@cs00 html]# echo "Test Server:192.168.88.129 Static Server" > index.html
    

    生成动态测试文件:

    [root@cs00 html]# cat 8080.php 
    127.0.0.1:8080-php
    <?
    php phpinfo(); 
    ?>
    

    上传一张图片,如下:
    在这里插入图片描述
    配置Web Server2:
    生成静态测试文件:

    [root@cs01 html]# echo "Test Server:192.168.88.130 Static Server" > index.html
    

    生成动态测试文件:

    [root@cs01 html]# cat 8080.php 
    127.0.0.1:8081-php
    <?
    php phpinfo(); 
    ?>
    [root@cs01 html]# 
    

    上传一张图片,如下:

    在这里插入图片描述

  3. 测试转发静态界面以及动态页面:

    [root@instance-atub3vww ~]# curl  http://127.0.0.1
    Test Server:192.168.88.129 Static Server
    [root@instance-atub3vww ~]# curl  http://127.0.0.1
    Test Server:192.168.88.130 Static Server
    [root@instance-atub3vww ~]# curl  http://127.0.0.1
    Test Server:192.168.88.129 Static Server
    [root@instance-atub3vww ~]# curl  http://127.0.0.1
    Test Server:192.168.88.130 Static Server
    [root@instance-atub3vww ~]# 
    [root@instance-atub3vww ~]# 
    [root@instance-atub3vww ~]# curl  http://127.0.0.1/8080.php
    127.0.0.1:8081-php
    <?
    php phpinfo(); 
    ?>
    [root@instance-atub3vww ~]# curl  http://127.0.0.1/8080.php
    127.0.0.1:8080-php
    <?
    php phpinfo(); 
    ?>
    [root@instance-atub3vww ~]# curl  http://127.0.0.1/8080.php
    127.0.0.1:8081-php
    <?
    php phpinfo(); 
    ?>
    [root@instance-atub3vww ~]# curl  http://127.0.0.1/8080.php
    127.0.0.1:8080-php
    <?
    php phpinfo(); 
    ?>
    [root@instance-atub3vww ~]# 
    

    测试转发图片:
    在这里插入图片描述
    在这里插入图片描述

  4. 测试自动剔除坏的节点:

    停掉一边Nginx,本此演示停的是8081端口服务:

    [root@cs01 html]# systemctl stop nginx
    [root@cs01 html]# 
    

    测试结果,Nginx服务正常可用,只访问8080端口的服务:在这里插入图片描述

Nginx负载的5种策略设置方法:

  • 1、轮询(默认)

    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    upstream backserver {
    server 192.168.88.129;
    server 192.168.88.130;
    }

  • 2、指定权重

    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    upstream backserver {
    server 192.168.88.129 weight=1;
    server 192.168.88.130 weight=2;
    }

  • 3、IP绑定 ip_hash

    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    upstream backserver {
    ip_hash;
    server 192.168.88.129;
    server 192.168.88.130;
    }

  • 4、fair(第三方)

    按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    upstream backserver {
    server server1;
    server server2;
    fair;
    }

  • 5、url_hash(第三方)

    按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
    upstream backserver {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值