这里写自定义目录标题
Nginx详解
一款高性能,轻量级web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30 000~50 000个并发请求
附加:一个进程可以接待4096个用户(左右)
Nginx 工作原理
Nginx 由内核和模块组成
Nginx 本身做的工作实际上很少,当它接到一个HTTP请求时,它仅仅是通过查找配置文件将此次请求映射到一个location block,而此location中所配置的各个指令则会启动不同的模块去完成工作,因此模块可以看做Nginx真正的劳动工作者。
通常一个location中的指令会涉及一个handler模块和多个filter模块(当然,多个location可以复用同一个模块)。handler模块负责处理请求,完成响应内容的生成,而filter模块对响应内容进行处理。用户根据自己的需要所开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx的功能才会如此强大。
Nginx的模块从结构上分为核心模块、基础模块和第三方模块:
核心模块:HTTP模块、EVENT模块和MAIL模块;
基础模块:HTTP Access 模块、HTTP FastCGI 模块、HTTP Proxy模块和HTTP Rewrite 模块;
第三方模块:HTTP Upstream Request Hash 模块、Notice 模块和HTTP Access Key 模块
模块之间的功能
Nginx的模块从功能上分为如下三类:
Handlers(处理器模块):此类模块直接处理请求,并直接输出内容和修改headers信息等操作。Handlers处理器模块一般只能有一个: Filters(过滤器模块):此类模块主要对其他处理器模块输出的内容进行修改操作,最后由Nginx输出; Proxies(代理类模块):此类模块是Nginx的HTTP Upstream 之类的模块,这些模块主要与后端一些服务比如FastCGl等进行交互,实现服务代理和负载均衡等功能。
Nginx的进程模型在工作方式上,Nginx分为单工作进程和多工作进程两种模式。
在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;
在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。
Nginx在启动后,会有一个master进程和多个worder进程
master进程主要用来管理worker进程,主要包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。master进程充当整个进程组与用户的交互接口,同时对进程进行监护。它不需要处理网络事件,不负责业务的执行,只会通过管理worker进程来实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。
Nginx+FastCGI运行原理
Nginx 不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。 wrapper为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper 可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后Fork(派生)出一个新的线程,这个线程调用解释器或者外部程序处理脚本并返回数据;接着wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后Nginx将返回的数据(html页面或者图片)发送给客户端。
效果图
Nginx模块单元介绍
ngx_http_access_module模块:实现基于ip的访问控制功能
ngx_http_auth_basic_module模块:实现基于用户的访问控制,使用basic机制进行用户认证
ngx_http_stub_status_module模块:用于输出nginx的基本状态信息
ngx_http_log_module模块:用指定的格式写入请求日志
ngx_http_gzip_module模块:用gzip算法来压缩数据可以节约带宽,默认nginx不压缩数据,但是压缩会消耗CPU资源,且压缩文本图像类效果较好,能达到30%左右,但压缩音频视频没有多大意义,因为本身音视频就是被压缩过的,很可能对音视频压缩反而会增大其体积
ngx_http_ssl_module模块:启用https时使用的模块
ngx_http_rewrite_module模块:重定向模块,可以将客户端的请求基于regex所描述的模式进行检查,而后完成替换。当旧的业务和新的业务不一样,网站更名了,就可以使用此模块。将访问旧的请求重定向成新的
ngx_http_referer_module模块:可以跟踪链接从哪里跳转过来的,该字段可以用来防止盗链
ngx_http_headers_module模块:向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
主配置文件修改
全局配置
#user nobody; ##指定用户,默认是匿名用户
worker_processes 1; ##工作进程,实现高并发可以增加值
#error_log logs/error.log; #错误日志文件
#pid logs/nginx.pid; ##pid文件
进程数配置
events { ##一个进程包含多个线程
use epoll; ##能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
worker_connections 4096; ##连接数4096基于线程数
}
http配置
介绍
http{}:协议层面 server{}:服务层面 location{}:网页站点目录层面
http {
access _loglogslaccess.log main;
sendfile on; ##发送邮件
keepalive_timeout 65; ##连接超时时间
server {
listen 80;
server_name www.bt.com; ##域名
charset utf-8; ##字符集
location / {
root html; ##网页站点目录名称
index index.html index.php;} ##主页的相对路径
error_page 500 502 503504 15ox.html; ##提示错误页面(50是服务器出错)
location=/50x.html {
root html;) }
}
}
Nginx的优化安装(创建快照)
安装支持软件
[root@serverl ~]# yum -y install pcre-devel zlib-devel
解压缩文件并进行编译安装
[root@serverl ~]# tar zxf nginx-1.12.0.tar.gz
[root@serverl ~]# cd nginx-1.12.2/
[root@serverl nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module //最后一段为统计状态模块
[root@serverl nginx-1.12.2]#make && make install
创建一个不可登录的程序用户
[root@serverl ~]# useradd -M -s /sbin/nologin nginx
nginx命令执行路径优化
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin
启动服务
nginx
查看nginx服务是否开启
[root@server1 ~]# netstat -anpt | grep nginx
[root@localhost ~]# killall -s QUIT nginx ###选项-s QUIT等于-3 停止服务
[root@localhost ~]# netstat -anpt | grep nginx
[root@localhost ~]# nginx
[root@localhost ~]# killall -s HUP nginx ###选项-s HUP等于-1 重新加载
[root@server1 ~]# netstat -anpt | grep nginx ###查看nginx服务是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 56577/nginx: master
附加:killall -1 nqinx 等同于killall -s HUP nginx 重新加载
killall -3 nqinx 等同于killall -s QUIT nginx停止服务
制作管理脚本,并执行
[root@server1 ~]# vi /etc/init.d/nginx ##制作管理脚本
#!/bin/bash
# chkconfig: 35 20 80
# description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
killall -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 satrt
;;
reload)
killall -s HDP $(cat $PIDF)
;;
*)
echo "Usage: $0 {satrt|stop|reload|status}"
exit1
esac
exit 0
[root@server1 ~]# chmod +x /etc/init.d/nginx
[root@server1 ~]# chkconfig --add nginx
[root@server1 ~]# systemctl start nginx
[root@server1 ~]# systemctl status nginx
Nginx的访问状态统计
修改主配置文件
[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
修改#user nobody为user nginx nginx ##两个nginx的意思 运行账户 组
error_log logs/error.log info ###去除#号
events {
use epoll; ###新增此行,默认使用select/poll
worker_connections 1024; ###表示一个工作进程运行1024进程
}
添加以下内容
}
location ~/status { ###配置统计功能
stub_status on;
access_log off;
} ###在server模块里的error_page上面增加
检查配置文件
[root@localhost ~]# 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
查看版本号及开启的模块
[root@localhost ~]# nginx -V ###查看版本号及开启的模块
20 nginx version: nginx/1.12.2
21 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
22 configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
重启服务器,并关闭防火墙和核心防护
[root@localhost ~]# systemctl start nginx ###开启nginx服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0 ###关闭防火墙和核心防护
验证(如果没有出现效果图,可以尝试重启服务机)
在客户端浏览器输入:http://20.0.0.11/status 验证
实验效果显示结果详解
active connection - 活跃的连接数量
server accepts handled requests —— 总共处理了n个连接,成功建立了n次握手,总共处理了n个请求
reading —— 读取客户端的连接数。
writing —— 响应数据到客户端的数量
waiting —— 开启keep-alive 的情况下,这个值等于 active - (reading+writing),意思就是Nginx已经处理完正在等候下一次请求指令的驻留连接
Nginx的验证功能
安装环境
[root@server1 ~]# yum install httpd-tools
设置用户以及密码
[root@server1 ~]# htpasswd -c /usr/local/nginx/passwd.db lisi
New password:
Re-type new password:
Adding password for user lisi
修改配置文件并重启服务
[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
添加以下内容:
location / {
root html;
index index.html index.htm;
allow 20.0.0.11/24; #允许本机访问
deny all;
auth_basic "secret"; ###验证方式为密码验证
auth_basic_user_file /usr/local/nginx/passwd.db; ###密码所在文件
[root@server1 ~]# systemctl restart nginx
验证配置文件是否有错
[root@localhost ~]# nginx -t
nginx: [warn] low address bits of 192.168.73.40/24 are meaningless in /usr/local/nginx/conf/nginx.conf:48
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
[root@localhost ~]# systemctl restart nginx ###重启服务
验证,效果图如下
配置Nginx虚拟主机
还原快照
附加:若以下实验出现以下问题
nginx: [warn] 2048 worker_connections exceed open file resource limit: 1024
添加这条命令则可以修复
[rootgserver2 ~]# ulimit -n 65535 >>/etc/rc.local
基于域名
实验环境,编译安装nginx,不要对配置文件进行修改
创建网页内容,并验证查看
[root@server1 ~]# mkdir -p /var/www/aa
[root@server1 ~]# mkdir -p /var/www/ab
[root@server1 ~]# cd /var/www/aa
[root@server1 aa]# vi index.html
<html><body><h1>web1</h1></body></html>
[root@server1 aa]# cd /var/www/ab
[root@server1 ab]# vi index.html
<html><body><h1>web2</h1></body></html>
为了方便编辑主配置文件,创建超连接
[root@server1 ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
修改主配置文件
[root@server1 ~]# vi /etc/nginx.conf
...
server {
listen 80;
server_name www.aa.com;
#charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/aa;
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
... 省略 以下为添加内容
#}
}
server {
listen 80;
server_name www.ab.com;
#charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
检查配文件是否出现问题,并重启服务
[root@client ~]# 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
[root@client ~]# systemctl restart nginx
修改静态域名映射
[root@client ~]# vi /etc/hosts
20.0.0.13 www.aa.com www.ab.com //添加在最后一行
实验效果图如下所示
如果没出现实验现象,尝试重启服务器,并关闭防火墙进行测试
其他的客户机也可以访问,需要在本地添加以下命令
[root@server1 ~]# vi /etc/hosts (域名静态映射)
添加一行:20.0.0.13 www.aa.com www.ab.com
基于IP
添加虚拟网址并查看
[root@www ~]# ifconfig ens33:1 192.168.20.20/24
[root@www ~]# ip addr
...
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:58:21:84 brd ff:ff:ff:ff:ff:ff
inet 20.0.0.13/24 brd 20.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.20.20/24 brd 192.168.20.255 scope global ens33:1
valid_lft forever preferred_lft forever
修改主配置
listen 20.0.0.10:80; //aa
...
listen 192.168.20.20:80; //ab
检查语法并重启服务
[root@www ~]# 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
[root@www ~]# systemctl restart nginx
修改配置并重启服务
[root@www ~]# vi /etc/selinux/config
...
SELINUX=disabled
...
[root@server1 ~]# systemctl restart nginx
实验效果图如下所示:
附加
如果没有出现该效果,可以重启服务器,但是要注意重新添加虚拟网卡,并重启服务和关闭防火墙和核心防护
基于端口
修改主配置
[root@server1 ~]# vi /etc/nginx.conf
listen 20.0.0.10:8080; //aa
20.0.0.10:8060; //ab
[root@server1 ~]# systemctl restart nginx
重启服务
[root@server1 ~]# systemctl restart nginx
附加:如果没出现效果图,则需重启服务器