|
159 | 159 | #### <a name="Nginx_Web4_knowledge"/> 第四章 高级配置
|
160 | 160 | + 基本语法:location [=|~|~*|^~] /uri/ { … }
|
161 | 161 | 1. `= `:严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
|
162 |
| - 1. `~ `:为区分大小写匹配(可用正则表达式) |
163 |
| - 1. `!~ `:为区分大小写不匹配 |
164 |
| - 1. `!~*`:为不区分大小写不匹配 |
165 |
| - 1. ` ^~ `:如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式 |
| 162 | + 2. `~ `:为区分大小写匹配(可用正则表达式) |
| 163 | + 3. `!~ `:为区分大小写不匹配 |
| 164 | + 4. `!~*`:为不区分大小写不匹配 |
| 165 | + 5. ` ^~ `:如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式 |
166 | 166 | + [Perl 正则表达式参考](http://www.runoob.com/perl/perl-regular-expressions.html)
|
167 | 167 | + 正则中需要转义的特殊字符小结
|
168 | 168 | - [1] ` $` 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 ‘\n' 或 ‘\r'。要匹配 $ 字符本身,请使用 \$。
|
|
182 | 182 | - [2] `location ~ /documents/Abc { }`:匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
|
183 | 183 | - [3] **目录匹配:**
|
184 | 184 | 1. 可以匹配静态文件目录`(static/lib)`
|
185 |
| - 1. HLS直播目录`(/home/HLS/stream123/index.m3u8)` |
186 |
| - 1. HLS/MP4/FLV点播视频目录`(/home/HLS/stream123.m3u8)` |
187 |
| - 1. 匹配URL地址:`http://127.0.0.1/live/stream123/index.m3u8` |
188 |
| - 1. nginx.conf 配置信息 |
| 185 | + 2. HLS直播目录`(/home/HLS/stream123/index.m3u8)` |
| 186 | + 3. HLS/MP4/FLV点播视频目录`(/home/HLS/stream123.m3u8)` |
| 187 | + 4. 匹配URL地址:`http://127.0.0.1/live/stream123/index.m3u8` |
| 188 | + 5. nginx.conf 配置信息 |
189 | 189 | ```
|
190 | 190 | # 匹配任何以/live/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试
|
191 | 191 | location ^~ /live/ {
|
|
195 | 195 | ```
|
196 | 196 | + 后缀匹配
|
197 | 197 | 1. 匹配任何后缀文件名`gif|jpg|jpeg|png|css|js|ico|m3u8|ts` 结尾的请求
|
198 |
| - 1. TS 文件匹配`http://127.0.0.1/live/stream123/11.ts` |
199 |
| - 1. M3U8 文件匹配`http://127.0.0.1/live/stream123/index.m3u8` |
200 |
| - 1. 匹配URL地址:`http://127.0.0.1/hls/123.m3u8` |
201 |
| - 1. nginx.conf 配置信息 |
| 198 | + 2. TS 文件匹配`http://127.0.0.1/live/stream123/11.ts` |
| 199 | + 3. M3U8 文件匹配`http://127.0.0.1/live/stream123/index.m3u8` |
| 200 | + 4. 匹配URL地址:`http://127.0.0.1/hls/123.m3u8` |
| 201 | + 5. nginx.conf 配置信息 |
202 | 202 | ```
|
203 | 203 | location ~* \.(gif|jpg|jpeg|png|css|js|ico|m3u8|ts)$ {
|
204 | 204 | root /home/tinywan/HLS/;
|
|
304 | 304 | + [默认配置信息](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/default-config.md)
|
305 | 305 | + 开发入门
|
306 | 306 | + Nginx与Lua的整体目录关系
|
307 |
| - ```Lua |
| 307 | + ```Bash |
308 | 308 | .
|
309 | 309 | ├── conf
|
310 | 310 | │ ├── nginx.conf -- Nginx 配置文件
|
|
369 | 369 | + nginx.conf 配置信息
|
370 | 370 | ```Lua
|
371 | 371 | http {
|
372 |
| - lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;"; |
373 |
| - server { |
374 |
| - listen 80; |
375 |
| - server_name localhost; |
376 |
| - location ~ \/.+\/.+\.(m3u8|ts) { |
377 |
| - if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) { |
378 |
| - set $app_name $1; |
379 |
| - set $a $2; |
380 |
| - } |
381 |
| - set $stream_id ""; |
382 |
| - default_type 'text/html'; |
383 |
| - rewrite_by_lua_file /opt/openresty/nginx/lua/proxy_pass_cdn.lua; |
384 |
| - proxy_connect_timeout 10; |
385 |
| - proxy_send_timeout 30; |
386 |
| - proxy_read_timeout 30; |
387 |
| - proxy_pass $stream_id; |
| 372 | + lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;"; |
| 373 | + server { |
| 374 | + listen 80; |
| 375 | + server_name localhost; |
| 376 | + location ~ \/.+\/.+\.(m3u8|ts) { |
| 377 | + if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) { |
| 378 | + set $app_name $1; |
| 379 | + set $a $2; |
388 | 380 | }
|
389 |
| -
|
| 381 | + set $stream_id ""; |
| 382 | + default_type 'text/html'; |
| 383 | + rewrite_by_lua_file /opt/openresty/nginx/lua/proxy_pass_cdn.lua; |
| 384 | + proxy_connect_timeout 10; |
| 385 | + proxy_send_timeout 30; |
| 386 | + proxy_read_timeout 30; |
| 387 | + proxy_pass $stream_id; |
390 | 388 | }
|
| 389 | +
|
| 390 | + } |
391 | 391 | }
|
392 | 392 | ```
|
393 | 393 | + [Lua脚本proxy_pass_cdn.lua](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-redis/proxy_pass_cdn.lua)
|
|
466 | 466 | + nginx.conf
|
467 | 467 | ```Lua
|
468 | 468 | location /cjson {
|
469 |
| - content_by_lua_block { |
470 |
| - local cjson = require "cjson" |
471 |
| -
|
472 |
| - local json = cjson.encode({ |
473 |
| - foo = "bar", |
474 |
| - some_object = {}, |
475 |
| - some_array = cjson.empty_array |
476 |
| - }) |
477 |
| - ngx.say(json) |
478 |
| - } |
| 469 | + content_by_lua_block { |
| 470 | + local cjson = require "cjson" |
| 471 | + local json = cjson.encode({ |
| 472 | + foo = "bar", |
| 473 | + some_object = {}, |
| 474 | + some_array = cjson.empty_array |
| 475 | + }) |
| 476 | + ngx.say(json) |
| 477 | + } |
479 | 478 | }
|
480 | 479 | ```
|
481 | 480 | + curl 请求
|
|
508 | 507 | + 基本用法
|
509 | 508 | ```Lua
|
510 | 509 | location /start {
|
511 |
| - content_by_lua_block { |
512 |
| - local session = require "resty.session".start() |
513 |
| - session.data.name = "OpenResty Fan Tinywan" |
514 |
| - session:save() |
515 |
| - ngx.say("<html><body>Session started. ", |
516 |
| - "<a href=/test>Check if it is working</a>!</body></html>") |
517 |
| - ngx.say(session.data.name,"Anonymous") |
518 |
| - } |
519 |
| - } |
| 510 | + content_by_lua_block { |
| 511 | + local session = require "resty.session".start() |
| 512 | + session.data.name = "OpenResty Fan Tinywan" |
| 513 | + session:save() |
| 514 | + ngx.say("<html><body>Session started. ", |
| 515 | + "<a href=/test>Check if it is working</a>!</body></html>") |
| 516 | + ngx.say(session.data.name,"Anonymous") |
| 517 | + } |
| 518 | + } |
520 | 519 | ```
|
521 | 520 | + curl 请求
|
522 | 521 | ```Bash
|
|
0 commit comments