Skip to content

Commit 0f42dfa

Browse files
committed
添加部分文档
1 parent 910e0cd commit 0f42dfa

File tree

5 files changed

+216
-3
lines changed

5 files changed

+216
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
- [x] [ssl配置](book/02配置样例/01ssl配置样例.md)
3030
- 调优列表
3131
- [x] [worker数量](book/03配置调优/01worker数量调优.md)
32-
- [x] [使用return代替rewrite做重定向](book/03配置调优/07使用return代替rewrite做重定向.md)
3332
- [ ] [使用HTTP2](book/03配置调优/02使用HTTP2.md)
3433
- [ ] [SSL会话缓存](book/03配置调优/03SSL会话缓存.md)
3534
- [ ] [使用server_name代替if指令判断domain](book/03配置调优/04使用server_name代替if指令判断domain.md)
3635
- [ ] [使用$request_uri代替正则](book/03配置调优/05使用$request_uri代替正则.md)
3736
- [ ] [使用try_files指令来确保文件存在](book/03配置调优/06使用try_files指令来确保文件存在.md)
37+
- [ ] [使用return代替rewrite做重定向](book/03配置调优/07使用return代替rewrite做重定向.md)
38+
- [ ] [pcre开启JIT调优](book/03配置调优/08启用PCRE-JIT以加速正则表达式的处理.md)
39+
- [ ] [upstream开启keepalive](book/03配置调优/09upstream开启keepalive.md)
40+
- [ ] [尽可能精准配置location](book/03配置调优/10尽可能精准配置location.md)
3841

3942
### 构建介质
4043

book/03配置调优/08启用PCRE JIT以加速正则表达式的处理.md renamed to book/03配置调优/08启用PCRE-JIT以加速正则表达式的处理.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### 使用return代替rewrite做重定向
1+
### 启用PCRE JIT以加速正则表达式的处理
22

33
[原文地址](https://github.com/trimstray/nginx-admins-handbook/blob/master/doc/RULES.md#beginner-enable-pcre-jit-to-speed-up-processing-of-regular-expressions)
44
- [更多nginx文档](https://weiliang-ms.github.io/nginx/)
@@ -15,7 +15,7 @@
1515

1616
> 使用`pcre_jit`的劣势
1717
18-
在某些情况下,`pcre_jit`可能有负面影响,具体参考[PCRE性能优化](../优秀文档/PCRE性能优化.md)
18+
在某些情况下,开启`pcre_jit`可能有负面影响,具体参考[PCRE性能优化](https://zherczeg.github.io/sljit/pcre.html)
1919

2020
> 启用方式
2121
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
### upstream开启keepalive
2+
3+
[原文地址](https://github.com/trimstray/nginx-admins-handbook/blob/master/doc/RULES.md#rationale-31)
4+
- [更多nginx文档](https://weiliang-ms.github.io/nginx/)
5+
- [更多linux相关文档](https://weiliang-ms.github.io/wl-awesome/)
6+
7+
> 解释说明
8+
9+
配置`keepalive`的主要意图:解决在高延迟网络上建立`TCP`连接的延迟问题。
10+
`nginx`与上游服务器之间需要持续保持一定数量的连接时,`keepalive`很有用。
11+
12+
开启`Keep-Alive`连接对性能有很大的影响:减少了打开和关闭连接所需的`CPU`和网络开销。
13+
14+
通过在`nginx`中启用`HTTP keepalive`,降低了`nginx`连接上游服务器的延迟,从而提高了性能,并减少了`nginx`耗尽临时端口的可能性。
15+
`nginx`将重用现有的`TCP`连接,而不创建新的`TCP`,
16+
这可以极大地减少繁忙服务器上`TIME_WAIT TCP`连接中的套接字数量(减少操作系统建立新连接的工作,减少网络上的数据包)
17+
18+
**注意:** 仅在`HTTP/1.1`时支持`Keep-Alive`连接。
19+
20+
> 配置样例
21+
22+
```nginx configuration
23+
# Upstream context:
24+
upstream backend {
25+
26+
# Sets the maximum number of idle keepalive connections to upstream servers
27+
# that are preserved in the cache of each worker process.
28+
keepalive 16;
29+
30+
}
31+
32+
# Server/location contexts:
33+
server {
34+
35+
...
36+
37+
location / {
38+
39+
# By default only talks HTTP/1 to the upstream,
40+
# keepalive is only enabled in HTTP/1.1:
41+
proxy_http_version 1.1;
42+
43+
# Remove the Connection header if the client sends it,
44+
# it could be "close" to close a keepalive connection:
45+
proxy_set_header Connection "";
46+
47+
...
48+
49+
}
50+
51+
}
52+
```
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
### location尽可能的精确
2+
3+
> 解释说明
4+
5+
精确的`location`匹配通常被用来加快选择过程,匹配通过后立即结束算法的执行。
6+
7+
使用`=`修饰符可以定义`URI``location`的精确匹配。它的处理速度非常快,可以节省大量的`CPU`开销。
8+
9+
```nginx configuration
10+
location = / {
11+
12+
...
13+
14+
}
15+
16+
# Matches the query /v9 only and stops searching:
17+
location = /v9 {
18+
19+
...
20+
21+
}
22+
23+
...
24+
```
25+
26+
如果找到精确匹配,则搜索终止。 例如,存在`/`请求,并且请求的频率比较高,则可以定义`location = /`加快这些请求的处理速度。
27+
28+
### 关于nginx location匹配顺序
29+
30+
**例子来源以下地址**
31+
32+
[https://github.com/trimstray/nginx-admins-handbook#introduction](https://github.com/trimstray/nginx-admins-handbook#introduction)
33+
34+
> 假设配置如下
35+
36+
```nginx configuration
37+
server {
38+
39+
listen 80;
40+
server_name xyz.com www.xyz.com;
41+
42+
location ~ ^/(media|static)/ {
43+
root /var/www/xyz.com/static;
44+
expires 10d;
45+
}
46+
47+
location ~* ^/(media2|static2) {
48+
root /var/www/xyz.com/static2;
49+
expires 20d;
50+
}
51+
52+
location /static3 {
53+
root /var/www/xyz.com/static3;
54+
}
55+
56+
location ^~ /static4 {
57+
root /var/www/xyz.com/static4;
58+
}
59+
60+
location = /api {
61+
proxy_pass http://127.0.0.1:8080;
62+
}
63+
64+
location / {
65+
proxy_pass http://127.0.0.1:8080;
66+
}
67+
68+
location /backend {
69+
proxy_pass http://127.0.0.1:8080;
70+
}
71+
72+
location ~ logo.xcf$ {
73+
root /var/www/logo;
74+
expires 48h;
75+
}
76+
77+
location ~* .(png|ico|gif|xcf)$ {
78+
root /var/www/img;
79+
expires 24h;
80+
}
81+
82+
location ~ logo.ico$ {
83+
root /var/www/logo;
84+
expires 96h;
85+
}
86+
87+
location ~ logo.jpg$ {
88+
root /var/www/logo;
89+
expires 48h;
90+
}
91+
}
92+
```
93+
94+
> 匹配规则如下
95+
96+
| 请求URL | 相匹配的location | 最终匹配 |
97+
|---------------------|-------------------------------------------------------------------------------------------------------------------|---------------------------|
98+
| / | 1\) prefix match for / | / |
99+
| /css | 1\) prefix match for / | / |
100+
| /api | 1\) exact match for /api | /api |
101+
| /api/ | 1\) prefix match for / | / |
102+
| /backend | 1\) prefix match for /2\) prefix match for /backend | /backend |
103+
| /static | 1\) prefix match for / | / |
104+
| /static/header\.png | 1\) prefix match for /2\) case sensitive regex match for ^/\(media\|static\)/ | ^/\(media\|static\)/ |
105+
| /static/logo\.jpg | 1\) prefix match for /2\) case sensitive regex match for ^/\(media\|static\)/ | ^/\(media\|static\)/ |
106+
| /media2 | 1\) prefix match for /2\) case insensitive regex match for ^/\(media2\|static2\) | ^/\(media2\|static2\) |
107+
| /media2/ | 1\) prefix match for /2\) case insensitive regex match for ^/\(media2\|static2\) | ^/\(media2\|static2\) |
108+
| /static2/logo\.jpg | 1\) prefix match for /2\) case insensitive regex match for ^/\(media2\|static2\) | ^/\(media2\|static2\) |
109+
| /static2/logo\.png | 1\) prefix match for /2\) case insensitive regex match for ^/\(media2\|static2\) | ^/\(media2\|static2\) |
110+
| /static3/logo\.jpg | 1\) prefix match for /static32\) prefix match for /3\) case sensitive regex match for logo\.jpg$ | logo\.jpg$ |
111+
| /static3/logo\.png | 1\) prefix match for /static32\) prefix match for /3\) case insensitive regex match for \.\(png\|ico\|gif\|xcf\)$ | \.\(png\|ico\|gif\|xcf\)$ |
112+
| /static4/logo\.jpg | 1\) priority prefix match for /static42\) prefix match for / | /static4 |
113+
| /static4/logo\.png | 1\) priority prefix match for /static42\) prefix match for / | /static4 |
114+
| /static5/logo\.jpg | 1\) prefix match for /2\) case sensitive regex match for logo\.jpg$ | logo\.jpg$ |
115+
| /static5/logo\.png | 1\) prefix match for /2\) case insensitive regex match for \.\(png\|ico\|gif\|xcf\)$ | \.\(png\|ico\|gif\|xcf\)$ |
116+
| /static5/logo\.xcf | 1\) prefix match for /2\) case sensitive regex match for logo\.xcf$ | logo\.xcf$ |
117+
| /static5/logo\.ico | 1\) prefix match for /2\) case insensitive regex match for \.\(png\|ico\|gif\|xcf\)$ | \.\(png\|ico\|gif\|xcf\)$ |
118+
119+
120+
> 匹配顺序说明
121+
122+
`nginx根据uri进行最优匹配`
123+
124+
125+
- 基于前缀的`nginx` `location`匹配(没有正则表达式): 每个`location`都将根据请求`URI`进行检查
126+
- `nginx`搜索精确的匹配: 如果=修饰符与请求`URI`完全匹配,则立即选择此特定位置块
127+
- 如果没有找到精确的位置块(即没有相应的=修饰符),`nginx`将继续使用非精确的前缀。它从这个`URI`的最长匹配前缀位置开始,方法如下:
128+
- 如果最长匹配前缀`location``^~`修饰符,`nginx`将立即停止搜索并选择该`location`
129+
- 假设最长匹配前缀`location`不使用`^~`修饰符,匹配将被临时存储,并继续执行
130+
- 一旦选择并存储了最长匹配前缀`location``nginx`就会继续计算区分大小写和不敏感的正则表达式`location`
131+
第一个匹配`URI`的正则表达式`location`将立即被选中来处理请求
132+
- 如果没有找到匹配请求`URI`的正则表达式`location`,则选择先前存储的前缀`location`来服务请求
133+
134+
**最长匹配解释说明:**
135+
请求为`/a/b/c/d`时,`location A``location B``location B`为最长匹配
136+
137+
- `location A`
138+
```nginx configuration
139+
location /a {
140+
...
141+
}
142+
```
143+
144+
- `location B`
145+
```nginx configuration
146+
location /a/b/c {
147+
...
148+
}
149+
```

book/优秀文档/PCRE性能优化.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ TODO
3737

3838
### 编译时开销
3939

40+
决定何时使用或不使用`JIT`编译是一个重要的问题。
41+
由于`JIT`是一种重量级优化,我们永远不应该忘记编译时间开销。
42+
因此,如果对较小的输入只使用一次编译表达式,那么总运行时可能更大。
4043

44+
以下值是在`Intel 2.67GHz``GCC 4.4.5 64`位模式下测量的
45+
注意:`ns`表示纳秒(10的-9次幂),`Int`类型。
46+
47+
### 总结
48+
49+
`JIT`编译是一项强大的技术,它能够加速解释执行: `Java、JavaScript、ActionScript`(使用NanoJIT)或正则表达式引擎。

0 commit comments

Comments
 (0)