Linux配置下nginx负载均衡

1.安装部署nginx
1、在第三方下载nginx-1.14.0.tar.gz

# tar zxf nginx-1.14.0.tar.gz

# ls

# cd nginx-1.14.0

# ls

# cd src/

# ls

# cd core/

# vim nginx.h

第14行的nginx版本删掉,防黑客攻击

# cd ..

# cd auto/

# cd cc/

# vim gcc

注释CFLAGS="$CFLAGS -g"

#yum install gcc -y

# yum install pcre-devel -y

# yum install openssl-devel -y

# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio

# make

# make install

# ln -s /usr/local/nginx/sbin/nginx /sbin/

# nginx -t

# nginx

在网页访问172.25.25.4、

2.负载均衡
1)负载均衡配置

# 主配置文件:/usr/local/nginx/conf/nginx.conf

# vim /usr/local/nginx/conf/nginx.conf

user  nginx nginx;
# nginx 多核绑定
worker_processes  2;
worker_cpu_affinity 01 10;
events {
    worker_connections  65535;
}
# 后端服务器
http {
        upstream westos{
        server 172.25.25.1:80;
        server 172.25.25.3:80;
     }

server {
                listen 80;
                server_name www.redhat.org;

                location / {
                proxy_pass http://westos;
                }
        }
# useradd -M -d /usr/local/nginx/ nginx #创建用户

# nginx -s reload #重新加载

# vim /etc/security/limits.conf

nginx    - nofile     65536

开启两个后端服务器的httpd

真机测试:

# curl www.redhat.org

(2)hash算法:

# vim /usr/local/nginx/conf/nginx.conf

http {

upstream westos{

ip_hash;

server 172.25.25.1:80;

server 172.25.25.3:80;

}

3)weight权重,默认为1

# vim /usr/local/nginx/conf/nginx.conf

http {

upstream westos{

server 172.25.25.1:80 weight=2;

server 172.25.25.3:80;

}

3.增加外部扩展模块
需要用到nginx-1.10.1.tar.gz版本

# 关闭服务

# nginx -s stop

# 在第三方下载nginx-sticky-module-ng.tar.gz nginx-1.10.1.tar.gz,解压

# tar zxf nginx-sticky-module-ng.tar.gz

# tar zxf nginx-1.10.1.tar.gz

# cd nginx-1.10.1

#ls

# 源码编译三部曲

# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/mnt/nginx-sticky-module-ng
# make

# make install

# cd /opt/nginx/conf/

# cp /usr/local/nginx/conf/nginx.conf .

# 更改主配置文件

# vim nginx.conf

http {

upstream westos{

sticky;

server 172.25.25.1:80;

server 172.25.25.3:80;

}

# /opt/nginx/sbin/nginx -t

# 启动服务

# /opt/nginx/sbin/nginx
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值