【转】Nginx配置location总结及rewrite规则写法

简介: 转载:https://segmentfault.com/a/1190000002797606 http://www.cnblogs.com/lidabo/p/4169396.htmlnginx 配置文件,自下到上分为三种层次分明的结构: |    http block        the p...

 转载:https://segmentfault.com/a/1190000002797606

 http://www.cnblogs.com/lidabo/p/4169396.html

nginx 配置文件,自下到上分为三种层次分明的结构:
 |    http block        the protocol level
 |      server block        the server level
 V        location block        the requested URI

Nginx 允许用户定义 Location block ,并指定一个匹配模式(pattern)匹配特定的 URI。除了简单的字符串(比如文件系统路径),还允许使用更为复杂的匹配模式(pattern)。
Location block 的基本语法形式是:

location [=|~|~*|^~|@] pattern { ... }

[=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)。

location正则写法

location  = / {
  # 精确匹配 / ,主机名后面不能带任何字符串
  [ configuration A ] 
}

location  / {
  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  # 但是正则和最长字符串会优先匹配
  [ configuration B ] 
}

location /documents/ {
  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration C ] 
}

location ~ /documents/Abc {
  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration CC ] 
}

location ^~ /images/ {
  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
  [ configuration D ] 
}

location ~* \.(gif|jpg|jpeg)$ {
  # 匹配所有以 gif,jpg或jpeg 结尾的请求
  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
  [ configuration E ] 
}

location /images/ {
  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
  [ configuration F ] 
}

location /images/abc {
  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
  # F与G的放置顺序是没有关系的
  [ configuration G ] 
}

location ~ /images/abc/ {
  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
    [ configuration H ] 
}

location ~* /js/.*/\.js
  • 已=开头表示精确匹配
  • 如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
  • ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配
  • / 通用匹配, 如果没有其它匹配,任何请求都会匹配到

顺序 no优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

上面的匹配结果 按照上面的location写法,以下的匹配示例成立:

  • / -> configuration A
    精确完全匹配,即使/index.html也匹配不了
  • /downloads/download.html -> configuration B
    匹配B以后,往下没有任何匹配,采用B
  • /images/1.gif -> configuration D
    匹配到F,往下匹配到D,停止往下
  • /images/abc/def -> configuration D
    最长匹配到G,往下匹配D,停止往下
    你可以看到 任何以/images/开头的都会匹配到D并停止,FG写在这里是没有任何意义的,H是永远轮不到的,这里只是为了说明匹配顺序
  • /documents/document.html -> configuration  C
    匹配到C,往下没有任何匹配,采用C
  • /documents/1.jpg -> configuration E
    匹配到C,往下正则匹配到E
  • /documents/Abc.jpg -> configuration CC
    最长匹配到C,往下正则顺序匹配到CC,不会往下到E

[ configuration A ] 精准匹配

这会完全匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。

server {
    server_name website.com;
    location = /abcd {
    […]
    }
}

匹配情况:

  1. http://website.com/abcd        # 正好完全匹配
  2. http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
  3. http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
  4. http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
  5. http://website.com/abcde    # 不匹配,因为不是完全匹配

[ configuration B ] 精准匹配

可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。

server {
    server_name website.com;
    location /abcd {
    […]
    }
}

匹配情况:

  1. http://website.com/abcd        # 正好完全匹配
  2. http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
  3. http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
  4. http://website.com/abcd/    # 末尾存在反斜杠(trailing slash)也属于匹配范围内
  5. http://website.com/abcde    # 仍然匹配,因为 URI 是以 pattern 开头的

[ configuration CC ] 正则匹配(区分大小写)

这个 location modifier 对大小写敏感,且 pattern 须是正则表达式

server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}

匹配情况:

  1. http://website.com/abcd        # 完全匹配
  2. http://website.com/ABCD        # 不匹配,~ 对大小写是敏感的
  3. http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
  4. http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
  5. http://website.com/abcde    # 不匹配正则表达式 ^/abcd$

注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

[ configuration CD ] 正则匹配(不区分大小写)

与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式

server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}

匹配情况:

  1. http://website.com/abcd        # 完全匹配
  2. http://website.com/ABCD        # 匹配,这就是它不区分大小写的特性
  3. http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
  4. http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
  5. http://website.com/abcde    # 不匹配正则表达式 ^/abcd$

 

 

 

 

实际使用建议

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}

 

普通匹配和正则匹配优先级:

如果我们访问:http://127.0.0.1:8080/image/aiai.png

此时, “/” ”/image/aiai.png” 匹配,同时,”image”正则 ”image/logo.png”也能匹配,谁发挥作用?

答案:正则表达式的成果将会使用!

    location / {
            root   D:\wnmp\www\html;
            index  index.html index.htm index.php;
        }
        
        location ~ image {
            root D:\wnmp\www; //在这里要注意了,如果location 正则写image了则的 root 路径中不可以在次写image
            index index.html;
        }

图片真正会访问:D:\wnmp\www\image\aiai.png (而不是:D:\wnmp\www\html\image\aiai.png)

 

普通长短优先级

我们访问 http://127.0.0.1:8080/foo

location / {
             root   /usr/local/nginx/html;
             index  index.html index.htm;
         }
 
location /foo {
            root /var/www/html;
             index index.html;
}

 对于uri “/foo”,   两个location的patt,都能匹配他们,即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,此时, 真正访问 /var/www/html/index.html 原因:’/foo’匹配的更长,因此使用之:

 

 


 


 

location ~ /hls123/(\d+).m3u8$ {
                #设置nginx变量
                set $a $1;
                echo $a "::a = : ${a}";
}
curl "http://localhost/hls123/4001489370813.m3u8"
4001489370813 ::a = : 4001489370813

以上的这种是可以是接受参数的 $1 就是这参数

    location ~ \/.+\/.+\.(m3u8|ts) {
                #设置nginx变量
                if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) {
                        set $app_name $1;
                        set $a $2;
                }
                echo "::document = : ${document_uri}";
                echo "::uri = : ${uri}";
                echo "::app_name = : ${app_name}";
                echo "::stream_name = : ${a}";
                #set $stream_id "";
                #default_type 'text/html';
                #lua_code_cache off;
                #rewrite_by_lua_file  /home/www/lua-tinywan/set_by_file.lua;
                #echo "stream_id :" $stream_id;
                #proxy_pass $stream_id;
        }
curl "http://localhost/hls123/4001489370813.m3u8"
::document = : /hls123/4001489370813.m3u8
::uri = : /hls123/4001489370813.m3u8
::app_name = : hls123
::stream_name = : 4001489370813

可以使用上面的这个方法获取需要的参数(不会)

 

目录
相关文章
|
25天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
28 0
|
24天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
99 0
|
23小时前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
13 0
|
1天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
8 0
|
4天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
15 0
|
6天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
7天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
14 0
|
17天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
25 0
|
18天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
33 0
|
27天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0