nginx负载均衡

准备3台服务器,一台做nginx负载均衡,两台为Apache的web服务器

 

1.首先源码安装nginx

nginx的安装方式

配置nginx负载均衡和nginx负载均衡调度算法为加权轮询,加权轮询比例为1:3,下列代码有注释。

vim /usr/local/nginx/conf/nginx.conf
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;
    upstream webservers {          #添加nginx反向代理
        server 192.168.189.130 weight=1;  #nginx需要代理的服务器,可以根据情况加端口
        server 192.168.189.131 weight=3;  #服务器2
    }
    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 / {
                proxy_pass  http://webservers;  #反向代理设置为web群组
        }

  

为两台服务器创建测试页面

采用Apache服务,路径:

/var/www/html
vim /var/www/html/index.html
web1 web2

 

测试访问nginx的ip,并测试负载均衡和加权轮询

可以看到1:3的web1/2出现次数

 小步骤:

收集整理nginx负载均衡的日志到/tmp下面

cat /usr/local/nginx/logs/*.log > /tmp/nginx

  过滤出访问最多的前两条client IP地址

[root@localhost logs]# cat /usr/local/nginx/logs/access.log | awk '{print $1}' |sort|uniq |sed -n '1,2p'

  查看到两个访问最多的ip

nginx负载均衡的日志:

tail -f /usr/local/nginx/logs/access.log
posted @ 2019-12-20 10:03  Security  阅读(261)  评论(0编辑  收藏  举报
小布的个人空间 京ICP备2023021324号-1