配置

代理

配置示例

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
#  kencery 注释说明Nginx文件
# 时间:2016-1-19
# 学习内容,只是来自互联网,有版权问题请联系我删除。

######## Nginx的main(全局配置)文件
#指定nginx运行的用户及用户组,默认为nobody
#user nobody;
#开启的线程数,一般跟逻辑CPU核数一致
worker_processes 1;

#定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式
##debug输出最多,crir输出最少,根据实际环境而定
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;`

#指定进程id的存储文件位置
#pid logs/nginx.pid;

#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制
#worker_rlimit_nofile 65535

events {
#设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式
#use epoll;

#定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。
worker_connections 1024;
}

#######Nginx的Http服务器配置,Gzip配置
http {
#主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。
include mime.types;

#核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式
default_type application/octet-stream;

#下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#引用日志main
#access_log logs/access.log main;

#设置允许客户端请求的最大的单个文件字节数
#client_max_body_size 20M;
#指定来自客户端请求头的headebuffer大小
#client_header_buffer_size 32k;
#指定连接请求试图写入缓存文件的目录路径
#client_body_temp_path /dev/shm/client_body_temp;
#指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB
#large client_header_buffers 4 32k;

#开启高效文件传输模式
sendfile on;
#开启防止网络阻塞
#tcp_nopush on;
#开启防止网络阻塞
#tcp_nodelay on;

#设置客户端连接保存活动的超时时间
#keepalive_timeout 0;
keepalive_timeout 65;

#设置客户端请求读取超时时间
#client_header_timeout 10;
#设置客户端请求主体读取超时时间
#client_body_timeout 10;
#用于设置相应客户端的超时时间
#send_timeout

####HttpGZip模块配置
#httpGzip modules
#开启gzip压缩
#gzip on;
#设置允许压缩的页面最小字节数
#gzip_min_length 1k;
#申请4个单位为16K的内存作为压缩结果流缓存
#gzip_buffers 4 16k;
#设置识别http协议的版本,默认为1.1
#gzip_http_version 1.1;
#指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
#gzip_comp_level 2;
#指定压缩的类型
#gzip_types text/plain application/x-javascript text/css application/xml;
#让前端的缓存服务器进过gzip压缩的页面
#gzip_vary on;
upstream aProxy{
server 127.0.0.1:8181;
server 127.0.0.1:8282;
}
#########Nginx的server虚拟主机配置
server {
listen 80;
server_name uucs.wenxiaomiao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ /* {
proxy_pass http://localhost:6001;
}
#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;
}
}
server {
#监听端口为 80
listen 80;

#设置主机域名
server_name localhost;

#设置访问的语言编码
#charset koi8-r;

#设置虚拟主机访问日志的存放路径及日志的格式为main
#access_log logs/host.access.log main;

#设置虚拟主机的基本信息
location / {
#设置虚拟主机的网站根目录
root /home/uucs/applications/nginx/web;

#设置虚拟主机默认访问的网页
index index.html index.htm;
}
location ^~ /api/ {
rewrite "^/api/(.*)$" /admin/$1 break;
proxy_pass http://localhost:80;
proxy_set_header Host $host;
}
location ^~ /juhe/ {
#echo 'juhe pipei chenggong';
proxy_pass http://apis.juhe.cn;
## 重写地址 /juhe ----> http://apis.juhe.cn
rewrite ^.+juhe/?(.*)$ /$1 break;
}

#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
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# 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_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

配置说明

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
# 前言:location是Nginx配置中的一个指令,用于访问的URL匹配,而在这个location中所配置的每个指令将会启动不同的 模块去完成相应的工作。

location [=|~|~*|^~] uri { … }
# 其中,方括号中的四种标识符是可选项,用来改变请求字符串和uri的匹配方式。uri是待匹配的请求字符串,可以是不包含正则的字符串,这种模式被称为“标准的uri";也可以包含正则,这种模式被称为"正则uri"。例如:

location ~ .*\.(php|php5)?$ {
  root /var/www/html;
  ……
}

# 标识符 描述
# = 精确匹配:用于标准uri前,要求请求字符串和uri严格匹配。如果匹配成功就停止匹配,立即执行该location里面的请求。
# ~ 正则匹配:用于正则uri前,表示uri里面包含正则,并且区分大小写。
# ~* 正则匹配:用于正则uri前,表示uri里面包含正则,不区分大小写。
# ^~ 非正则匹配;用于标准uri前,nginx服务器匹配到前缀最多的uri后就结束,该模式匹配成功后,不会使用正则匹配。
# 无 普通匹配(最长字符匹配);与location顺序无关,是按照匹配的长短来取匹配结果。若完全匹配,就停止匹配。


# 为了方便验证各匹配标识符,我们借助第三方模块 echo模块进行操作。安装步骤向右看👉echo-nginx-module的安装、配置、使用 “=” 精准匹配
location = /news/ {
# 全匹配
}
location ~ \.(html) {
# 正则匹配前缀
}
location ~* \.(html){
# 正则匹配前缀,不区分大小写
}
location ^~ /index/ {
# 非正则匹配前缀
}
location / {
#全匹配
}

路径重写

限流

限流配置

1、正常限制访问频率(正常流量)

限制一个用户发送的请求,我Nginx多久接收一个请求。

Nginx中使用ngx_http_limit_req_module模块来限制的访问频率,限制的原理实质是基于漏桶算法原理来实现的。在nginx.conf配置文件中可以使用limit_req_zone命令及limit_req命令限制单个IP的请求处理频率。

定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉

1
2
3
4
5
6
7
8
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
#绑定限流维度
server{
location/seckill.html{
limit_req zone=zone;
proxy_pass http://lj_seckill;
}
}

1r/s代表1秒一个请求,1r/m一分钟接收一个请求, 如果Nginx这时还有别人的请求没有处理完,Nginx就会拒绝处理该用户请求。

2、突发限制访问频率(突发流量)

限制一个用户发送的请求,我Nginx多久接收一个。

上面的配置一定程度可以限制访问频率,但是也存在着一个问题:如果突发流量超出请求被拒绝处理,无法处理活动时候的突发流量,这时候应该如何进一步处理呢?Nginx提供burst参数结合nodelay参数可以解决流量突发的问题,可以设置能处理的超过设置的请求数外能额外处理的请求数。我们可以将之前的例子添加burst参数以及nodelay参数:

定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉

1
2
3
4
5
6
7
8
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
#绑定限流维度
server{
location/seckill.html{
limit_req zone=zone burst=5 nodelay;
proxy_pass http://lj_seckill;
}
}

为什么就多了一个 burst=5 nodelay; 呢,多了这个可以代表Nginx对于一个用户的请求会立即处理前五个,多余的就慢慢来落,没有其他用户的请求我就处理你的,有其他的请求的话我Nginx就漏掉不接受你的请求

3、 限制并发连接数

Nginx中的ngx_http_limit_conn_module模块提供了限制并发连接数的功能,可以使用limit_conn_zone指令以及limit_conn执行进行配置。接下来我们可以通过一个简单的例子来看下:

1
2
3
4
5
6
7
8
9
10
11
12
http {
limit_conn_zone $binary_remote_addr zone=myip:10m;
limit_conn_zone $server_name zone=myServerName:10m;
server {
location / {
limit_conn myip 10;
limit_conn myServerName 100;
rewrite / http://www.lijie.net permanent;
}
}
}

上面配置了单个IP同时并发连接数最多只能10个连接,并且设置了整个虚拟服务器同时最大并发数最多只能100个链接。当然,只有当请求的header被服务器处理后,虚拟服务器的连接数才会计数。刚才有提到过Nginx是基于漏桶算法原理实现的,实际上限流一般都是基于漏桶算法和令牌桶算法实现的。

引用

引用

日志

Nginx日志配置位于conf 的http 块下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
http {
log_format main '----------------- \n'
'[$remote_addr] [$request][$status] referer:[$http_referer] \n '
' TIME:[$time_local] bytes:[$body_bytes_sent] USER:[$remote_user] '
'forwarded:[$http_x_forwarded_for] \n'
' [$http_user_agent] ';

# log_format main escape=json '{ "@timestamp": "$time_iso8601", '
log_format json '{ "@timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr",'
'"request": "$request",'
'"upstr_addr": "$upstream_addr",'
'"costime": "$request_time",'
'"realtime": "$upstream_response_time",'
'"status": $status,'
'"x_forwarded": "$http_x_forwarded_for",'
'"referer": "$http_referer",'
'"bytes":$body_bytes_sent,'
'"dm":$request_body,'
'"agent": "$http_user_agent" }';
}
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
$http_x_forwarded_for  #客户端的真实ip通常web服务器放在反向代理的后面这样就不能获取到客户的IP地址了
# 通过$remote_add拿到的IP地址是反向代理服务器的iP地址。
# 反向代理服务器在转发请求的http头信息中可以增加x_forwarded_for信息
# 用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。$remote_addr
# 远程客户端的IP地址
$remote_user #远程客户端用户名称用于记录浏览者进行身份验证时提供的名字如果没有登录就是空白。
$time_local #访问的时间与时区比如18/Jul/2012:17:00:01 +0800时间信息最后的"+0800"
#表示服务器所处时区位于UTC之后的8小时。
$request_method #HTTP请求方法,通常为"GET"或"POST"
$scheme #请求使用的Web协议,"http" 或 "https"
$host #HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名.请求中的主机头字段,
#如果请求中的主机头不可用,则为服务器处理请求的服务器名称
$request_uri #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写
$uri #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,
#它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"
$query_string #请求中的参数值
$server_protocol #服务器的HTTP版本,通常为 "HTTP/1.0" 或 "HTTP/1.1"
$status #HTTP响应代码
$body_bytes_sent #传输给客户端的字节数,响应头不计算在内;
#这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$http_referer #url跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent #用户终端浏览器等信息
$request_time #处理客户端请求使用的时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,
#直到把最后一个字符发送给客户端后进行日志写入为止。
$upstream_addr #真正提供服务的主机地址
$request_id #生产唯一ID方便查询问题
$upstream_response_time #请求过程中upstream的响应时间
$arg_PARAMETER 客户端GET请求中PARAMETER 字段的值
$args 客户端请求中的参数
$binary_remote_addr 远程地址的二进制表示
$body_bytes_sent 已发送的消息体字节数
$content_length HTTP请求信息中content-length的字段
$content_type 请求信息中content-type字段
$cookie_COOKIE 客户端请求中COOKIE头域的值
$document_root 针对当前请求的根路径设置值
$document_uri$uri相同
$host 请求信息中的host头域,如果请求中没有Host行,则等于设置的服务器名
$http_HEADER HTTP请求信息里的HEADER地段
$http_host$host相同,但是如果请求信息中没有host行,则可能不同客户端cookie信息
$http_cookie 客户端cookie信息
$http_referer 客户端是从哪一个地址跳转过来的
$http_user_agent 客户端代理信息,也就是你客户端浏览器
$http_via 最后一个访问服务器的IP
$http_x_forwarded_for 相当于访问网路访问的路径
$is_args 如果有args的值,则等于”?”,否则为空
$limit_rate 对连接速率的限制
$nginx_version 当前Nginx的版本
$pid 当前Nginx服务器的进程的进程ID
$query_string$args相同
$remote_addr 客户端IP地址
$remote_port 客户端的端口
$remote_user 客户端的用户名,用于 auth basic module验证
$request 客户端请求
$request_body 客户端发送的报文体
$request_body_file 发送后端服务器的本地临时缓存文件的名称
$request_filename 当前请求的文件路径名,由root或alias指令与URI请求生成
$request_method 请求后端数据的方法,例如”GET”,”POST”
$request_uri 请求的URI,带参数,不包含主机名
$scheme 所用的协议,如http或者HTTPS,比如rewrite^(.+)$$scheme://mysite.name$redirect
$sent_http_cache_control 对应http请求头中的Cache-Control,需要打开chrome浏览器,右键检查,选中network,点中其中一个请求的资源
$sent_http_connection 对应http请求中的Connection
$sent_http_content_type 对应http请求中的Content-Type
$sent_last_modified 对应请求中的Last-Modified
$server_addr 服务端的地址
$server_name 请求到达的服务器名
$server_port 请求到达服务器端口号
$server_protocol 请求协议的版本号,HTTP1.0/HTTP1.1
$uri 请求的不带请求参数的URI,可能和最初的值有不同,比如经过重定向之类的