首页 Linux

Nginx配置中一个不起眼字符"/"的巨大作用,失之毫厘谬以千里

2022-03-03 14:53 简书

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

10439291-75019a08f04e9e0a

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

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

原因就是:Nginx在配置proxy_pass代理转接时,少些“/”字符造成的。有同学就有疑问,加不加“/”,区别真的那么大吗?我们带着这个疑问,来探究下这个问题。

location目录匹配详解

nginx每个location都是一个匹配目录,nginx的策略是:访问请求来时,会对访问地址进行解析,从上到下逐个匹配,匹配上就执行对应location大括号中的策略,并根据策略对请求作出相应。

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

location/wddd/ {

    proxy_connect_timeout18000; #修改成半个小时

    proxy_send_timeout18000;

    proxy_read_timeout18000;

    proxy_pass   http://127.0.0.1:8080;

}

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

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

下面举个例子说明:

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

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

访问地址都是以:http://www.yuchen.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 {

    listen80;

    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;

}

# 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/api6/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/;

}

}

返回首页
返回顶部