1、nginx的并发优化
问题:nginx为啥比apache快??下去了解
2、nginx平滑升级
[root@server1 ~]# nginx -v 当前安装版本1.8
nginx version: nginx/1.18.0
[root@foundation50 lamp]# scp nginx-1.19.1.tar.gz server1:/mnt 将已经下载的nginx1.19版本拷贝到server1上
[root@server1 ~]# cd /mnt/
[root@server1 mnt]# tar zxf nginx-1.19.1.tar.gz 解压
[root@server1 mnt]# cd nginx-1.19.1/ 进入nginx解压后的 目录
[root@server1 nginx-1.19.1]# vim auto/cc/gcc 注释到debug
如何硬藏nginx版本??
[root@server1 nginx-1.19.1]# cd src/core/
[root@server1 core]# vim nginx.h 编辑
[root@server1 nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
执行 ,注意:执行的参数和1.18的版本要一致,1.19没有加stick模块,1.18加了此模块,为了参数一致并能启动nginx,编辑nginx配置文件注释掉stick参数
[root@server1 sbin]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
[root@server1 nginx-1.19.1]# make 编译
[root@server1 nginx-1.19.1]# make install 安装
[root@server1 nginx-1.19.1]# cd objs/
[root@server1 objs]# du -h nginx 可以看出关掉debug后,编译大小只有916k
916K nginx
[root@server1 conf]# nginx 启动成功
[root@server1 objs]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx nginx.old
[root@server1 sbin]# rm -fr nginx.old 删除生成的nginx.old
[root@server1 sbin]# cp nginx nginx.old 把nginx备份一份,起名为nginx.old
[root@server1 sbin]# cd /mnt/nginx-1.19.1/
[root@server1 nginx-1.19.1]# cd objs/
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/ -f表示强制 ,将编译的1.19版本的nginx拷贝到/usr/local/nginx/sbin/
覆盖掉老版本1.18编译的nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
[root@server1 ~]# nginx -v
nginx version: nginx/1.19.1 可以发现版本已经覆盖了
[root@server1 objs]# ps ax | grep nginx 查看nginx的master进程号
6801 ? Ss 0:00 nginx: master process nginx
6802 ? S 0:00 nginx: worker process
6803 ? S 0:00 nginx: worker process
6805 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 objs]# kill -USR2 6801 (主进程) 将新的nginx启动起来
[root@server1 objs]# ps ax | grep nginx
6801 ? Ss 0:00 nginx: master process nginx
6802 ? S 0:00 nginx: worker process
6803 ? S 0:00 nginx: worker process
6822 ? S 0:00 nginx: master process nginx
6823 ? S 0:00 nginx: worker process
6824 ? S 0:00 nginx: worker process
6826 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# kill -WINCH 6801 下线老版本nginx,关闭老版本进程
[root@server1 sbin]# ps ax | grep nginx 可以发现关闭了老版本子进程,保留了老版本主进程,是为了回退
6801 ? Ss 0:00 nginx: master process nginx
6822 ? S 0:00 nginx: master process nginx
6823 ? S 0:00 nginx: worker process
6824 ? S 0:00 nginx: worker process
7034 pts/0 R+ 0:00 grep --color=auto nginx
3、 nginx版本回退
[root@server1 sbin]# ls
nginx nginx.old
[root@server1 sbin]# mv nginx nginx.new 将新的nginx改名
[root@server1 sbin]# ls
nginx.new nginx.old
[root@server1 sbin]# mv nginx.old nginx 将老版本nginx.old 名字改回来为nginx
[root@server1 sbin]# ls
nginx nginx.new
[root@server1 sbin]# kill -HUP 6801 唤醒老的进程
[root@server1 sbin]# ps ax | grep nginx 可以发现老的子进程又出来了
6801 ? Ss 0:00 nginx: master process nginx
6822 ? S 0:00 nginx: master process nginx
6823 ? S 0:00 nginx: worker process
6824 ? S 0:00 nginx: worker process
7123 ? S 0:00 nginx: worker process
7124 ? S 0:00 nginx: worker process
7159 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 conf]# kill -WINCH 6822 关闭新的主进程6822
[root@server1 conf]# kill 6822 杀掉进程
[root@foundation50 Desktop]# curl -I 172.25.50.1
4、nginx限制并发
[root@server1 download]# nginx -s reload 重载服务
[root@server1 nginx]# cd html/ 进入默认发布目录
[root@server1 html]# mkdir download 建立download目录
[root@foundation50 Desktop]# cd /mnt/pub/docs/
[root@foundation50 docs]# scp vim.jpg server1:/usr/local/nginx/html/download 拷贝图片到download目录
root@server1's password:
vim.jpg 100% 443KB 18.1MB/s 00:00
访问172.25.50.1/download/vim.ipg 访问成功
测试:
[root@foundation50 docs]# ab -c10 -n10 http://172.25.50.1/download/vim.jpg 压力测试,
设置了1个并发,但是请求了10个并发,所以其余的失败了
如果请求1个并发
[root@foundation50 docs]# ab -c1 -n10 http://172.25.50.1/download/vim.jpg
5、nginx限流
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf 编辑配置文件
[root@server1 conf]# nginx -s reload 重载服务
[root@foundation50 docs]# ab -c1 -n10 http://172