代码拉取完成,页面将自动刷新
Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
,执行:sudo fuser -k 80/tcp
location /live {
alias /home/tinywan/HLS/;
}
tinywan@tinywan:~/HLS$ cat index.html
alias /home/tinywan/HLS/index.html
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/index.html
alias /home/tinywan/HLS/index.html
/live/index.html
,那么Nginx将会在服务器上查找/home/tinywan/HLS/index.html
文件url
中的location
后面的部分会被追加到alias
指定的目录后面,而location
后面的/live
路径将会别自动抛弃 location ~ ^/live/(.*)$ {
alias /home/tinywan/HLS/$1;
}
tinywan@tinywan:~/HLS$ pwd
/home/tinywan/HLS
tinywan@tinywan:~/HLS$ cat txt.txt
txt file
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/txt.txt
txt file
/live/txt.txt
那么Nginx将会在服务器上查找/home/tinywan/HLS/txt.txt
文件alias
换成 root
location ~ ^/live/(.*)$ {
root /home/tinywan/HLS/$1;
}
tinywan@tinywan:~/HLS$ curl http://127.0.0.1/live/txt.txt
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>openresty/1.11.2.1</center>
</body>
</html>
/home/tinywan/HLS/txt.txt/live/txt.txt
alias
指定的目录是当前目录root
指定的是根目录
location /
中通过root
命令配置目录,其他目录匹配的位置使用alias
命令 location ~ ^/live/(\w+)/(.*) {
alias /home/tinywan/HLS/live/$1/$2;
}
tinywan@tinywan:~/HLS/live/stream123$ pwd
/home/tinywan/HLS/live/stream123
tinywan@tinywan:~/HLS/live/stream123$ cat index.m3u8
12312312312
tinywan@tinywan:~/HLS/live/stream123$ curl "http://127.0.0.1/live/stream123/index.m3u8?token=1234&api=009132"
12312312312
基本语法:location [=||*|^~] /uri/ { … }
=
:严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。~
:为区分大小写匹配(可用正则表达式)!~
:为区分大小写不匹配!~*
:为不区分大小写不匹配^~
:如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式正则中需要转义的特殊字符小结
$
匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 ‘\n' 或 ‘\r'。要匹配 $ 字符本身,请使用 $。 ( )
标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 和。*
匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 *。 +
匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 +。 .
匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \。 [ ]
标记一个中括号表达式的开始。要匹配 [,请使用 [。?
匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 ?。\
将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n' 匹配字符 ‘n'。'\n' 匹配换行符。序列 ‘\' 匹配 “\”,而 ‘(' 则匹配 “(”。 ^
匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 ^。 { }
标记限定符表达式的开始。要匹配 {,请使用 {。|
指明两项之间的一个选择。要匹配 |,请使用 |。正则表达式 (Regular expression) 匹配location
location ~* \.(gif|jpg|jpeg)$ { }
:匹配所有以 gif,jpg或jpeg 结尾的请求location ~ /documents/Abc { }
:匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索(static/lib)
(/home/HLS/stream123/index.m3u8)
(/home/HLS/stream123.m3u8)
http://127.0.0.1/live/stream123/index.m3u8
# 匹配任何以/live/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试
location ^~ /live/ {
root /home/tinywan/HLS/;
}
# 以上匹配成功后的组合:/home/tinywan/HLS/live/....
后缀匹配
gif|jpg|jpeg|png|css|js|ico|m3u8|ts
结尾的请求http://127.0.0.1/live/stream123/11.ts
http://127.0.0.1/live/stream123/index.m3u8
http://127.0.0.1/hls/123.m3u8
location ~* \.(gif|jpg|jpeg|png|css|js|ico|m3u8|ts)$ {
root /home/tinywan/HLS/;
}
HSL直播目录匹配实际案例(请测试上线)
http://127.0.0.1/live/stream123/index.m3u8
location ^~ /live/ {
root /home/tinywan/HLS/;
}
Rewrite 常用全局变量
curl -G -d "name=Tinywan&age=24" http://127.0.0.1/rewrite_var/1192/index.m3u8
变量 | 值 | 描述 |
---|---|---|
$args | name=Tinywan&age=24 | 存放URL 请求的指令 |
$content_length | 0 | 请求头中的Content-length字段 |
$content_type | 0 | 请求头中的Content-Type字段 |
$document_root | /opt/openresty/nginx/html | 当前请求在root指令中指定的值 |
$document_uri | /rewrite_var/1192/index.m3u8 | 与$uri相同 |
$host | 127.0.0.1 | 请求主机头字段,否则为服务器名称 |
$http_user_agent | curl/7.47.0 | 客户端agent信息 |
$http_cookie | 0 | COOKIE变量的值 |
$limit_rate | 0 | 限制连接速率 |
$request_body_file | null | 客户端请求主体信息的临时文件名 |
$request_method | GET | 客户端请求的动作,通常为GET或POST |
$remote_addr | 127.0.0.1 | 客户端的IP地址 |
$remote_port | 33516 | 客户端端口 |
$remote_user | 0 | 已经经过Auth Basic Module验证的用户名 |
$request_filename | /opt/openresty/nginx/html/rewrite_var/1192/index.m3u8 | 当前请求的文件路径 |
$request_uri | /rewrite_var/1192/index.m3u8?name=Tinywan&age=24 | 包含请求参数的原始URI,不包含主机名 |
$query_string | name=Tinywan&age=24 | 与$args相同 |
$scheme | http | HTTP方法(如http,https |
$server_protocol | HTTP/1.1 | 请求使用的协议,通常是HTTP/1.0或HTTP/1.1 |
$server_addr | 127.0.0.1 | 服务器地址 |
$server_name | localhost | 服务器名称 |
$server_port | 80 | 请求到达服务器的端口号 |
$uri | /rewrite_var/1192/index.m3u8 | 不带请求参数的当前URI |
$binary_remote_addr | 乱码 | 二进制格式的客户端地址 |
https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md
https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md
/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/config.md
Rewrite 正则匹配uri
参数接收
curl http://192.168.18.143/live/tinywan123/index.m3u8
location ~* ^/live/(\w+)/(\D+)\.(m3u8|ts)$ {
set $num $2;
set $arg1 $1;
echo "args === ${arg1}";
echo "1==$1 2==$2 3==$3";
echo "Total_numbser :: $num";
echo "URI $uri";
}
args === tinywan123
$1==tinywan123 $2==index $3==m3u8
Total_numbser :: index
URI /live/tinywan123/index.m3u8
Total_numbser ::
(\w+)
(\D+)
(m3u8|ts)
.
需要用转义字符转义\.
location ~* /wap/(\d+)/(.+)
{
proxy_pass http://mx$1.test.com:6601/$2?$args;
}
http://127.0.0.1/proxy/index.html
会被代理到:http://127.0.0.1:8000/index.html
location /proxy/ {
proxy_pass http://127.0.0.1:8000/;
}
http://127.0.0.1/proxy/index.html
会被代理到:http://127.0.0.1:8000/proxy/index.html
location /proxy/ {
proxy_pass http://127.0.0.1:8000;
}
http://127.0.0.1/proxy/index.html
会被代理到:http://127.0.0.1:8000/video/index.html
location /proxy/ {
proxy_pass http://127.0.0.1:8000/video/;
}
http://127.0.0.1/proxy/index.html
会被代理到:http://127.0.0.1:8000/videoindex.html
location /proxy/ {
proxy_pass http://127.0.0.1:8000/video;
}
http://127.0.0.1:8000/proxy/index.html
会被匹配到:/usr/local/nginx/html/proxy/index.html
location /proxy/ {
root /usr/local/nginx/html;
index index.html index.htm;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。