Nginx简介
NGINX是免费,开源,高性能的HTTP和反向代理服务器,邮件代理服务器,通用TCP/UDP代理服务器。
特性:
高可靠性
支持热部署:不停机更新配置文件,升级版本,更换日志文件
低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
event-driven,aio,mmap,sendfile
基本功能:
静态资源的web服务器http协议反向代理服务器
pop3/imap4协议反向代理服务器
FastCGI(LNMP),uWSGI(python)等协议
模块化(非DSO),如zip,SSL模块
Nginx的web服务
虚拟主机(server)
支持 keep-alive 和管道连接
访问日志(支持基于日志缓冲提高其性能)
url rewirte
路径别名
基于IP及用户的访问控制
支持速率限制及并发数限制
重新配置和在线升级而无须中断客户的工作进程
Memcached 的 GET 接口
nginx的程序架构:
master/worker结构
负载加载和分析配置文件、管理worker进程、平滑升级
一个或多个 worker 进程处理并响应用户请求
缓存相关的进程:cache loader:载入缓存对象
cache manager:管理缓存对象
Nginx服务配置
[root@CentOS74 ~]# ll /usr/share/nginx/html/
total 20
-rw-r--r-- 1 root root 3650 Mar 6 17:26 404.html #提示网页
-rw-r--r-- 1 root root 3693 Mar 6 17:26 50x.html #错误提示页
-rw-r--r-- 1 root root 3700 Mar 6 17:26 index.html #测试网页
-rw-r--r-- 1 root root 368 Mar 6 17:26 nginx-logo.png
-rw-r--r-- 1 root root 2811 Mar 6 17:26 poweredby.png
Nginx的主配置文件存放在 /etc/nginx/nginx.conf,以四部分组成
主配置段
http 服务配置段
mail 服务配置段
stream 服务配置段主配置段
其中,http 配置段又以 server的公共配置与具体配置
主配置段
user nginx; #worker进程以谁的身份运行
worker_processes 2; #worker进程的数量,通常为主机的CPU核心数
worker_cpu_affinity 0001 0010; #worker进程与CPU绑定,提高cache命中率
worker_priority 19; #worker进程的nice优先级
worker_rlimit_nofile 10240; #worker进程能够打开的文件个数
error_log /var/log/nginx/error.log; #错误日志存放路径
pid /run/nginx.pid; #master进程的pid文件存放路径
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; #声明字配置文件的存放路径和命名规则
events {
worker_connections 10240; #每个worker能够打开的最大并大连接数
}
删除 pid 文件
[root@CentOS74 ~]# cat /run/nginx.pid
1173
[root@CentOS74 ~]# rm -f /run/nginx.pid
[root@CentOS74 ~]# nginx -s stop #无法通过nginx命令管理服务,但是并不影响访问
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
查看 nginx 进程信息
[root@CentOS74 ~]# ps axo pid,cmd,psr,nice | grep nginx
1173 nginx: master process nginx 1 0
1324 nginx: worker process 0 19 #稳定运行在第一颗cpu上,nice值为19
1325 nginx: worker process 1 19 #稳定运行在第二颗cpu上,nice值为19
注意:centos默认的最大文件限制数为1024,所以在修改配置文件后也需要修改 ulimit 来保证高并发。
use epoll
指明并发连接请求的处理方法 ,默认自动选择 epoll
accept_mutex on | off 互斥处理新的连接请求的方法;on指由各个worker轮流处理新请求,off指每个新请求的到达都会通知(唤醒)所有的worker进程,但只有一个进程可获得连接,造成“惊群”,影响性能。
httpd 服务配置段
公共配置
tcp_nodelay on | off;
在keepalived模式下的连接是否启用TCP_NODELAY选项当为off时,延迟发送,合并多个请求后再发送
默认On时,不延迟发送
sendfile on | off;
是否启用sendfile功能,在内核中封装报文直接发送
server_tokens on | off | build | string;
是否在响应报文的Server首部显示nginx版本
server具体配置
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
}
listen:监听端口
监听端口可以是端口号。也可以是 IP 地址:端口
default_server 设定为默认虚拟主机
ssl 限制仅能够通过ssl连接提供服务
backlog=number 超过并发连接数后,新请求进入后援队列的长度
rcvbuf=size 接收缓冲区大小
sndbuf=size 发送缓冲区大小
server_name:虚拟主机名
虚拟主机名可以是单个,也可以是以空格隔开的多个
支持*作为通配符,也支持以~开头的正则表达式(不建议使用,影响性能)
匹配优先级机制从高到低:
(1) 首先是字符串精确匹配 如:www.magedu.com
(2) 左侧*通配符 如:*.magedu.com
(3) 右侧*通配符 如:www.magedu.*
(4) 正则表达式 如: ~^.*\.magedu\.com$
(5) default_server
root:设置web资源的路径映射,用于指明请求的URL所对应的文档的目录路径
可以是指定路径作为根路径,也可以是软连接作为根路径
[root@CentOS74 ~]# ln -s /etc/fstab /data/vhost/linux/index.html
[root@CentOS69 ~]# curl www.linux.com
#
# /etc/fstab
# Created by anaconda on Mon Jun 4 05:11:17 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=cd95b711-4ad3-44fa-9527-85b31b12eca1 / xfs defaults 0 0
UUID=5bf51b3f-2ee8-4941-b115-4fb7511e689a /boot xfs defaults 0 0
UUID=ad5f314c-ec77-4c91-8c70-273df51d8043 /data xfs defaults 0 0
UUID=5976aab0-c761-42bc-b242-267edabf630c swap swap defaults 0 0
/dev/sr0 /mnt/cdrom iso9660 defaults 0 0
location:在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置
[root@CentOS74 ~]# tree /data/ #查看目录结构
/data/
└── vhost
├── centos
│ ├── index.html
│ └── test
│ └── index.html
└── linux
└── index.html
4 directories, 3 files
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location /test { #匹配/data/vhost/centos/下的test目录,不是当访问/test时跳转
root /data/vhost/centos/;
}
}
[root@CentOS69 ~]# curl www.linux.com
centos
[root@CentOS69 ~]# curl www.linux.com/test/
test
=:对URI做精确匹配;
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location = /test/index.html { #精准匹配,不能是目录
root /data/vhost/centos/;
}
}
[root@CentOS69 ~]# curl www.linux.com/test/ #重载配置文件前
linux test
[root@CentOS69 ~]# curl www.linux.com/test/index.html
linux test
[root@CentOS69 ~]# curl www.linux.com/test/ #重载配置文件后
test
[root@CentOS69 ~]# curl www.linux.com/test/index.html
test
^~:对URI的最左边部分做匹配检查,不区分字符大小写
~:对URI做正则表达式模式匹配,区分字符大小写
~*:对URI做正则表达式模式匹配,不区分字符大小写
不带符号:匹配起始于此uri的所有的uri
匹配优先级从高到低:=, ^~, ~/~*, 不带符号
alias:路径别名,文档映射的另一种机制
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location /test/ {
alias /data/vhost/centos/; #当访问/test/uri时,转到/data/vhost/centos/
}
}
[root@CentOS69 ~]# curl www.linux.com/test/
centos
root,给定的路径对应于location中的/uri/
左侧的/
alias,给定的路径对应于location中的/uri/右侧的/
index:指定默认网页文件
error_page:定义错误页,以指定的响应状态码进行响应
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
index myself.html;
error_page 404 =200 /404.html; #指定错误使用指定页面,并返回200响应码
}
[root@CentOS69 ~]# curl www.linux.com/index.html
sorry,not find page. #正确跳转至指定页面
[root@CentOS69 ~]# curl -I www.linux.com/index.html
HTTP/1.1 200 OK #但是响应码为200
Server: nginx/1.12.2
Date: Tue, 10 Jul 2018 22:08:38 GMT
Content-Type: text/html
Content-Length: 21
Last-Modified: Tue, 10 Jul 2018 22:02:32 GMT
Connection: keep-alive
ETag: "5b452cf8-15"
Accept-Ranges: bytes
try_files:按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
try_files $uri $uri/ /test/index.html =404; #查找uri对应的文件或文件夹是否存在,不存在则访问指定页面,并响应404
}
[root@CentOS69 ~]# curl www.linux.com
linux test
[root@CentOS69 ~]# curl www.linux.com/404.html
sorry,not find page. #查找404页面,存在并打开,若是目录,则打开目录下的index页面
注意:查找行为是精准匹配,不会响应自动添加的 index.html,所以只有准确输入 url。
keepalive_timeout timeout [header_timeout];
设定保持连接超时时长,0表示禁止长连接,默认为75s
[root@CentOS69 ~]# curl -I www.linux.com
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 11 Jul 2018 20:21:28 GMT
Content-Type: text/html
Content-Length: 6
Last-Modified: Tue, 10 Jul 2018 20:25:06 GMT
Connection: keep-alive #默认支持长连接
ETag: "5b451622-6"
Accept-Ranges: bytes
keepalive_requests number;
在一次长连接上所允许请求的资源的最大数量
默认为100
keepalive_disable none | browser ...对哪种浏览器禁用长连接
send_timeout time;向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非整个响应过程的传输时长
client_body_buffer_size size;
用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置,能够加快文件的访问速度
设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量
目录名为16进制的数字;
client_body_temp_path /var/tmp/client_body 1 2 2
1 1级目录占1位16进制,即2^4=16个目录 0-f
2 2级目录占2位16进制,即2^8=256个目录 00-ff
2 3级目录占2位16进制,即2^8=256个目录 00-ff
limit_rate rate;
限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制
限制客户端使用除了指定的请求方法之外的其它方法
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
try_files $uri $uri/ /test/index.html =404;
location / {
root /data/vhost/centos;
limit_except GET{
allow 192.168.30.69;
deny 192.168.30.0/24;
}
}
}
[root@CentOS69 ~]# curl -XPUT -I www.linux.com
HTTP/1.1 405 Not Allowed #服务器允许使用其他方法,但是网页不支持
[root@CentOS75 ~]# curl -XPUT -I www.linux.com
HTTP/1.1 403 Forbidden #服务器直接拒绝使用其他方法
aio on | off | threads[=pool];
是否启用aio功能
directio size | off;当文件大于等于给定大小时,例如directio 4m,同步(直接)写磁盘,而非写缓存
open_file_cache off;open_file_cache max=N [inactive=time];
nginx可以缓存以下三种信息:
(1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间
(2) 打开的目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息
max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现管理
inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除
open_file_cache_errors on | off;
是否缓存查找时发生错误的文件一类的信息,默认值为off
open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项
默认值为1
open_file_cache_valid time;缓存项有效性的检查频率
默认值为60s
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
实现基于ip的访问控制功能
用于:http, server, location, limit_except
自上而下检查,一旦匹配,将生效,条件严格的置前
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
try_files $uri $uri/ /test/index.html =404;
location / {
root /data/vhost/centos;
deny 192.168.30.75; #定义访问权限
allow 192.168.30.0/24;
deny all; #定义兜底权限
}
}
[root@CentOS69 ~]# curl -I www.linux.com
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 11 Jul 2018 21:25:13 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Tue, 10 Jul 2018 20:25:49 GMT
Connection: keep-alive
ETag: "5b45164d-7"
Accept-Ranges: bytes
[root@CentOS75 ~]# 吧curl -I www.linux.com
HTTP/1.1 403 Forbidden
Server: nginx/1.12.2
Date: Wed, 11 Jul 2018 21:25:19 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
auth_basic string | off;
auth_basic_user_file file;
实现基于用户的访问控制,使用basic机制进行用户认证
[root@CentOS74 conf.d]# cat vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
auth_basic "login conf"; #声明启用访问控制,并指定登陆提示
auth_basic_user_file "/etc/nginx/conf.d/.nginxuser"; #指定用户列表路径
location / {
root /data/vhost/centos;
}
}
用户列表需要使用 httpd-tools 工具的 htpasswd 命令生成
[root@CentOS74 conf.d]# cat .nginxuser
nginx1:$apr1$x/UqIKVB$GiyNWv22okLo7Zt6GoFcu1
nginx2:$apr1$7EOIXdy0$w1K42AeXKwm0rwEv.PKjX1
用于输出nginx的基本状态信息
[root@CentOS74 conf.d]#⮀⮀cat vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
auth_basic "login conf";
auth_basic_user_file "/etc/nginx/conf.d/.nginxuser";
location / {
root /data/vhost/centos;
}
location /status {
stub_status; #当访问指定uri时显示服务基本信息
}
}
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数
log_format name string ...;
string可以使用nginx核心模块及其它模块内嵌的变量,只能定义在 http 配置段中
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];access_log off;
访问日志文件路径,格式及相关的缓冲的配置
http {
log_format testlog '$remote_addr - $remote_user [$time_iso8601] "$request" ' #定义日志记录格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
root /data/vhost/centos;
access_log /var/log/nginx/linux.com.log testlog ; #指定日志存放路径和使用的日志格式
}
}
[root@CentOS74 vhost]# cat /var/log/nginx/linux.com.log
192.168.30.69 - - [2018-07-12T22:47:50+08:00] "GET / HTTP/1.1" 200 7 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
open_log_file_cache off;缓存各日志文件相关的元数据信息
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔
gzip on | off;
启用或禁用gzip压缩
gzip_comp_level level;压缩比由低到高:1 到 9,默认:1
gzip_disable regex ...;匹配到客户端浏览器不执行压缩
gzip_min_length length;启用压缩功能的响应报文大小阈值
gzip_http_version 1.0 | 1.1;
设定启用压缩功能时,协议的最小版本,默认:1.1
gzip_buffers number size;支持实现压缩功能时缓冲区数量及每个缓存区的大小
默认:32 4k 或 16 8k
gzip_types mime-type ...;指明仅对哪些类型的资源执行压缩操作;即压缩过滤器
默认包含有text/html,不用显示指定,否则出错
gzip_vary on | off;如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”
gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能
off:不启用压缩
expired,no-cache, no-store,private:对后端服务器的响应报文首部Cache-Control值任何一个,启用压缩功能