一.Nginx的概述
- Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx稳定性高,而且系统资源消耗少
- Nginx相对于Apache主要处理静态请求,而apache主要处理动态请求
- Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
二.Nginx的安装
- Nginx安装文件可以从官方网站 http://www.nginx.org/ 下载
- 安装运行nginx的运行环境
#下载依赖的软件
[root@nginx1 ~]# mkdir -p /apps/nginx
[root@nginx1 ~]# yum -y install gcc pcre-devel openssl-devel zlib-devel
[root@nginx1 ~]# useradd -s /sbin/nologin nginx
[root@nginx1 ~]# cd /usr/local/src/
[root@nginx1 src]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
[root@nginx1 src]# ls
nginx-1.18.0.tar.gz
[root@nginx1 src]# tar xf nginx-1.18.0.tar.gz
[root@nginx1 src]# cd nginx-1.18.0/
[root@nginx1 nginx-1.18.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#make编译 make install安装
[root@nginx1 nginx-1.18.0]# make && make install
[root@nginx1 nginx-1.18.0]# chown -R nginx.nginx /apps/nginx
[root@nginx1 nginx-1.18.0]# ll /apps/nginx
总用量 0
drwxr-xr-x 2 nginx nginx 333 1月 13 13:34 conf
drwxr-xr-x 2 nginx nginx 40 1月 13 13:34 html
drwxr-xr-x 2 nginx nginx 6 1月 13 13:34 logs
drwxr-xr-x 2 nginx nginx 19 1月 13 13:34 sbin
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心的配置文件,其中.conf则是用来配置nginx相关的功能,
html:目录中保存nginx服务器的web文件,但是可以更改为其他目录保存web文件
logs:用来保存nginx服务器的访问日志错误日志等
sbin:保存nginx二进制启动脚本,可以接受不同的参数实现不同的功能
- 验证版本及编译参数
[root@nginx1 nginx-1.18.0]# ls /apps/nginx/sbin
nginx
[root@nginx1 nginx-1.18.0]# ln -s /apps/nginx/sbin/nginx /usr/bin
[root@nginx1 nginx-1.18.0]# nginx -v
nginx version: nginx/1.18.0
[root@nginx1 nginx-1.18.0]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
- 使用下列的命令控制nginx
nginx -t //检查
nginx //启动
killall -1 nginx //重启
killall -3 nginx //停止
三.服务控制的优化
- 主程序Nginx支持标准的进程信号,通过kill或者killall命令发送HUP信号表示重载配置,发送QUIT信号表示退出进程,发送KILL信号表示杀死进程
kill -s HUP nginx //-s HUP等同于 -1,表示重载
kill -s QUIT nginx //-s QUIT等同于 -3,表示停止
- systemctl工具来控制Nginx服务脚本
#复制同一版本的nginx的yum安装生成的service文件
[root@nginx1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server //描述
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target //描述服务类型
Wants=network-online.target
[Service]
Type=forking //后台形式运行
PIDFile=/apps/nginx/run/nginx.pid //PID文件位置
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf //启动服务
ExecReload=/bin/kill -s HUP $MAINPID //根据PID重载
ExecStop=/bin/kill -s TERM $MAINPID //根据PID关闭
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
#创建Pid文件存放目录
[root@nginx1 ~]# mkdir /apps/nginx/run/
[root@nginx1 ~]# vim /apps/nginx/conf/nginx.conf
#修改配置文件
pid /apps/nginx/run/nginx.pid;
[root@nginx1 ~]# systemctl daemon-reload
[root@nginx1 ~]# systemctl enable --now nginx
[root@nginx1 ~]# ll /apps/nginx/run
总用量 4
-rw-r--r-- 1 root root 5 1月 13 14:17 nginx.pid
四.nginx的配置文件
- Nginx的服务器中的主配置文件 /apps/nginx/conf/nginx.conf 中包括全局配置、I/O事件配置和HTTP配置
- 全局配置,由各种配置语句组成,不使用特定的界定标记。全局配置部分包括Nginx服务的运行用户、工作进程数、错误日志、PID存放位置等基本设置
#user nobody; //运行的用户
worker_processes 1; //工作的进程数 ,取决于服务器的cpu
#error_log logs/error.log; //错误日志文件的位置
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /apps/nginx/run/nginx.pid; //PID文件的位置
- I/O事件配置,使用“event{ }”界定标记,用来指定Nginx进程的I/O响应模型、每个进程的连接数等设置。对于2.6及以上的版本的内核,建议使用epoll模型以提高性能;每个进程的连接数根据实际需要来定,一般在10000以下(默认为1024)
events {worker_connections 1024; //每个进程处理1024个连接
}
//Nginx能提供服务的连接数为(工作进程数X连接数,即1*1024=1024)
- HTTP配置,使用“http{ }”界定标记,包括访问日志、http端口、网页目录、默认字符集、连接保持,以及后面要讲到的虚拟web主机、PHP解析等一系列设置,其中大部分配置语句包含在server{}中。
http {include mime.types;default_type application/octet-stream;sendfile on; //支持文件下载#tcp_nopush on; //保持连接超时时间#keepalive_timeout 0; keepalive_timeout 65;server {listen 80; //监听地址以及端口server_name localhost;location / {root html; //网站根目录的位置index index.html index.htm; //默认首页}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html; //内部错误的反馈页面location = /50x.html { //错误页面配置root html;}
}
五.访问状态统计
-
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的web访问情况,配置编译参数时–with_http_stub_statius_module来启用此模块支持,使用nginx -V来查看Nginx中是否包含该模块
-
要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf 配置文件,到指定访问位置并添加stub_status配置代码
location /status {stub_status on; ##开启访问状态统计功能access_log off; ##关闭日志记录}
- 重启服务
[root@nginx1 ~]# systemctl restart nginx.service
验证结果
[root@nginx1 ~]# curl 10.1.1.161/status
Active connections: 1 //当前的连接数
server accepts handled requests //表示已处理的连接数1 1 1 //已处理的连接数 //成功的TCP握手次数 //已处理的请求数
Reading: 0 Writing: 1 Waiting: 0