Nginx负载均衡应用案例剖析

实验环境1
1测试硬件准备
三台虚拟机,两台做负载均衡一台做RS

Nginx负载均衡应用案例剖析
2测试软件准备
系统:Red Hat6.4 x86_64
软件:nginx-1.8.1.tar.gz
3安装之前需要先安装相关基础环境包(有些系统里面已经有了)
yum install openssl
yum install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel -y
yum install libxml2 libxml2-devel zlib zlib-devel ncurses ncurses-devel curl curl-devel -y
yum install gd gd2 gd-level gd2-devel -y
4 安装pcre软件包
wget https://ftp.pcre.org/pub/pcre/pcre-8.32.tar.gz --no-check-certificate
tar -zxf pcre-8.32.tar.gz
./configure
编译配置报错
问题描述:
checking for dirent.h... yes
checking windows.h usability... no
checking windows.h presence... no
checking for windows.h... no
configure: error: You need a C++ compiler for C++ support.
解决方法:系统包缺少C++编译器 需要安装gcc-c++的包
然后 yum 安装下就可以了
make
make install
cd ../ 到上级目录
====================pcre安装完成===============================
5 安装nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module
说明:--user=nginx 指定用户
--group=nginx 指定组
--prefix=/application/nginx-1.8.1 指定安装路径
--with-http_stub_status_module 状态模块
--with-http_ssl_module ssl模块
useradd nginx -s /sbin/nologin -M 需要把用户创建起来 -M 不创建家目录 -s 指定非登录式 shell
make
make install
=========nginx安装完成==========================================
6 安装完后的配置
ln -s /application/nginx-1.8.1 /application/nginx
echo "/usr/local/lib" >>/etc/ld.so.conf
tail -1 /etc/ld.so.conf``
ldconfig
/application/nginx/sbin/nginx
[root@lb01 application]# ps -ef | grep nginx | grep -v grep
root 17057 1 0 16:18 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 17058 17057 0 16:18 ? 00:00:00 nginx: worker process
[root@lb01 application]# lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 17057 root 6u IPv4 82990 0t0 TCP
:http (LISTEN)
nginx 17058 nginx 6u IPv4 82990 0t0 TCP *:http (LISTEN)
7 配置调试用于测试的web服务器
注意:操作只在以下nginx web 服务器节点操作:
Nginx负载均衡应用案例剖析
配置并查看web服务配置结果
两台RS1全部按照上面的步骤nginx服务。
然后执行如下命令:
RS1(192.168.232.132)
echo "www.etiantian132.org">/application/nginx/html/index.html
cat /application/nginx/html/index.html
/application/nginx/sbin/nginx -s reload
RS2(192.168.232.133)
echo "www.etiantian133.org">/application/nginx/html/index.html
cat /application/nginx/html/index.html
/application/nginx/sbin/nginx -s reload
然后各自在本机curl下自己看下显示效果
Nginx负载均衡应用案例剖析
修改主配置文件实现负载均衡
cd /application/nginx/conf
mkdir extra 创建extra目录
cd extra/
vim ../nginx.conf

#user  nobody;
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;
    sendfile        on;
    keepalive_timeout  65;
    include extra/upstream01.conf;
}
######################################
# 删除我们不需要的 添加这行内容
include extra/upstream01.conf;

然后我们需要在extra目录下面创建upstream01.conf 并编辑它
vim extra/upstream01.conf
#blog lb by cyt at 20180107
upstream blog_real_server {
        server 192.168.232.132:80 weight=5;    #upstream 定义一个vserver的名字                                                   blog_real_server
        server 192.168.232.133:80 weight=5;

}

        server {
                listen   80;

                server_name blog.etiantian.org;

                location / {

                proxy_pass http://blog_real_server;   # 通过proxy_pass 定义如果访问                                                    blog.etiantian.org 自动转到                                                        blog_real_server 下面定义的两台                                                    realserver上面去

        }
}

检查语法

然后保存退出后
[root@lb01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
还需要本机上面做一个本地域名解析操作 将本机的ip 解析为blog.etiantian.org

Nginx负载均衡应用案例剖析

重启三台机器上面的nginx
/application/nginx/sbin/nginx -s reload
然后使用curl 命令测试
Nginx负载均衡应用案例剖析

可以看到多次执行 会平均分配到两台机器上面去,从而是实现负载均衡。

相关推荐