Nginx

Nginx

介绍

简介

官网:http://nginx.org/

在这里插入图片描述

  • Nginx是俄罗斯访问量第二的rambler.ru站点设计开发的,从2004年发布至今,凭借开源的力量, 已经接近成熟与完善
  • Nginx功能丰富,可作为HTTP(web)服务器,也可作为反向代理服务器邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能,并且支持很多第三方的模块扩展,可实现负载均衡功能

特点(优点)

  • 高可靠:稳定性高,一个master多个worker,master进程用于管理调度请求分发到哪一个worker进程,worker进程用于响应请求。当其中一个worker进程出现问题,master会调度其他worker进程
  • 热部署 :(1)平滑升级 ;(2)可以快速重载配置
  • 高并发:可以同时响应更多的请求 ,达到几万的并发量,基于事件epoll模型 (异步网络IO模型)
  • 响应快:尤其在处理静态文件上,响应速度很快 ,内核基于sendfile机制
  • 低消耗:cpu和内存占用低,建立开销1w个请求 ,内存只消耗2-3MB
  • 分布式支持 :反向代理,七层负载均衡
  • 跨平台:在多种系统平台都可以部署

工作原理

  • Nginx由内核一系列模块组成

  • 内核提供web服务的基本功能,如启用网络协议,创建运行环境,接收和分配客户端请求,处理模块之间的交互

  • Nginx的各种功能和操作都由模块来实现,Nginx的模块从结构上分为核心模块基础模块第三方模块。

    • 核心模块: HTTP模块、EVENT模块和MAIL模块
    • 基础模块: HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模
    • 第三方模块: HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块及用户自己开发的模块

安装与升级

安装

[root@server1 mysql]# cd /root/soft/
[root@server1 soft]# rz
#传入下载好的 nginx-1.14.2软件包,便于后续高版本升级
[root@server1 soft]# ls
mysql-5.6.33  mysql-5.6.33.tar.gz  nginx-1.14.2.tar.gz
[root@server1 soft]# vim nginx_install.sh

#!/bin/bash
#编译安装Nginx
nginx_install(){
   
#创建软件运行用户
`id www` &>>/dev/null
if [ $? -ne 0 ];then
useradd -s/sbin/nologin -M www
fi
#安装依赖(前面安装MySQL已经安装了很多依赖库)
yum -y install pcre-devel zlib-devel openssl-devel
#编译安装
cd /root/soft
tar xvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
nginx_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'

[root@server1 soft]# chmod +x nginx_install.sh
[root@server1 soft]# ./nginx_install.sh

服务配置
[root@server1 soft]# cd /etc/init.d/
[root@server1 init.d]# rz
#传入已经下载的nginx脚本
[root@server1 init.d]# chmod +x nginx 

[root@server1 conf]# vim /etc/rc.d/init.d/nginx

重装和升级

原因

在实际业务场景中,需要使用软件新版本的功能、特性。就需要对原有软件进行升级或者重装操作,一般会装最新的稳定版

信号控制Nginx
Kill 信号  pid
注意:信号全部发送给master进程
信号 说明 备注
TERM, INT 强制退出 -s stop
QUIT 执行完当前的请求后优雅退出 -s quit
HUP 平滑重载配置文件 -s reload
USR1 重新打开日志文件 -s reopen
USR2 平滑升级nginx二进制文件
WINCH 优雅关闭worker进程
案例
[root@server01 ~]# ps -ef |grep nginx
root      34295      1  0 Oct02 ?        00:00:00 nginx: master process ./nginx
www       69456  34295  0 06:11 ?        00:00:00 nginx: worker process
root      69913  69708  0 10:30 pts/1    00:00:00 grep --color nginx
[root@server01 ~]# kill -QUIT 34295
[root@server01 ~]# ps -ef |grep nginx
root      69915  69708  0 10:31 pts/1    00:00:00 grep --color nginx
[root@server01 ~]# service nginx start
正在启动 nginx:                                           [确定]
重装
  • 停掉服务
  • 备份配置文件和网站目录里的资源文件
  • 删除编译的安装的软件包和源码包
  • 重新解压编译安装即可
平滑升级
  • 方法1
解压软件包
[root@server01 ~]# rz

[root@server01 ~]# tar -xzf nginx-1.20.1.tar.gz 
[root@server01 ~]# cd nginx-1.20.1

查看之前版本配置参数
[root@server01 nginx-1.20.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module

复制之前参数,再次配置
[root@server01 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
[root@server01 nginx-1.20.1]# make && make install

操作完成之后,会把原来的旧版本备份为nginx.old
[root@server01 nginx-1.20.1]# cd /usr/local/nginx/sbin/
[root@server01 sbin]# ls
nginx  nginx.old
[root@server01 sbin]# ./nginx -v
nginx version: nginx/1.20.1
[root@server01 sbin]# ./nginx.old -v
nginx version: nginx/1.14.2

新旧版本同时运行
[root@server01 sbin]# ps -ef|grep nginx
root      69995      1  0 10:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       69997  69995  0 10:32 ?        00:00:00 nginx: worker process                                          
root      72741  69708  0 11:26 pts/1    00:00:00 grep --color nginx
[root@server01 sbin]# cat ../logs/nginx.pid 
69995
[root@server01 sbin]# kill -USR2 69995
[root@server01 sbin]# ps -ef|grep nginx
root      69995      1  0 10:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       69997  69995  0 10:32 ?        00:00:00 nginx: worker process                                          
root      72743  69995  0 11:28 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       72744  72743  0 11:28 ?        00:00:00 nginx: worker process                                          
root      72746  69708  0 11:28 pts/1    00:00:00 grep --color nginx

停止掉旧进程
#kill -WINCH 优雅的关闭的子进程,再关闭旧的主进程
[root@server01 sbin]#  kill -WINCH 69995
[root@server01 sbin]#  kill -QUIT 69995
[root@server01 sbin]# ps -ef|grep nginx
root      72743      1  0 11:28 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       72744  72743  0 11:28 ?        00:00:00 nginx: worker process                                          
root      72748  69708  0 11:32 pts/1    00:00:00 grep --color nginx
  • 方法2
在nginx中,默认提供了平滑升级的操作
[root@server01 sbin]# cd /root/nginx-1.20.1
[root@server01 nginx-1.20.1]# vim Makefile
 18 upgrade:
 19     /usr/local/nginx/sbin/nginx -t
 20 
 21     kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
 22     sleep 1
 23     test -f /usr/local/nginx/logs/nginx.pid.oldbin
 24 
 25     kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

平滑升级
[root@server01 nginx-1.20.1]# make install && make upgrade

文件配置

配置文件介绍

[root@server01 nginx-1.20.1]# cp /usr/local/nginx/conf/nginx.conf{,.bak}
[root@server01 nginx-1.20.1]# cd /usr/local/nginx/
[root@server01 nginx]# grep -Ev "#|^$" conf/nginx.conf.bak > conf/nginx.conf

[root@server01 nginx]# cat conf/nginx.conf
#user  nobody;	nginx子进程启动用户
w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值