一、Nginx的介绍
Nginx(engine X) 是一款高性能、高并发的提供web服务的管理软件,在硬件设备一定的情况下可以支持上万条并发,25000个非活跃的连接数只需要占用系统资源3-5M,非常的节省系统资源,nginx支持热部署可以7X24小时不停机更新,本身是一个模块化的软件功能丰富,nginx本身处理动态请求的能力比较弱,通常使用反向代理功能将动态请求转交给后端服务器进行处理,自身处理静态页面,不仅实现了动静分离提高业务的运行速度的同时还隐藏了后端真实服务器的IP地址,提高了安全性。当公司需要更换域名的时候,只需要使用rewrite模块做一个重定向就可以让用户访问旧的域名访问到新的域名,upstream模块可以实现负载均衡的效果,只需要增删节点就可以管理,nginx还支持平滑升级,这些优势都大大降低了运维架构的复杂度。
二、Nginx高效的原因
- 基于异步非阻塞/异步IO模型
- 异步、同步
- 异步速度快
- 非阻塞、阻塞
- 阻塞
- 进程必须等待磁盘IO完成
- 非阻塞
- 进程在等待磁盘IO的同时,可以处理其他事务
- 基于epoll模型设计的
- select
- 周期性询问, 限制最大文件数1024
- poll
- 周期性询问,取消最大文件数的限制
- epoll
- 通知机制
三、nginx的安装部署
nginx官方网站: nginx.org
1、进入官网,复制链接,下载nginx安装包
[root@ecs-ab0f ~]# wget http://nginx.org/download/nginx-1.8.1.tar.gz
[root@ecs-ab0f ~]# tar -xf nginx-1.8.1.tar.gz //解压
2、安装nginx的依赖
[root@ecs-ab0f ~]# yum install -y gcc pcre-devel openssl-devel zlib-devel
3、编译安装nginx
[root@ecs-ab0f ~]# cd nginx-1.8.1 //进入解压好的目录
[root@ecs-ab0f nginx-1.8.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[root@ecs-ab0f nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module //指定安装目录 安装stub_status模块
安装过程中......
checking for OS
+ Linux 3.10.0-1160.108.1.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found[root@ecs-ab0f nginx-1.8.1]# make && make install
编译完成后会显示下面信息
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/root/nginx-1.8.1”
4、nginx的安装目录介绍
- nginx安装目录/conf
- 配置文件 nginx.conf主配置文件
- nginx安装目录/logs
- 存放日志
- nginx安装目录/html
- 默认网页目录
- nginx安装目录/sbin
- 二进制文件
5、nginx启动管理
[root@ecs-ab0f ~]# /usr/local/nginx/sbin/nginx
[root@ecs-ab0f ~]# netstat -tunlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12848/nginx: master
- master process 主进程
- 派生子进程、记录日志、重新加载配置文件
- worker process 工作进程
- 接收、处理客户端访问请求
1、设置nginx开机自启动
[root@localhost ~]# sed -ri '$a \/usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local
[root@localhost ~]# chmod a+x /etc/rc.d/rc.local
停用nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
2、nginx重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
3、检测配置文件语法
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、查看nginx版本
[root@ecs-ab0f ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.8.15、查看nginx配置参数
[root@ecs-ab0f ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module
重启Nginx报错
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
原因是:端口冲突,原因是现有的httpd开着服务,占用着80端口,所以nginx无法启动