Nginx 配置“/”的作用

在这里插入图片描述

location 路径正则匹配

在这里插入图片描述
匹配优先级:

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
  1. 精确匹配:
    =前缀的指令严格匹配这个查询。如果找到,停止搜索。
  2. 普通字符匹配:
    所有剩下的常规字符串,最长的匹配。
    如果这个匹配使用^〜前缀,搜索停止。
  3. 正则匹配:
    正则表达式,在配置文件中定义的顺序,匹配到一个结果,搜索停止;
  4. 默认匹配:
    如果第3条规则产生匹配的话,结果被使用。
    否则,如同从第2条规则被使用。

location uri正则表达式

. :匹配除换行符以外的任意字符
? :重复0次或1+ :重复1次或更多次
* :重复0次或更多次
\d :匹配数字
^ :匹配字符串的开始
$ :匹配字符串的结束
{
    
    n} :重复n次
{
    
    n,} :重复n次或更多次
[c] :匹配单个字符c
[a-z] :匹配a-z小写字母的任意一个
(a|b|c) : 属线表示匹配任意一种情况,每种情况使用竖线分隔,一般使用小括号括括住,匹配符合a字符 或是b字符 或是c字符的字符串
\ 反斜杠:用于转义特殊字符

location目录匹配详解

依访问地址:http://www.wandouduoduo.com/wddd/index.html为例,nginx配置如下:

location /wddd/ {
    
    
    proxy_connect_timeout 18000; ##修改成半个小时
    proxy_send_timeout 18000;
    proxy_read_timeout 18000;
    proxy_pass http://127.0.0.1:8080;
}

总结:location如果没有“/”时,请求就可以模糊匹配以字符串开头的所有字符串,而有“/”时,只能精确匹配字符本身。

  • 配置location /wandou可以匹配/wandoudouduo请求,也可以匹配/wandou*/duoduo等等,只要以wandou开头的目录都可以匹配到。
  • 配置location /wandou/必须精确匹配/wandou/这个目录的请求,不能匹配/wandouduoduo/或/wandou*/duoduo等请求。

proxy_pass有无“/”的四种区别探究

访问地址都是以:http://www.wandouduoduo.com/wddd/index.html 为例。请求都匹配目录/wddd/

第一种:加"/"

   location  /wddd/ {
    
    
    proxy_pass  http://127.0.0.1:8080/;
   }

测试结果,请求被代理跳转到:http://127.0.0.1:8080/index.html

第二种: 不加"/"

	location  /wddd/ {
    
    
    proxy_pass http://127.0.0.1:8080;
   }

测试结果,请求被代理跳转到:http://127.0.0.1:8080/wddd/index.html

第三种: 增加目录加"/"

location  /wddd/ {
    
    
    proxy_pass http://127.0.0.1:8080/sun/;
   }

测试结果,请求被代理跳转到:http://127.0.0.1:8080/sun/index.html

第四种:增加目录不加"/"

location  /wddd/ {
    
    
    proxy_pass http://127.0.0.1:8080/sun;
   }

测试结果,请求被代理跳转到:http://127.0.0.1:8080/sunindex.html

总结

  • location目录后加"/",只能匹配目录,不加“/”不仅可以匹配目录还对目录进行模糊匹配。
  • proxy_pass无论加不加“/”,代理跳转地址都直接拼接。

大量实例

server {
    
    
  listen 80;
  server_name localhost;
  
  # http://localhost/wddd01/xxx -> http://localhost:8080/wddd01/xxx
  location /wddd01/ {
    
    
    proxy_pass http://localhost:8080;
  }

  # http://localhost/wddd02/xxx -> http://localhost:8080/xxx
  location /wddd02/ {
    
    
    proxy_pass http://localhost:8080/;
  }

  # http://localhost/wddd03/xxx -> http://localhost:8080/wddd03*/xxx
  location /wddd03 {
    
    
    proxy_pass http://localhost:8080;
  }

❤ 看成是 /xxx直接拼接上去了
  # http://localhost/wddd04/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。
  location /wddd04 {
    
    
    proxy_pass http://localhost:8080/;
  }

  # http://localhost/wddd05/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。
  location /wddd05/ {
    
    
    proxy_pass http://localhost:8080/haha;
  }

  # http://localhost/wddd06/xxx -> http://localhost:8080/haha/xxx
  location /wddd06/ {
    
    
    proxy_pass http://localhost:8080/haha/;
  }# http://localhost/wddd07/xxx -> http://localhost:8080/haha/xxx
  location /wddd07 {
    
    
    proxy_pass http://localhost:8080/haha;
  }
        
  # http://localhost/wddd08/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。
  location /wddd08 {
    
    
    proxy_pass http://localhost:8080/haha/;
  }

Guess you like

Origin blog.csdn.net/qq_39578545/article/details/120927393