搭建nginx七层负载

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值