一、nginx介绍
1、作用
- 配置web服务
- 反向代理、负载均衡
3、nginx高效的原因
- 基于异步非阻塞/异步IO模型
- 异步、同步
- 异步速度快
- 非阻塞、阻塞
- 阻塞
- 进程必须等待磁盘IO完成
- 非阻塞
- 进程在等待磁盘IO的同时,可以处理其他事务
- 阻塞
- 异步、同步
- 基于epoll模型设计的
- select
- 周期性询问, 限制最大文件数1024
- poll
- 周期性询问,取消最大文件数的限制
- epoll
- 通知机制
- select
二、nginx安装部署
1、下载nginx安装包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
2、安装依赖
[root@localhost ~]# yum install -y gcc openssl-devel pcre-devel zlib-devel
3、编译安装nginx
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0/
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.18.0]# make && make install
三、 nginx相关文件目录
- nginx安装目录/conf
- 配置文件 nginx.conf主配置文件
- nginx安装目录/logs
- 存放日志
- nginx安装目录/html
- 默认网页目录
- nginx安装目录/sbin
- 二进制文件
四、nginx启动管理
1、启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9726/nginx: master
[root@localhost ~]# ps -elf | grep nginx
1 S root 9726 1 0 80 0 - 11499 sigsus 15:56 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx 9727 9726 0 80 0 - 11612 ep_pol 15:56 ? 00:00:00 nginx: worker process
- master process 主进程
- 派生子进程、记录日志、重新加载配置文件
- worker process 工作进程
- 接收、处理客户端访问请求
2、设置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
3、停止nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
4、nginx重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
5、检测配置文件语法
[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
6、查看nginx版本
[root@localhost ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
7、查看nginx的配置参数
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module