常用的命令

1
2
3
4
5
6
7
8
9
nginx -h   # 帮助
nginx -t # 验证配置文件的正确性
nginx -v # 查看nginx的版本
nginx -c filename # 按指定的配置文件启动,默认的路径conf/nginx.conf

nginx -s reload # 重新载入配置文件
nginx -s reopen # 重启nginx
nginx -s stop # 关闭nginx
nginx -s quit # 退出

nginx的配置文件的详解

nginx的配置分为四个部分:main(全区设置),server(主机配置),upstream(负载均衡服务器设置),和location(URL匹配特定位置设置)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#定义Nginx运行的用户和用户组
user www www;
#pid文件
#进程文件
pid /var/run/nginx.pid;
#nginx进程数,建议设置为等于CPU总核心数。
#==worker进程数,通常设置等同于CPU数量,auto为自动检测
worker_processes 8;
worker_processes auto;
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
#==worker进程打开最大文件数,可CPU*10000设置
worker_rlimit_nofile 65535;
#worker_rlimit_nofile 100000;
#全局错误日志
error_log logs/error.log;
# error_log logs/error.log info; # 可以设置级别

# 设置前台运行,默认后台运行
daemon off;

#工作模式与连接数上限
#events模块中包含nginx中所有处理连接的设置
events {
#==worker进程同时打开的最大连接数,可CPU*2048设置
worker_connections 2048;
#==告诉nginx收到一个新链接通知后接受尽可能多的链接
multi_accept on;
#==设置用于复用客户端线程的轮训方法
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
}


#http模块控制着nginx http处理的所有核心特性
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
#上传文件大小限制
large_client_header_buffers 4 64k;
#==允许客户端请求的最大单文件字节数
client_max_body_size 8m;
#==冲区代理缓冲用户端请求的最大字节数
client_header_buffer_size 32k;
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #长连接超时时间,单位是秒

#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义本虚拟主机的访问日志
access_log /var/log/nginx/access.log main;
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip模块设置
#==设置nginx采用gzip压缩的形式发送数据,减少发送数据量,但会增加请求处理时间及CPU处理时间,需要权衡
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
#==设置数据压缩等级,1-9之间,9最慢压缩比最大
gzip_comp_level 2; #压缩等级
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss
#==加vary给代理服务器使用,针对有的浏览器支持压缩,有个不支持,根据客户端的HTTP头来判断是否需要压缩
gzip_vary on;
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#nginx在压缩资源之前,先查找是否有预先gzip处理过的资源
#!gzip_static on;
#为指定的客户端禁用gzip功能
gzip_disable "MSIE[1-6]\.";
#允许或禁止压缩基于请求和相应的响应流,any代表压缩所有请求
gzip_proxied any;
#==设置对数据启用压缩的最少字节数,如果请求小于10240字节则不压缩,会影响请求速度
gzip_min_length 10240;

#==开发缓存的同时也指定了缓存文件的最大数量,20s如果文件没有请求则删除缓存
open_file_cache max=100000 inactive=20s;
#==指多长时间检查一次缓存的有效信息
open_file_cache_valid 60s;
#==文件缓存最小的访问次数,只有访问超过5次的才会被缓存
open_file_cache_min_uses 5;
#当搜索一个文件时是否缓存错误信息
open_file_cache_errors on;

#==允许客户端请求的最大单文件字节数
client_max_body_size 8m;
#==冲区代理缓冲用户端请求的最大字节数
client_header_buffer_size 32k;


upstream blog.ha97.com {
#ip_hash;
#weight参数表示权重值,权值越高被分配到的几率越大
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
#允许客户端请求的最大单文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数,
client_body_buffer_size 128k;
#==nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 60;
#==连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 120;
#==后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 20;
#==设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 32k;
#==proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_buffers 4 128k;
#==高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 256k;
#==设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 256k;
#==1G内存缓冲空间,3天不用删除,最大磁盘缓冲空间2G
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:1024m inactive=3d max_size=2g;


#设定负载均衡服务器列表
upstream nginx.test.com{
#后端服务器访问规则
#ip_hash;
#weight参数表示权重值,权值越高被分配到的几率越大
#server 10.11.12.116:80 weight=5;
#PC_Local
server 10.11.12.116:80;
#PC_Server
server 10.11.12.112:80;
#Notebook
#server 10.11.12.106:80;
}

#虚拟主机设定模块(挂载多个站点,只需要配置多个server及upstream节点即可)
server {
#监听80端口
listen 80;
#定义使用nginx.test.com访问
server_name nginx.test.com;
#设定本虚拟主机的访问日志
access_log logs/nginx.test.com.access.log;

#默认请求
# 语法规则:location [=|~|~*|^~] /uri/ {...} 先匹配普通location,在匹配正则location
# = 开头表示精确匹配
# ^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可,无需考虑编解码
# ~ 开头表示区分大小写的正则匹配
# ~* 开头表示不区分大小写的正则匹配
# !~ 开头表示区分大小写的不匹配的正则
# !~* 开头表示不区分大小写的不匹配的正则
# / 通用匹配,任何请求都会被匹配到
location / {
#限制IP访问
deny 192.168.0.2;
allow 192.168.0.0/24;
allow 192.168.1.1;
deny all;
#定义服务器的默认网站根目录位置
root html;
#定义首页索引文件的名称
index index.html index.htm;
#定义后端负载服务器组
proxy_pass http://nginx.test.com;
}

#定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/{
root /var/www/virtual/htdocs;
#过期时间1天
expires 1d;
#关闭媒体文件日志
access_log off;
log_not_found off;
}
#设定查看Nginx状态的地址
location /NginxStatus {
#!stub_status on; #无此关键字
access_log off;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止访问的文件.htxxx
location ~ /\.ht {
deny all;
}
}
#网站较多的情况下ngxin又不会请求瓶颈可以考虑挂多个站点,并把虚拟主机配置单独放在一个文件内,引入进来
#include website.conf;
}

对域名的匹配规则

1
2
3
4
5
server_name location;      # 匹配本地主机ip;
server_name www.baidu.com; # 精确配置
server_name *.baidu.com; # 以通配符开头的配置
server_name baidu.*; # 以通配符结尾的配置
server_name ~^(?<user>.+)\.example\.net$; #正则的配置,必须以波浪线为开头

路由对location的匹配规则

贪婪原则:匹配尽可能长的路径;

1
2
3
4
5
6
7
8
9
10
11
server {
location / {
root /data/www;
}

location /images/ {
root /data;
}
}

# /images/匹配到第二个;

通配规则

1
2
3
4
5
以=开头,表示精确匹配;
以^~开头,表示uri以某个常规字符串开头,不是正则匹配;
以~开头,表示区分大小写的正则匹配;
以~*开头,表示不区分大小写的正则匹配;
/,表示通用匹配, 如果没有其它匹配,任何请求都会匹配到;

注意

1
2
3
4
5
6
7
8
9
# 如果有两个开头一样的路由,有可能匹配不到

如:

location /eeg

location /eegabc

# 上面会造成/eegabc匹配失败

nginx的日志设置规则

主要分为access_log和error_log;

1
2
3
4
5
6
7
access log : 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息
默认值: access_log logs/access.log combined; # 表示配置的路径和格式
配置段: http, server, location, if in location, limit_except

error log : 记录服务器错误日志
默认值: error_log logs/error.log error; # 表示配置的路径和级别,有[debug | info | notice | warn | error | crit | alert | emerg]八个级别;
配置段: main, http, server, location

负载均衡的规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#设定负载均衡服务器列表
upstream test{
#后端服务器访问规则
#ip_hash; # 根据ip的hash规则分配,主要是当需要会话保持的时候,session是不能共享的;
#weight参数表示权重值,权值越高被分配到的几率越大,服务器的运算能力有时不是一样的;
#server 10.11.12.116:80 weight=5;
#PC_Local
server 10.11.12.116:80 max_fails=3 fail_timeout=30;
# max_fails:最多的转发失败的次数,如果转发失败超过次数,就认为该服务器挂掉;
# fail_timeout:请求失败暂停的服务时间,在这个时间内会暂时放弃转发给该服务器;
#PC_Server
server 10.11.12.112:80 down; # down:暂时不参与负载均衡;
server 10.11.12.112:80 backup; # backup:备用服务器,当其他所有的机器忙的时候,会启用备用服务器;
}