目录
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端口,也就是做了重定向。现在电商都是做的整站加密。
- [root@nodea ~]# tar zxf nginx-1.21.6
- [root@nodea ~]# cd nginx-1.21.6/
- [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 进行编译
- [root@nodea nginx-1.21.6]# yum install -y gcc
- [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
- [root@nodea nginx-1.21.6]# yum install pcre-devel
- [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
- [root@nodea nginx-1.21.6]# yum install -y openssl-devel
- [root@nodea nginx-1.21.6]# make
- [root@nodea nginx-1.21.6]# make install生成配置目录
- [root@nodea nginx-1.21.6]# cd /usr/local/nginx/
- [root@nodea sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ 做软链接

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

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

- [root@nodea nginx]# cd ..
- [root@nodea local]# rm -rf nginx/
- [root@nodea ~]# cd nginx-1.21.6/
- [root@nodea nginx-1.21.6]# vim auto/cc/gcc
- [root@nodea nginx-1.21.6]# make clean
- [root@nodea nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
- [root@nodea nginx-1.21.6]# make
- [root@nodea nginx-1.21.6]# make install
- [root@nodea conf]# vim nginx.conf
- [root@nodea conf]# nginx
- [root@nodea conf]# nginx -s reload



测试:

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

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

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

- [root@nodea conf]# usermod -s /bin/bash nginx
- [root@nodea conf]# su - nginx
- -bash-4.2$ ulimit -a
- [root@nodea conf]# usermod -s /sbin/nologin nginx
- [root@nodea conf]# nginx -s reload

平滑升级:不需要关闭当前的服务,用户端是感觉不到的。
1.升级:
下载 nginx 新版本软件,正常执行./configure 和make 但不要执行make install
- 1).下载与编译
- [root@nodea ~]# tar zxf nginx-1.22.0.tar.gz
- [root@nodea ~]# cd nginx-1.22.0
- [root@nodea nginx-1.22.0]# vim auto/cc/gcc 注销debug
- [root@nodea nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
- [root@nodea nginx-1.22.0]# make
- 注:
- (1). 此处只用 make 编译就可以,不能做 make install ,因为 make install会将之前的信息覆盖;
- (2). 此处之前如果没有关闭 debug ,要先执行 make clean ,然后关闭 debug ,再编译再 make
- [root@nodea nginx-1.22.0]# cd objs
- [root@nodea objs]# ./nginx -v 查看版本
- [root@nodea objs]# curl -I localhost查看正在运行的版本

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

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

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

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

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

2.调度不到负载均衡上时显示本机的页面
修改配置文件内容,当负载均衡出现问题时,返回本机的 8080 端口,显示本机的发布页面;
- [root@nodea conf]# yum install httpd -y
- [root@nodea conf]# vim /etc/httpd/conf/httpd.conf
- [root@nodea conf]# systemctl start httpd
- [root@nodea conf]# vim nginx.conf
- [root@nodea conf]# nginx -s reload
- [root@nodea conf]# cd /var/www/html
- [root@nodea html]# echo nodea backup > index.html
- [root@nodea html]# curl 192.168.0.7


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 值做响应。
- [root@nodea ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42\(1\).zip
- [root@nodea ~]# cd nginx-1.21.6/
- [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
- [root@nodea nginx-1.21.6]# make
- 注:不能 make install ,需要平滑加载。
- 编译完成之后,需要停掉之前的 nginx,将新编译出来的 nginx 启动脚本移动到 /usr/local/nginx/sbin/ 中覆盖之前的 nginx;
- [root@nodea objs]# \cp -f nginx /usr/local/nginx/sbin/
- [root@nodea conf]# vim nginx.conf

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

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 左右的速度;
- .修改配置文件,加入自动索引的参数;
重新加载之后,此时再次访问发布页面时会看到索引。
![]()
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
![]()
[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
![]()
防止域名恶意解析到服务器 IP:
[root@nodea conf]# vim nginx.conf
[root@nodea conf]# nginx -s reload
![]()
测试:
![]()
![]()
客户端测试
[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
不指定发布页时,将按照设置访问默认发布目录下的默认发布页客户端进行测试:
当进行盗链时,通过重定向让盗链访问指定图片
![]()
![]()
![]()
![]()
![]()