Nginx的使用总结(二)
Nginx.conf配置文件结构
配置文件结构
1、nginx.conf 的主体结构
2、全局块
3、events 块
4、http 块
①、http 全局块
②、server 块
我们将 nginx 安装在 /usr/local/nginx 目录下,其默认的配置文件都放在这个目录的 conf 目录下,而主配置文件 nginx.conf 也在其中,后续对 nginx 的使用基本上都是对此配置文件进行相应的修改
nginx.conf 的主体结构
打开此文件,内容如下:
1 #user nobody;
2 worker_processes 1;
3
4 #error_log logs/error.log;
5 #error_log logs/error.log notice;
6 #error_log logs/error.log info;
7
8 #pid logs/nginx.pid;
9
10
11 events {
12 worker_connections 1024;
13 }
14
15
16 http {
17 include mime.types;
18 default_type application/octet-stream;
19
20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
21 # '$status $body_bytes_sent "$http_referer" '
22 # '"$http_user_agent" "$http_x_forwarded_for"';
23
24 #access_log logs/access.log main;
25
26 sendfile on;
27 #tcp_nopush on;
28
29 #keepalive_timeout 0;
30 keepalive_timeout 65;
31
32 #gzip on;
33
34 server {
35 listen 80;
36 server_name localhost;
37
38 #charset koi8-r;
39
40 #access_log logs/host.access.log main;
41
42 location / {
43 root html;
44 index index.html index.htm;
45 }
46
47 #error_page 404 /404.html;
48
49 # redirect server error pages to the static page /50x.html
50 #
51 error_page 500 502 503 504 /50x.html;
52 location = /50x.html {
53 root html;
54 }
55
56 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
57 #
58 #location ~ \.php$ {
59 # proxy_pass http://127.0.0.1;
60 #}
61
62 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
63 #
64 #location ~ \.php$ {
65 # root html;
66 # fastcgi_pass 127.0.0.1:9000;
67 # fastcgi_index index.php;
68 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
69 # include fastcgi_params;
70 #}
71
72 # deny access to .htaccess files, if Apache's document root
73 # concurs with nginx's one
74 #
75 #location ~ /\.ht {
76 # deny all;
77 #}
78 }
79
80
81 # another virtual host using mix of IP-, name-, and port-based configuration
82 #
83 #server {
84 # listen 8000;
85 # listen somename:8080;
86 # server_name somename alias another.alias;
87
88 # location / {
89 # root html;
90 # index index.html index.htm;
91 # }
92 #}
93
94
95 # HTTPS server
96 #
97 #server {
98 # listen 443 ssl;
99 # server_name localhost;
100
101 # ssl_certificate cert.pem;
102 # ssl_certificate_key cert.key;
103
104 # ssl_session_cache shared:SSL:1m;
105 # ssl_session_timeout 5m;
106
107 # ssl_ciphers HIGH:!aNULL:!MD5;
108 # ssl_prefer_server_ciphers on;
109
110 # location / {
111 # root html;
112 # index index.html index.htm;
113 # }
114 #}
115
116 }
# 开头的表示注释内容,我们去掉所有以 # 开头的段落,精简之后的内容如下:
1 worker_processes 1;
2
3 events {
4 worker_connections 1024;
5 }
6
7
8 http {
9 include mime.types;
10 default_type application/octet-stream;
11
12
13 sendfile on;
14
15 keepalive_timeout 65;
16
17 server {
18 listen 80;
19 server_name localhost;
20
21 location / {
22 root html;
23 index index.html index.htm;
24 }
25
26 error_page 500 502 503 504 /50x.html;
27 location = /50x.html {
28 root html;
29 }
30
31 }
32
33 }
根据上述文件,我们可以很明显的将 nginx.conf 配置文件分为三部分:
全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
比如上面第一行配置的:
worker_processes 1;
这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约
events 块
比如上面的配置:
events {
worker_connections 1024;
}
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
上述例子就表示每个 work process 支持的最大连接数为 1024.
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
http 块
1 http {
2 include mime.types;
3 default_type application/octet-stream;
4
5
6 sendfile on;
7
8 keepalive_timeout 65;
9
10 server {
11 listen 80;
12 server_name localhost;
13
14 location / {
15 root html;
16 index index.html index.htm;
17 }
18
19 error_page 500 502 503 504 /50x.html;
20 location = /50x.html {
21 root html;
22 }
23
24 }
25
26 }
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
需要注意的是:http 块也可以包括 http全局块、server 块。
①、http 全局块
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
②、server 块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。后面会详细介绍虚拟主机的概念。
每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
1、全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
2、location 块
一个 server 块可以配置多个 location 块。
这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
具体配置
全局配置:user、worker_processes、error_log、pid
events:网络连接相关、worker_connections
http:最重要的部分,大部分功能在这里
server:虚拟主机相关
location:server里面
nginx.conf全局配置
定义运行nginx服务的用户,还可以加上组,如 user nobody nobody
user nobody
定义nginx子进程数量,即提供服务的进程数量,该数值建议和服务cpu核数保持一致。 除了可以定义数字外,还可以定义为auto,表示让系统自动调整
worker_processes 1
定义错误日志的路径,可以是相对路径(相对prefix路径的),也可以是绝对路径。 该配置可以在此处定义,也可以定义到http、server、location里
error_log logs/error.log
定义错误日志路径以及日志级别 错误日志级别: 常见的错误日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少,如果不定义默认是error
error_log logs/error.log notice
定义nginx进程pid文件所在路径,可以是相对路径,也可以是绝对路径
pid logs/nginx.pid
定义nginx最多打开文件数限制。如果没设置的话,这个值为操作系统(ulimit -n)的限制保持一致。 把这个值设高,nginx就不会有“too many open files”问题了
worker_rlimit_nofile 100000
events配置部分
定义每个work_process同时开启的最大连接数,即允许最多只能有这么多连接
worker_connections 1024
当某一个时刻只有一个网络连接请求服务器时,服务器上有多个睡眠的进程会被同时叫醒,这样会损耗一定的服务器性能。 Nginx中的accept_mutex设置为on,将会对多个Nginx进程(worker processer)接收连接时进行序列化,防止多个进程争抢资源。 默认就是on
accept_mutex on
nginx worker processer可以做到同时接收多个新到达的网络连接,前提是把该参数设置为on。 默认为off,即每个worker process一次只能接收一个新到达的网络连接
multi_accept on
Nginx服务器提供了多个事件驱动器模型来处理网络消息。
其支持的类型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。
* select:只能在Windows下使用,这个事件模型不建议在高负载的系统使用
* poll:Nginx默认首选,但不是在所有系统下都可用
* kqueue:这种方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系统中是最高效的
* epoll: 这种方式是在Linux 2.6+内核中最高效的方式
* rtsig:实时信号,可用在Linux 2.2.19的内核中,但不适用在高流量的系统中
* /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操作系统最高效的方式
* eventport: Solaris 10最高效的方式
use epoll
http配置部分
官方文档: http://nginx.org/en/docs/
参考链接: https://segmentfault.com/a/1190000012672431
参考链接: https://segmentfault.com/a/1190000002797601
参考链接: https://kb.cnblogs.com/page/92320/
include mime.types; //cat conf/mime.types
定义nginx能识别的网络资源媒体类型(如,文本、html、js、css、流媒体等)
default_type application/octet-stream;
定义默认的type,如果不定义改行,默认为text/plain
mime.type
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
其中main为日志格式的名字,后面的为nginx的内部变量组成的一串字符串
log_format
定义日志的路径以及采用的日志格式,该参数可以在server配置块中定义
access_log logs/access.log main
是否调用sendfile函数传输文件,默认为off,使用sendfile函数传输,可以减少user mode和kernel mode的切换,从而提升服务器性能。
对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载
sendfile on
该参数限定Nginx worker process每次调用sendfile()函数传输数据的最大值,默认值为0,如果设置为0则无限制
sendfile_max_chunk 128k
当tcp_nopush设置为on时,会调用tcp_cork方法进行数据传输。
使用该方法会产生这样的效果:当应用程序产生数据时,内核不会立马封装包,而是当数据量积累到一定量时才会封装,然后传输。这样有助于解决网络堵塞问题。
默认值为on。举例:快递员收快递、发快递,包裹累积到一定量才会发,节省运输成本
tcp_nopush on
该参数有两个值,第一个值设置nginx服务器与客户端会话结束后仍旧保持连接的最长时间,单位是秒,默认为75s。
第二个值可以省略,它是针对客户端的浏览器来设置的,可以通过curl -I看到header信息中有一项Keep-Alive: timeout=60,如果不设置就没有这一项。
第二个数值设置后,浏览器就会根据这个数值决定何时主动关闭连接,Nginx服务器就不操心了。但有的浏览器并不认可该参数
keepalive_timeout 65 60
这个超时时间是发送响应的超时时间,即Nginx服务器向客户端发送了数据包,但客户端一直没有去接收这个数据包。 如果某个连接超过send_timeout定义的超时时间,那么Nginx将会关闭这个连接
send_timeout
浏览器在发送含有较大HTTP包体的请求时,其头部会有一个Content-Length字段,client_max_body_size是用来限制Content-Length所示值的大小的。
这个限制包体的配置不用等Nginx接收完所有的HTTP包体,就可以告诉用户请求过大不被接受。会返回413状态码。
例如,用户试图上传一个1GB的文件,Nginx在收完包头后,发现Content-Length超过client_max_body_size定义的值,就直接发送413(Request Entity Too Large)响应给客户端
client_max_body_size 10m
是否开启gzip压缩
gzip on
设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大
gzip_min_length 1k
设置系统获取几个单位的buffer用于存储gzip的压缩结果数据流。4 16k代表分配4个16k的buffer
gzip_buffers 4 16k
用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户会看到乱码,所以为了支持前期版本加上了这个选项。 如果你用了Nginx反向代理并期望也启用Gzip压缩的话,由于末端通信是http/1.1,故请设置为 1.1
gzip_http_version 1.1
gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_comp_level 6
匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
在conf/mime.conf里查看对应的type。
示例:gzip_types text/plain application/x-javascript text/css text/html application/xml;
gzip_types mime-type …
Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含”Via”的 header头。
以下为可用的值:
off - 关闭所有的代理结果数据的压缩
expired - 启用压缩,如果header头中包含 "Expires" 头信息
no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
any - 无条件启用压缩
gzip_proxied any
和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据
gzip_vary on
server配置部分
server{ } 包含在http{ }内部,每一个server{ }都是一个虚拟主机(站点)。
nginx.conf配置文件中server{}部分的内容如下:
server {
listen 80; //监听端口为80,可以自定义其他端口,也可以加上IP地址,如,listen 127.0.0.1:8080;
server_name localhost; //定义网站域名,可以写多个,用空格分隔。
#charset koi8-r; //定义网站的字符集,一般不设置,而是在网页代码中设置。
#access_log logs/host.access.log main; //定义访问日志,可以针对每一个server(即每一个站点)设置它们自己的访问日志。
##在server{}里有很多location配置段
location / {
root html; //定义网站根目录,目录可以是相对路径也可以是绝对路径。
index index.html index.htm; //定义站点的默认页。
}
#error_page 404 /404.html; //定义404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //当状态码为500、502、503、504时,则访问50x.html
location = /50x.html {
root html; //定义50x.html所在路径
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#定义访问php脚本时,将会执行本location{}部分指令
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; //proxy_pass后面指定要访问的url链接,用proxy_pass实现代理。
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000; //定义FastCGI服务器监听端口与地址,支持两种形式,1 IP:Port, 2 unix:/path/to/sockt
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //定义SCRIPT_FILENAME变量,后面的路径/scripts为上面的root指定的目录
# include fastcgi_params; //引用prefix/conf/fastcgi_params文件,该文件定义了fastcgi相关的变量
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { //访问的url中,以/.ht开头的,如,www.example.com/.htaccess,会被拒绝,返回403状态码。
# deny all; //这里的all指的是所有的请求。
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000; //监听8000端口
# listen somename:8080; //指定ip:port
# server_name somename alias another.alias; //指定多个server_name
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl; //监听443端口,即ssl
# server_name localhost;
### 以下为ssl相关配置
# ssl_certificate cert.pem; //指定pem文件路径
# ssl_certificate_key cert.key; //指定key文件路径
# ssl_session_cache shared:SSL:1m; //指定session cache大小
# ssl_session_timeout 5m; //指定session超时时间
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //指定ssl协议
# ssl_ciphers HIGH:!aNULL:!MD5; //指定ssl算法
# ssl_prefer_server_ciphers on; //优先采取服务器算法
# location / {
# root html;
# index index.html index.htm;
# }
#}