Skip to content

Commit c64e6f9

Browse files
committed
update readme
1 parent 4638ad2 commit c64e6f9

10 files changed

+3527
-1568
lines changed

Diff for: Lua-Script/lua-basic.md

+486
Large diffs are not rendered by default.

Diff for: Nginx/nginx-basic.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
## Nginx 基础知识
3+
---
4+
+ [NGINX 所有 Modules](https://www.nginx.com/resources/wiki/modules/)
5+
+ [Nginx 配置文件 nginx.conf 详解](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-base-config.md)
6+
#### agentzh的Nginx教程(版本2016.07.21)
7+
+ [agentzh的Nginx教程地址](https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html)
8+
+ Nginx 变量漫谈(一)
9+
+ Nginx 变量的值只有一种类型,那就是字符串
10+
+ Nginx “变量插值”
11+
```bash
12+
location /test {
13+
set $first "hello ";
14+
echo "${first}world";
15+
}
16+
```
17+
+ set 指令(以及前面提到的 geo 指令)不仅有赋值的功能,它还有创建 Nginx 变量的副作用,即当作为赋值对象的变量尚不存在时
18+
+ Nginx 变量一旦创建,其变量名的可见范围就是整个 Nginx 配置,甚至可以跨越不同虚拟主机的 server 配置块
19+
+ Nginx 变量的生命期是不可能跨越请求边界的
20+
+ Nginx 变量漫谈(二)
21+
+ 跳转
22+
+ 内部跳转:就是在处理请求的过程中,于服务器内部,从一个 location 跳转到另一个 location 的过程。
23+
+ 外部跳转: HTTP 状态码 301 和 302 所进行的“外部跳转”
24+
+ 标准 ngx_rewrite 模块的 rewrite 配置指令其实也可以发起“内部跳转”
25+
+ Nginx 核心和各个 Nginx 模块提供的“预定义变量”
26+
+ Nginx 会在匹配参数名之前,自动把原始请求中的参数名调整为全部小写的形式
27+
+ 如果你尝试改写另外一些只读的内建变量,比如 $arg_XXX 变量,在某些 Nginx 的版本中甚至可能导致进程崩溃。
28+
+ Nginx 变量漫谈(四)
29+
+ map 指令:用于定义两个 Nginx 变量之间的映射关系,或者说是函数关系
30+
+ map 指令只能在 http 块中使用
31+
+ map 配置指令的工作原理是为用户变量注册 “取处理程序”,并且实际的映射计算是在“取处理程序”中完成的,而“取处理程序”只有在该用户变量被实际读取时才会执行(当然,因为缓存的存在,只在请求生命期中的第一次读取中才被执行),所以对于那些根本没有用到相关变量的请求来说,就根本不会执行任何的无用计算。
32+
+ Nginx 变量漫谈(四)
33+
+ [Nginx的11个Phases](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-phases.md)
34+
+ [Nginx 陷阱和常见错误](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-1-config.md)
35+
+ [Nginx 高并发系统内核优化](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-parameter-config.md)

Diff for: Nginx/nginx-high-basic.md

+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
## <a name="Nginx_Web_knowledge"/> Nginx高性能WEB服务器详解
2+
#### <a name="Nginx_Web1_knowledge"/> 第一章 初探
3+
+ [Nginx 编译安装以及参数详解](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-2-config.md)
4+
+ NGINX变量详解
5+
- [x] [nginx变量使用方法详解笔记(1)](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Develop/notes-1.md)
6+
- [x] [nginx变量使用方法详解笔记(2)](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Develop/notes-2.md)
7+
- [x] [nginx变量使用方法详解笔记(3)](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-2-config.md)
8+
+ Nginx指令执行顺序
9+
- [x] [Nginx指令执行命令(01)](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Develop/command-order-01.md)
10+
#### <a name="Nginx_Web2_knowledge"/> 第二章 安装部署
11+
+ 启动错误:`Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)`,执行:`sudo fuser -k 80/tcp`
12+
+ [基于域名、IP的虚拟主机配置](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-2-4-all-config.md)
13+
+ [完整、标准配置实际示列](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-2-4-basic-config.md)
14+
+ [日志文件配置与切割](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-2-4-log-cut.md)
15+
+ alias 和 root 在location 下的应用
16+
- 通过alias 实现别名功能
17+
```
18+
location /live {
19+
alias /home/tinywan/HLS/;
20+
}
21+
```
22+
- curl 请求结果
23+
```
24+
tinywan@tinywan:~/HLS$ cat index.html
25+
alias /home/tinywan/HLS/index.html
26+
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/index.html
27+
alias /home/tinywan/HLS/index.html
28+
```
29+
- 结论:
30+
1. cul 请求 `/live/index.html`,那么Nginx将会在服务器上查找`/home/tinywan/HLS/index.html` 文件
31+
1. 请求的`url` 中的`location`后面的部分会被追加到`alias `指定的目录后面,而`location`后面的`/live`路径将会别自动抛弃
32+
- 类似案例[2]:
33+
- config配置信息
34+
```
35+
location ~ ^/live/(.*)$ {
36+
alias /home/tinywan/HLS/$1;
37+
}
38+
```
39+
- curl 请求结果
40+
```
41+
tinywan@tinywan:~/HLS$ pwd
42+
/home/tinywan/HLS
43+
tinywan@tinywan:~/HLS$ cat txt.txt
44+
txt file
45+
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/txt.txt
46+
txt file
47+
```
48+
- 如果url请求`/live/txt.txt`那么Nginx将会在服务器上查找`/home/tinywan/HLS/txt.txt` 文件
49+
- **与root 功能的差别**:
50+
- config配置信息,注意:一下的`alias` 换成 `root `
51+
```
52+
location ~ ^/live/(.*)$ {
53+
root /home/tinywan/HLS/$1;
54+
}
55+
```
56+
- curl 请求结果
57+
```
58+
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/txt.txt
59+
<html>
60+
<head><title>404 Not Found</title></head>
61+
<body bgcolor="white">
62+
<center><h1>404 Not Found</h1></center>
63+
<hr><center>openresty/1.11.2.1</center>
64+
</body>
65+
</html>
66+
```
67+
- 日志文件信息(打开Nginx的rewrite日志:rewrite_log on;):
68+
```
69+
/home/tinywan/HLS/txt.txt/live/txt.txt
70+
```
71+
- **二者的区别**
72+
1. `alias` 指定的目录是当前目录
73+
1. `root` 指定的是根目录
74+
1. 一般建议的`location /`中通过`root`命令配置目录,其他目录匹配的位置使用`alias`命令
75+
- 案例[3]:
76+
- config配置信息
77+
```
78+
location ~ ^/live/(\w+)/(.*) {
79+
alias /home/tinywan/HLS/live/$1/$2;
80+
}
81+
```
82+
- curl 请求结果
83+
```
84+
tinywan@tinywan:~/HLS/live/stream123$ pwd
85+
/home/tinywan/HLS/live/stream123
86+
tinywan@tinywan:~/HLS/live/stream123$ cat index.m3u8
87+
12312312312
88+
tinywan@tinywan:~/HLS/live/stream123$ curl "http://127.0.0.1/live/stream123/index.m3u8?token=1234&api=009132"
89+
12312312312
90+
```
91+
#### <a name="Nginx_Web3_knowledge"/> 第三章 架构初探
92+
- [ ] 测试一
93+
#### <a name="Nginx_Web4_knowledge"/> 第四章 高级配置
94+
+ 基本语法:location [=|~|~*|^~] /uri/ { … }
95+
1. `= `:严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
96+
2. `~ `:为区分大小写匹配(可用正则表达式)
97+
3. `!~ `:为区分大小写不匹配
98+
4. `!~*`:为不区分大小写不匹配
99+
5. ` ^~ `:如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式
100+
+ [Perl 正则表达式参考](http://www.runoob.com/perl/perl-regular-expressions.html)
101+
+ 正则中需要转义的特殊字符小结
102+
- [1] ` $` 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 ‘\n' 或 ‘\r'。要匹配 $ 字符本身,请使用 \$。
103+
- [2] ` ( )` 标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 和。
104+
- [3] ` * ` 匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 \*。
105+
- [4] ` +` 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+。
106+
- [5] ` . ` 匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \。
107+
- [6] ` [ ]` 标记一个中括号表达式的开始。要匹配 [,请使用 \[。
108+
- [7] ` ? ` 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。
109+
- [8] ` \ ` 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n' 匹配字符 ‘n'。'\n' 匹配换行符。序列 ‘\\' 匹配 “\”,而 ‘\(' 则匹配 “(”。
110+
- [9] ` ^ ` 匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 \^。
111+
- [10] ` { }` 标记限定符表达式的开始。要匹配 {,请使用 \{。
112+
- [11] ` | ` 指明两项之间的一个选择。要匹配 |,请使用 \|。
113+
114+
+ 正则表达式 (Regular expression) 匹配location
115+
- [1] `location ~* \.(gif|jpg|jpeg)$ { }`:匹配所有以 gif,jpg或jpeg 结尾的请求
116+
- [2] `location ~ /documents/Abc { }`:匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
117+
- [3] **目录匹配:**
118+
1. 可以匹配静态文件目录`(static/lib)`
119+
2. HLS直播目录`(/home/HLS/stream123/index.m3u8)`
120+
3. HLS/MP4/FLV点播视频目录`(/home/HLS/stream123.m3u8)`
121+
4. 匹配URL地址:`http://127.0.0.1/live/stream123/index.m3u8`
122+
5. nginx.conf 配置信息
123+
```
124+
# 匹配任何以/live/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试
125+
location ^~ /live/ {
126+
root /home/tinywan/HLS/;
127+
}
128+
# 以上匹配成功后的组合:/home/tinywan/HLS/live/....
129+
```
130+
+ 后缀匹配
131+
1. 匹配任何后缀文件名`gif|jpg|jpeg|png|css|js|ico|m3u8|ts` 结尾的请求
132+
2. TS 文件匹配`http://127.0.0.1/live/stream123/11.ts`
133+
3. M3U8 文件匹配`http://127.0.0.1/live/stream123/index.m3u8`
134+
4. 匹配URL地址:`http://127.0.0.1/hls/123.m3u8`
135+
5. nginx.conf 配置信息
136+
```
137+
location ~* \.(gif|jpg|jpeg|png|css|js|ico|m3u8|ts)$ {
138+
root /home/tinywan/HLS/;
139+
}
140+
```
141+
+ HSL直播目录匹配实际案例(请测试上线)
142+
1. 可以后缀文件名:`http://127.0.0.1/live/stream123/index.m3u8`
143+
```
144+
location ^~ /live/ {
145+
root /home/tinywan/HLS/;
146+
}
147+
```
148+
149+
+ [nginx配置location总结及rewrite规则写法](http://seanlook.com/2015/05/17/nginx-location-rewrite/)
150+
#### <a name="Nginx_Web5_knowledge"/> 第五章 Gzip压缩
151+
+ 测试一
152+
#### <a name="Nginx_Web6_knowledge"/> 第六章 Rewrite 功能
153+
+ Rewrite 常用全局变量
154+
+ 请求案例: `curl -G -d "name=Tinywan&age=24" http://127.0.0.1/rewrite_var/1192/index.m3u8`
155+
+ 接受结果:
156+
157+
| 变量 | 值 |描述 |
158+
| --------- | ----------- |----------- |
159+
| $args | name=Tinywan&age=24 |存放URL 请求的指令 |
160+
| $content_length | 0 | 请求头中的Content-length字段|
161+
| $content_type | 0 |请求头中的Content-Type字段 |
162+
| $document_root | /opt/openresty/nginx/html | 当前请求在root指令中指定的值 |
163+
| $document_uri | /rewrite_var/1192/index.m3u8 | 与$uri相同 |
164+
| $host | 127.0.0.1 |请求主机头字段,否则为服务器名称 |
165+
| $http_user_agent | curl/7.47.0 | 客户端agent信息|
166+
| $http_cookie | 0 | COOKIE变量的值|
167+
| $limit_rate | 0 | 限制连接速率|
168+
| $request_body_file | null | 客户端请求主体信息的临时文件名|
169+
| $request_method | GET | 客户端请求的动作,通常为GET或POST |
170+
| $remote_addr | 127.0.0.1 |客户端的IP地址 |
171+
| $remote_port | 33516 |客户端端口|
172+
| $remote_user | 0 | 已经经过Auth Basic Module验证的用户名|
173+
| $request_filename | /opt/openresty/nginx/html/rewrite_var/1192/index.m3u8 |当前请求的文件路径 |
174+
| $request_uri | /rewrite_var/1192/index.m3u8?name=Tinywan&age=24 |包含请求参数的原始URI,不包含主机名 |
175+
| $query_string | name=Tinywan&age=24 | 与$args相同|
176+
| $scheme | http |HTTP方法(如http,https |
177+
| $server_protocol | HTTP/1.1 |请求使用的协议,通常是HTTP/1.0或HTTP/1.1 |
178+
| $server_addr | 127.0.0.1 |服务器地址 |
179+
| $server_name | localhost | 服务器名称|
180+
| $server_port | 80 |请求到达服务器的端口号 |
181+
| $uri | /rewrite_var/1192/index.m3u8 | 不带请求参数的当前URI|
182+
| $binary_remote_addr | 乱码 | 二进制格式的客户端地址|
183+
184+
+ uri 介绍 **(Nginx中的URI是相对的URI)**
185+
+ URL:`https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md`
186+
+ 绝对URI:`https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md`
187+
+ 相对URI:`/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md`
188+
![Markdown](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/URI-URL-Image.jpg)
189+
190+
+ Rewrite 正则匹配` uri `参数接收
191+
1. 请求案例:`curl http://192.168.18.143/live/tinywan123/index.m3u8`
192+
2. Nginx.conf配置文件
193+
```Lua
194+
location ~* ^/live/(\w+)/(\D+)\.(m3u8|ts)$ {
195+
set $num $2;
196+
set $arg1 $1;
197+
echo "args === ${arg1}";
198+
echo "1==$1 2==$2 3==$3";
199+
echo "Total_numbser :: $num";
200+
echo "URI $uri";
201+
}
202+
203+
```
204+
3. 输出结果
205+
```
206+
args === tinywan123
207+
$1==tinywan123 $2==index $3==m3u8
208+
Total_numbser :: index
209+
URI /live/tinywan123/index.m3u8
210+
Total_numbser ::
211+
```
212+
4. $1为正则匹配多个英文字母或数字的字符串 `(\w+)`
213+
$2 为正则匹配多个非数字 `(\D+)`
214+
$3 为正则匹配的第一个值 `(m3u8|ts)`
215+
`.` 需要用转义字符转义`\.`
216+
## <a name="Nginx_Web7_knowledge"/> 第七章 代理服务
217+
+ [正向代理和反向代理的概念](#title)
218+
+ [正向代理服务](#title)
219+
+ [反向代理的服务](#title)
220+
+ [Nginx日志服务](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-2-Log.md)
221+
+ 负载均衡
222+
+ HTTP负载均衡
223+
- [x] [简单的负载平衡](http://nginx.org/en/docs/http/ngx_http_core_module.html?&_ga=1.179030369.49817296.1480411319#http)
224+
- [x] [简单的负载平衡](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-7-Proxy-1.md)
225+
- [x] [负载均衡五个配置实例](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-7-Proxy.md)
226+
- [x] [Openresty-Lua动态修改upstream后端服务](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/openresty-nginx-lua-Proxy.md)
227+
+ TCP负载均衡
228+
- [x] [Module ngx_stream_core_module](http://nginx.org/en/docs/stream/ngx_stream_core_module.html#stream)
229+
- [x] [负载均衡](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-8-tcp-Proxy.md)
230+
+ proxy_pass 代理的URL总结
231+
+ 在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
232+
+ 将url中以/wap/开头的请求转发到后台对应的某台server上,注意最后的?$args,表明把原始url最后的get参数也给代理到后台
233+
```bash
234+
location ~* /wap/(\d+)/(.+)
235+
{
236+
proxy_pass http://mx$1.test.com:6601/$2?$args;
237+
}
238+
```
239+
+ 第一种配置,访问:`http://127.0.0.1/proxy/index.html` 会被代理到:`http://127.0.0.1:8000/index.html`
240+
```bash
241+
location /proxy/ {
242+
proxy_pass http://127.0.0.1:8000/;
243+
}
244+
```
245+
+ 第二种配置,访问:`http://127.0.0.1/proxy/index.html` 会被代理到:`http://127.0.0.1:8000/proxy/index.html`
246+
```bash
247+
location /proxy/ {
248+
proxy_pass http://127.0.0.1:8000;
249+
}
250+
```
251+
+ 第三种配置,访问:`http://127.0.0.1/proxy/index.html` 会被代理到:`http://127.0.0.1:8000/video/index.html`
252+
```bash
253+
location /proxy/ {
254+
proxy_pass http://127.0.0.1:8000/video/;
255+
}
256+
```
257+
+ 第四种配置,访问:`http://127.0.0.1/proxy/index.html` 会被代理到:`http://127.0.0.1:8000/videoindex.html`
258+
```bash
259+
location /proxy/ {
260+
proxy_pass http://127.0.0.1:8000/video;
261+
}
262+
```
263+
+ location 直接访问:
264+
+ 以下配置,当访问:`http://127.0.0.1:8000/proxy/index.html` 会被匹配到:`/usr/local/nginx/html/proxy/index.html`
265+
```bash
266+
location /proxy/ {
267+
root /usr/local/nginx/html;
268+
index index.html index.htm;
269+
}
270+
```
271+
272+
## <a name="Nginx_Web8_knowledge"/> 第八章 缓存机制
273+
+ 测试一
274+
## <a name="Nginx_Web9_knowledge"/> 第九章 Nginx初探1
275+
+ 测试一
276+
## <a name="Nginx_Web10_knowledge"/> 第十章 Nginx初探1
277+
+ 测试一

0 commit comments

Comments
 (0)