本文共 3927 字,大约阅读时间需要 13 分钟。
实验环境1
1测试硬件准备三台虚拟机,两台做负载均衡一台做RS 2测试软件准备系统:Red Hat6.4 x86_64软件:nginx-1.8.1.tar.gz3安装之前需要先安装相关基础环境包(有些系统里面已经有了)yum install opensslyum install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel -yyum install libxml2 libxml2-devel zlib zlib-devel ncurses ncurses-devel curl curl-devel -yyum install gd gd2 gd-level gd2-devel -y4 安装pcre软件包wget --no-check-certificatetar -zxf pcre-8.32.tar.gz ./configure 编译配置报错问题描述:checking for dirent.h... yeschecking windows.h usability... nochecking windows.h presence... nochecking for windows.h... noconfigure: error: You need a C++ compiler for C++ support.解决方法:系统包缺少C++编译器 需要安装gcc-c++的包然后 yum 安装下就可以了makemake installcd ../ 到上级目录====================pcre安装完成===============================5 安装nginxwget 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 指定非登录式 shellmakemake install=========nginx安装完成==========================================6 安装完后的配置ln -s /application/nginx-1.8.1 /application/nginxecho "/usr/local/lib" >>/etc/ld.so.conftail -1 /etc/ld.so.conf``ldconfig/application/nginx/sbin/nginx[root@lb01 application]# ps -ef | grep nginx | grep -v greproot 17057 1 0 16:18 ? 00:00:00 nginx: master process /application/nginx/sbin/nginxnginx 17058 17057 0 16:18 ? 00:00:00 nginx: worker process [root@lb01 application]# lsof -i tcp:80COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 17057 root 6u IPv4 82990 0t0 TCP :http (LISTEN)nginx 17058 nginx 6u IPv4 82990 0t0 TCP *:http (LISTEN)7 配置调试用于测试的web服务器注意:操作只在以下nginx web 服务器节点操作:配置并查看web服务配置结果两台RS1全部按照上面的步骤nginx服务。然后执行如下命令:RS1(192.168.232.132)echo "www.etiantian132.org">/application/nginx/html/index.htmlcat /application/nginx/html/index.html/application/nginx/sbin/nginx -s reloadRS2(192.168.232.133)echo "www.etiantian133.org">/application/nginx/html/index.htmlcat /application/nginx/html/index.html/application/nginx/sbin/nginx -s reload然后各自在本机curl下自己看下显示效果修改主配置文件实现负载均衡cd /application/nginx/confmkdir 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 20180107upstream 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 -tnginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful还需要本机上面做一个本地域名解析操作 将本机的ip 解析为blog.etiantian.org重启三台机器上面的nginx
/application/nginx/sbin/nginx -s reload然后使用curl 命令测试可以看到多次执行 会平均分配到两台机器上面去,从而是实现负载均衡。
转载于:https://blog.51cto.com/11569838/2058183