• 七层负载均衡-nginx


    目录

    一、Nginx的介绍

    二,Nginx的安装

    三,Nginx的优化

    四,nginx并发优化

     五,nginx平滑升级及回退

     六,nginx 负载均衡

     七,nginx 算法

     八,nginx限流

     九,nginx 配置管理

    十,nginx 重定向 

    十一,nginx 防盗链 


    一、Nginx的介绍

    Nginx是一款轻量级、高性能的HTTP和反向代理web服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like协议下发行;其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好;中国大陆使用nginx网站用户有:百度、京东、新郎、网易、腾讯、淘宝等,因其稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名

    Nginx与Apache的区别:

    【1】Nginx相对于Apache的优点:

    轻量级,同样是web 服务,比apache占用更少的内存及资源
    抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的,在高并发下nginx能保持低资源低消耗高性能
    高度模块化的设计,编写模块相对简单
    社区活跃,各种高性能模块出品迅速
    【2】Apache相对于Nginx的优点:

    rewrite功能比nginx的rewrite强大
    模块超多,基本想到的都可以找到
    少bug,nginx的bug相对较多
    超稳定
    Apache是通过libphp5.so这个模块来处理php文件;Nginx是通过php-fpm这个服务来处理php文件;Apache的libphp5.so是随着Apache服务器一起运行,而Nginx和php-fpm是各自独立运行,所以在运行过程中,Nginx和php-fpm都需要分别启动;Nginx处理动态请求是弱项,一般动态请求要Apache去做,Nginx只适合处理静态页面或反向代理;一般来说,需要性能的web服务器用Nginx,如果不需要性能只求稳定就选Apache

    LVS是四层负载,也就是基于传输层信息进行调度(传输层协议TCP/UDP)七层负载中比较出名就是haproxy与nginx,也就是基于应用层信息就行调度(应用层协议:HTTP等)。

    七层可以做重定向,比如下图访问淘宝地址www.taobao.com,可以看到http状态码为301表示重定向,重定向后的地址为 https://www.taobao.com/,端口为443端口,但用户通过浏览器上过去的都是标准的80端口,也就是做了重定向。现在电商都是做的整站加密。
     

    二,Nginx的安装

    1. [root@nodea ~]# tar zxf nginx-1.21.6
    2. [root@nodea ~]# cd nginx-1.21.6/
    3. [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 进行编译
    4. [root@nodea nginx-1.21.6]# yum install -y gcc
    5. [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
    6. [root@nodea nginx-1.21.6]# yum install pcre-devel
    7. [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
    8. [root@nodea nginx-1.21.6]# yum install -y openssl-devel
    9. [root@nodea nginx-1.21.6]# make
    10. [root@nodea nginx-1.21.6]# make install生成配置目录
    11. [root@nodea nginx-1.21.6]# cd /usr/local/nginx/
    12. [root@nodea sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ 做软链接

    1. [root@nodea conf]# nginx -t #检测语法
    2. [root@nodea conf]# nginx #启动nginx
    3. [root@nodea conf]# ps ax

    1. [root@nodea conf]# nginx -s stop #关闭nginx
    2. [root@nodea conf]# nginx -s reload #重启nginx

    三,Nginx的优化

    1. [root@nodea nginx]# cd ..
    2. [root@nodea local]# rm -rf nginx/
    3. [root@nodea ~]# cd nginx-1.21.6/
    4. [root@nodea nginx-1.21.6]# vim auto/cc/gcc
    5. [root@nodea nginx-1.21.6]# make clean
    6. [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
    7. [root@nodea nginx-1.21.6]# make
    8. [root@nodea nginx-1.21.6]# make install
    9. [root@nodea conf]# vim nginx.conf
    10. [root@nodea conf]# nginx
    11. [root@nodea conf]# nginx -s reload

     

      测试:

    当其中一台的httpd服务暂停后 

    四,nginx并发优化

    1. [root@nodea ~]# cd /usr/local/nginx/conf
    2. [root@nodea conf]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx 创建用户不给登陆权限
    3. [root@nodea conf]# vim nginx.conf

    1. [root@nodea conf]# nginx -s reload
    2. [root@nodea conf]# lscpu 查看cpu命令
    3. [root@nodea conf]# sysctl -a | grep file 查看内核数据
    4. [root@nodea conf]# ulimit -a 查看操作系统数据
    5. [root@nodea conf]# vim /etc/security/limits.conf设定系统并发量

    1. [root@nodea conf]# usermod -s /bin/bash nginx
    2. [root@nodea conf]# su - nginx
    3. -bash-4.2$ ulimit -a
    4. [root@nodea conf]# usermod -s /sbin/nologin nginx
    5. [root@nodea conf]# nginx -s reload

     五,nginx平滑升级及回退

    平滑升级:不需要关闭当前的服务,用户端是感觉不到的。

    1.升级:

    下载 nginx 新版本软件,正常执行./configure 和make 但不要执行make install

    1. 1).下载与编译
    2. [root@nodea ~]# tar zxf nginx-1.22.0.tar.gz
    3. [root@nodea ~]# cd nginx-1.22.0
    4. [root@nodea nginx-1.22.0]# vim auto/cc/gcc 注销debug
    5. [root@nodea nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
    6. [root@nodea nginx-1.22.0]# make
    7. 注:
    8. (1). 此处只用 make 编译就可以,不能做 make install ,因为 make install会将之前的信息覆盖;
    9. (2). 此处之前如果没有关闭 debug ,要先执行 make clean ,然后关闭 debug ,再编译再 make
    10. [root@nodea nginx-1.22.0]# cd objs
    11. [root@nodea objs]# ./nginx -v 查看版本
    12. [root@nodea objs]# curl -I localhost查看正在运行的版本

     

    1. 2).备份之前的数据
    2. [root@nodea objs]# cd /usr/local/nginx/sbin/
    3. [root@nodea sbin]# cp nginx nginx.old 备份nginx1.21.0版本数据
    1. 3).复制新版本
    2. [root@nodea sbin]# cd -
    3. /root/nginx-1.22.0/objs
    4. [root@nodea objs]# \cp -f nginx /usr/local/nginx/sbin/nginx强制用新版本信息覆盖老版本
    5. [root@nodea objs]# ps ax | grep nginx 查看当前版本所开启的进程号端口信息;

    1. 4.)升级新程序
    2. [root@nodea objs]# kill -USR2 16114 开启接管16114的新进程
    3. [root@nodea objs]# ps ax | grep nginx
    4. [root@nodea objs]# kill -WINCH 16114 结束16114的worker
    5. [root@nodea objs]# ps ax | grep nginx

     

    1. 版本回退:
    2. [root@nodea objs]# cd /usr/local/nginx/
    3. [root@nodea nginx]# cd sbin
    4. [root@nodea sbin]# \cp -f nginx.old nginx将1.21.0强制覆盖1.22.0
    5. [root@nodea sbin]# kill -HUP 16114 来唤醒原进程时直接用来开启子进程
    6. [root@nodea sbin]# ps ax | grep nginx
    7. [root@nodea sbin]# kill -WINCH 56581 结束新进程退回之前版本
    8. [root@nodea sbin]# ps ax | grep nginx
    9. [root@nodea sbin]# kill -9 56581
    10. [root@nodea sbin]# ps ax | grep nginx

     

     六,nginx 负载均衡

    1.负载均衡的权重

    1. 1.负载均衡的权重
    2. 编辑配置文件
    3. [root@nodea sbin]# cd ..
    4. [root@nodea nginx]# cd conf
    5. [root@nodea conf]# vim nginx.conf修改权重为2

    1. [root@nodea conf]# nginx -s reload
    2. [root@nodea conf]# for i in {1..10}; do curl 192.168.0.7;done

     2.调度不到负载均衡上时显示本机的页面
    修改配置文件内容当负载均衡出现问题时,返回本机的 8080 端口,显示本机的发布页面;

    1. [root@nodea conf]# yum install httpd -y
    2. [root@nodea conf]# vim /etc/httpd/conf/httpd.conf
    3. [root@nodea conf]# systemctl start httpd
    4. [root@nodea conf]# vim nginx.conf
    5. [root@nodea conf]# nginx -s reload
    6. [root@nodea conf]# cd /var/www/html
    7. [root@nodea html]# echo nodea backup > index.html
    8. [root@nodea html]# curl 192.168.0.7

     

     七,nginx 算法

    1).ip_hash 算法(根据客户端ip进行负载均衡,相同客户端IP访问时,会访问到同一客户端。一对一,一个sever对一个IP。

    编辑配置文件:

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

    基于 IP 的算法,当访问 IP 不变时,调度不变;

     该算法仍然有负载均衡,当客户端匹配的后台服务器挂掉时,会进行负载均衡调度到其他服务器,当该服务器恢复后,还会匹配到原服务器。

    2).sticky-cookie

    nginx 也可以做 cdn,但时 ip_hash 就不再合适,因为 ip_hash 解析的可能都是 cdn 的地址;

    那么就要用到别的算法,此处改为sticky-cookie;

    当用户在访问时,server 会给每个客户端返回一个 cookie 值; 会在浏览器中缓存,只要不关网页,当再次访问 server 时,会匹配 server 值做响应。

    1. [root@nodea ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42\(1\).zip
    2. [root@nodea ~]# cd nginx-1.21.6/
    3. [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
    4. [root@nodea nginx-1.21.6]# make
    5. 注:不能 make install ,需要平滑加载。
    6. 编译完成之后,需要停掉之前的 nginx,将新编译出来的 nginx 启动脚本移动到 /usr/local/nginx/sbin/ 中覆盖之前的 nginx;
    7. [root@nodea objs]# \cp -f nginx /usr/local/nginx/sbin/
    8. [root@nodea conf]# vim nginx.conf

     [root@nodea conf]# nginx -t语法检测

    [root@nodea conf]# nginx -s reload

     八,nginx限流

    1.控制单 IP 并发连接数

    1).编辑配置文件内容:

    [root@nodea conf]# vim nginx.conf

     

    2).在默认发布目录 /usr/local/nginx/html 里新建一个目录目录,再放个图片;然后重新加载配置文件的信息;

    [root@nodea nginx]# cd html

    [root@nodea html]# mkdir download

    [root@nodea download]# cp /root/vim.jpg /usr/local/nginx/html/download/

     

    3).测试:
    做压力测试:

    [root@localhost ~]# ab -n 10 -c 10 http://192.168.0.7/download/vim.jpg

     

    [root@nodea nginx]# cd logs

    [root@nodea logs]# cat access.log  查看运行日志

     

    4).由于我们在配置文件中对其并发做了限制,所以会有错误,如果此时将并发量更改为1,此时再次测试时便不会在有错误;

    [root@localhost ~]# ab -n 10 -c 1 http://192.168.0.7/download/vim.jpg

     

    5).编辑主配置文件,限制单位时间内的请求数目,以及速度限制:

    [root@nodea conf]# vim nginx.conf

     

    6).测试:此时在测试时,可以看到只有一个测试成功,其他的全部错误;因为 1s 只放行一个,其他的都拒绝;

     

    7).再次修改配置文件,一次处理一个时,其他的让其派对等候,并设置队列长度;

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

     

    8).再次测试,此时虽然没有错误,都会处理;但是默认还是 1s 一个,此时要时队列比较长,那么后端就需要增加服务来处理其请求;

     

    9).再次修改文件 ,nodelay 不要延迟;

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

     

    10).测试,此时速度很快,它只会处理消息对列中的数量,其他的就不再等待。

     

    11).编辑主配置文件,限制速度;

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

     

    12).测试
    一个并发,十个请求,此时两个请求需要10s 左右的速度;

     

     九,nginx 配置管理

    • 自动索引:下载方便;如我们在发布目录中放的文件,默认没有索引,是不能被访问到的;
    1. .修改配置文件,加入自动索引的参数;

    重新加载之后,此时再次访问发布页面时会看到索引。

     

    • Nginx expire 缓存配置: 缓存可以降低网站带宽,加速用户访问;

     

    • 日志轮询:

    1).查看日志量

     

    2).用脚本来保存日志;

    [root@nodea logs]# vim /opt/nginxlog.sh 进行日志重载

    [root@nodea logs]# chmod +x /opt/nginxlog.sh

    [root@nodea logs]# /opt/nginxlog.sh 执行脚本

     

    3).最好用 crontab 来定时管理日志任务
    当前为了测试,直接调用脚本来观察效果:为了安全,日志目录不需要给你 nginx 用户访问权限

    [root@nodea logs]# crontab -e

      

    • 禁用不必要的日志记录,以节省磁盘IO的消耗;

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

     

     

    • 站点目录和文件的限制

    [root@nodea conf]# vim nginx.conf只允许本机访问

    [root@nodea conf]# nginx -s reload

     

    • 中文乱码

     [root@nodea html]# vim index.html

     

    修改配置文件,识别中文:放在最外层,针对所有的都生效;

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

      

    十,nginx 重定向 

    • 防止域名恶意解析到服务器 IP:

    [root@nodea conf]# vim nginx.conf

    [root@nodea conf]# nginx -s reload

     

    • 也可以重定向:

     

     

    测试:

     

      

    • 80重定向443:

     

    客户端测试 

     

    • www.linux.org/bbs 重定向 bbs.linux.org:
      网站的扩容,站点太大,分出很多的模块;每个模块有自己的指定域名;

    [root@nodea conf]# mkdir /bbs

    [root@nodea conf]# echo bbs page > /bbs/index.html

    [root@nodea conf]# nginx -t

    [root@nodea conf]# nginx -s reload

    实验一:www.westos.org/bbs 重定向bbs.westos.org
    nginx配置文件,书写重定向策略:

    客户端进行测试:

     实验二:bbs.westos.org 重定向www.westos.org/bbs
    不指定发布页时,将按照设置访问默认发布目录下的默认发布页

     客户端进行测试:

     

    十一,nginx 防盗链 

     

    当进行盗链时,通过重定向让盗链访问指定图片

     

     

     

     

      

  • 相关阅读:
    Icon闪烁/设定蜂鸣器响的次数
    8个免费高清图片素材网站,再也不用担心侵权了。
    纽约时报个人叙事赛及纽约时报学生评论赛选哪个?
    如何在matlab绘图的标题中添加变量?变量的格式化字符串输出浅析
    uniapp实现防抖搜索
    【51单片机实验笔记】LED篇(二)多色LED的基本控制
    Open3d 使用marching cubes生成3D模型
    centos 安装svn
    PDF文件超出上传大小?三分钟学会PDF压缩
    Web前端开发技术课程大作业: 关于美食的HTML网页设计——HTML+CSS+JavaScript在线美食订餐网站html模板源码30个页面:
  • 原文地址:https://blog.csdn.net/z17609273238/article/details/126347045