Nginx 配置中一个字符 “/” 的作用失之毫厘谬以千里

Nginx作为一个轻量级的,高性能的 web 服务软件,因其占有内存少,并发能力强的特点,而广受欢迎和使用。国内很多大型互联网公司也对Nginx很是青睐。很多大厂像 BAT(百度,阿里和腾讯),TMD(头条,美团和滴滴)等等。

Nginx 配置中一个字符

使用过Nginx的同学都知道,你只需要按需求准确的更改好配置启动,那么就可以优雅的访问它了。所以说Nginx对配置文件的很是看中呢,这就要求我们更改配置文件时一定要再三确认,要不然可能因为疏忽而引发惨案呢?真实案例,就因为在配置时,少写了一个字符“/”,就造成访问不通报错,因而接到投诉。那么是怎么引起的呢?原因就是:Nginx在配置 proxy_pass 代理转接时,少些“/”字符造成的。有同学就有疑问,加不加“/”,区别真的那么大吗?我们带着这个疑问,来探究下这个问题。

location 目录匹配详解
nginx 每个 location 都是一个匹配目录,nginx 的策略是:访问请求来时,会对访问地址进行解析,从上到下逐个匹配,匹配上就执行对应 location 大括号中的策略,并根据策略对请求作出相应。
依访问地址:http://www.iwmyx.cn/wmyx/index.html 为例,nginx 配置如下:

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

那访问时就会匹配这个 location,从而把请求代理转发到本机的 8080 服务中,8080 服务相应后,信息原路返回。

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

下面举个例子说明:
配置 location /wmyx 可以匹配/wmyxxxxx 请求,也可以匹配/wmyx*/xxxx 等等,只要以 wmyx 开头的目录都可以匹配到。而 location /wmyx/必须精确匹配/wmyx/这个目录的请求,不能匹配/wmyxxxxx/或/wmyx*/xxxx 等请求。
proxy_pass 有无“/”的四种区别探究
访问地址都是以:http://www.iwmyx.cn/wmyx/index.html 为例。请求都匹配目录/wmyx/

第一种:加”/”

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

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

第二种: 不加”/”

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

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

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

location  /wmyx/ {
    proxy_pass http://127.0.0.1:8080/xxxx/;
   }

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

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

location  /wmyx/ {
    proxy_pass http://127.0.0.1:8080/xxxx;
   }

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

总结
location 目录后加”/”,只能匹配目录,不加“/”不仅可以匹配目录还对目录进行模糊匹配。而 proxy_pass 无论加不加“/”,代理跳转地址都直接拼接。
为了加深大家印象可以用下面的配置实验测试下:

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

  # http://localhost/wmyx02/xxxx -> http://localhost:8080/xxxx
  location /wmyx02/ {
    proxy_pass http://localhost:8080/;
  }

  # http://localhost/wmyx03/xxxx -> http://localhost:8080/wmyx03*/xxxx
  location /wmyx03 {
    proxy_pass http://localhost:8080;
  }

  # http://localhost/wmyx04/xxxx -> http://localhost:8080//xxxx,请注意这里的双斜线,好好分析一下。
  location /wmyx04 {
    proxy_pass http://localhost:8080/;
  }

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

  # http://localhost/api6/xxxx -> http://localhost:8080/aaaa/xxxx
  location /wmyx06/ {
    proxy_pass http://localhost:8080/aaaa/;
  }

  # http://localhost/wmyx07/xxxx -> http://localhost:8080/aaaa/xxxx
  location /wmyx07 {
    proxy_pass http://localhost:8080/aaaa;
  }
        
  # http://localhost/wmyx08/xxxx -> http://localhost:8080/aaaa//xxxx,请注意这里的双斜杠。
  location /wmyx08 {
    proxy_pass http://localhost:8080/aaaa/;
  }
}

 

相关:

彻底搞懂 Nginx 的五大应用场景

Nginx 你了解吗?

彻底搞懂 Nginx 的五大应用场景

© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...