Doraemon

小叮当    2012 - 2023
Doraemon

Choose mode

  • dark
  • auto
  • light
首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub
author-avatar

小叮当

39

Article

25

Tag

首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub

Nginx 匹配规则

小叮当    2012 - 2023

Nginx 匹配规则


小叮当 2020-10-17 Nginx运维

在线工具

Nginx location match tester gzip 检测

参考:

一文理清 nginx 中的 location 配置(系列一)

# Nginx 匹配规则

模式 含义
location = /uri “=” 表示精确匹配,只有完全匹配上才能生效
location ^~ /uri ^~ 开头对 URL 路径进行前缀匹配,并且在正则之前。用来匹配目录,立刻停止后续的正则搜索
location ~ pattern 开头表示区分大小写的正则匹配
location ~* pattern 开头表示不区分大小写的正则匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它 location 的请求都会匹配到,相当于 switch 中的 default
@ "@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
  • 首先精确匹配 =
  • 其次前缀匹配 ^~
  • 其次是按文件中顺序的正则匹配
  • 然后匹配不带任何修饰的前缀匹配。
  • 最后是交给 / 通用匹配
  • 当有匹配成功时候,停止匹配,按当前匹配规则处理请求

# 解释 alias 与 root 的区别

    location /app {
        root /var/www/html;
    }
1
2
3

root 的路径为 /var/www/html/app

    location /app {
        alias /var/www/html;
    }
1
2
3

alias 的路径 /var/www/html

root 是全路径。拼接 location 和 root 设置的路径

alias 相当于重置路径,重置为 alias 设置的路径

# 常用 Nginx 配置

# 1、根目录部署程序

try_files $uri $uri/ /index.html; 解决刷新 404 root /usr/share/nginx/html 真实访问 /usr/share/nginx/html/

    location / {
        root /usr/share/nginx/html;
        # access_log /usr/share/nginx/html/access.log compression;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
1
2
3
4
5
6
# 2、二级虚拟目录部署静态程序

前缀匹配 ^~(立刻停止后续的正则搜索) 关闭缓存:add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";

    location ^~ /a/ {
        #注意最后的“/”, location 和 alias 的最后都有 “/”,如果出现404 500 请注意检查 "/"
        alias /usr/share/nginx/html/;
        add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        index index.html /a/index.html;
        try_files $uri $uri/ /a/index.html;
    }
1
2
3
4
5
6
7
    #前缀匹配 ^~(立刻停止后续的正则搜索)
    location ^~ /aa {
        alias D:\\WWW\Prod\Angular;
        add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        index index.html /aa/index.html;
        try_files $uri $uri/ /aa/index.html;
    }
1
2
3
4
5
6
7
    location /aa {
        alias D:\\WWW\Prod\Angular;
        index index.html /aa/index.html;
        try_files $uri $uri/ /aa/index.html;
    }
1
2
3
4
5
# 2、二级虚拟目录代理 Api 地址
upstream usr_api {
    server localhost:8081;
}
1
2
3
    location ~ ^/usr/(.*)$ {
        proxy_pass http://usr_api/$1$is_args$args;
        proxy_http_version 1.1;
        # .net core OAuth2.0客户端模式代理,未测试通过
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
1
2
3
4
5
6
7
8
9
10
11
12
    location /git/ {
        # 后面的斜杠是一个关键,没有斜杠的话就会传递service到后端节点导致404
        proxy_pass http://10.3.0.3:3000/;
        proxy_redirect off;
        proxy_set_header Host $host; #这里改成proxy_set_header Host $host:2222;也没有用
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
1
2
3
4
5
6
7
8
9
upstream api_usr {
        server www.test.com:8085;
}

        #通过nginx重写后端api地址

        location ~ ^/op/api/others/(.*)$ {
                add_header X-RP-Path 'others';
                proxy_pass http://api_usr/op/api/$1$is_args$args;
        }
1
2
3
4
5
6
7
8
9
10
# 此处为截取请求地址中aa之后的路径,并赋给转发地址
        location ^~ /aa/ {
                if($request_uri~ /aa/ (.*)) {
                        set $bucketid $1;
                }
                proxy_pass http: //$bucketid;
        }
1
2
3
4
5
6
7
        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }
1
2
3
4
5
6

# Nginx 伪静态之 try_files 和 rewrite 讲解

try_files $uri $uri/ /aa/index.html; 这句话是什么意思? 答:读取静态文件

$uri 这个是 nginx 的一个变量,存放着用户访问的地址

比如:http://www.xxx.com/index.html, 那么$uri 就是 /index.html

$uri/ 代表访问的是一个目录,比如:http://www.xxx.com/hello/test/ ,那么$uri/就是 /hello/test/

完整的解释就是:try_files 去尝试到网站目录读取用户访问的文件,如果第一个变量存在,就直接返回;

不存在继续读取第二个变量,如果存在,直接返回;不存在直接跳转到第三个参数上。

  • Nginx 匹配规则
  • 解释 alias 与 root 的区别
  • 常用 Nginx 配置
  • Nginx 伪静态之 try_files 和 rewrite 讲解