1、环境准备:
192.168.171.11 负载均衡器
192.168.171.12 web服务器1
192.168.171.13 web服务器2
2、部署负载均衡器,安装nginx
安装依赖包:
#yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel wget
官网下载源码包:
#wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压源码包
#tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
编译安装:
#cd /usr/local/src/nginx-1.18.0/
#./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
参数说明
–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支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求);
–with-http_sub_module,启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本);
–with-http_flv_module,启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件);
–with-http_mp4_module,启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件);
#make && make install
查看是否安装成功:
#echo $?
创建nginx用户:
#useradd -M -s /sbin/nologin nginx
启动并添加开机自启:
#/usr/local/nginx/sbin/nginx
#echo /usr/local/nginx/sbin/nginx >> /etc/rc.local
#chmod +x /etc/rc.d/rc.local
修改nginx配置文件:
vi /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http {
server 192.168.171.12:80 weight=1; #weight权重
server 192.168.171.13:80 weight=2;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重新加载nginx
#/usr/local/nginx/sbin/nginx -s reload
3、部署web服务器
web1:
#yum -y install httpd
#echo “web1” >/var/www/html/index.html
#systemctl start httpd
web2:
#yum -y install httpd
#echo “web2” >/var/www/html/index.html
#systemctl start httpd
4、客户端访问测试
curl 192.168.171.11