Step1:首先,我们从最简单的开始——
nginx.conf
文件为:
server {
listen 80;
server_name shit.com;
location / {
root /etc/nginx/html/shit;
index index.html;
}
}
root:/# cat /etc/nginx/html/shit/index.html
<html> this dir is <h1>/etc/nginx/html/shit/index.html</h1> !! </html>
那么我们在浏览器上访问得到的结果是什么?
Step2:其次,我们给他加点料
nginx.conf
修改为
server {
listen 80;
server_name shit.com;
location / {
root /etc/nginx/html/shit;
index index.html;
}
}
server {
listen 80;
server_name shit.com;
location / {
root /etc/nginx/html/shit;
index index.html;
}
location = / {
root /etc/nginx/html/fuckshit;
index index.html;
}
}
root:/# cat /etc/nginx/html/fuckshit/index.html
<html> This is <h1>/etc/nginx/html/fuckshit/index.html</h1>!! </html>
那么这次输入shit.com
会出现什么情况呢?
当当当当~
看到这结果是不是很惊讶?!你们很有可能会想,是不是我没有刷新再骗你们,或者你们会说是不是因为浏览器内部的缓存导致的这样的结果。但是事实上,就算我们在等很长时间,或者换用一个新的从没访问过这个网址的浏览器来访问他也是同样的效果。
那么这是什么情况呢?是不是我们的精准匹配没起作用呢?我们继续往下看:——
Step3:揭秘nginx访问的神秘面纱
nginx.conf
配置文件为:
server {
listen 80;
server_name shit.com;
location / {
root /etc/nginx/html/shit;
index index.html;
}
location = / {
root /etc/nginx/html/fuckshit;
index index.html;
}
location = /index.html {
root /etc/nginx/html/test;
index index.html;
}
}
root:/# cat /etc/nginx/html/test/index.html
<html> This is <h1>/etc/nginx/html/test/index.html</h1> </html>
然后我们继续观察情况:——
访问shit.com
的结果为:
怎么样,是否看出了一些端倪儿?!为什么会显示test下的那个网页呢?!
好,我们现在就来揭开的他的访问神秘面纱!——
- 原来当我们在浏览器中输入了
shit.com
,等价于这个地址shit.com/
,于是根据精准匹配就匹配到
location = / {
root /etc/nginx/html/fuckshit;
index index.html;
}
然后检查root根目录和location组成的地址是否有效,但是由于组合起来的地址是/etc/nginx/html/fuckshit/
,这是一个目录,无法对客户端进行回应。于是这个时候的index
标签救起了作用,然后进行第二步的判断;
- 首先我们需要判断的是,
location
中的root
目录下是否含有index
标签下的文件,如果全部都没有的话,会报403 Forbidden
的错误,例如:
nginx.conf
文件内容为:
server {
listen 80;
server_name shit.com;
location / {
root /etc/nginx/html/shit;
index index.html;
}
location = / {
root /etc/nginx/html/fuckshit;
index index.htm; ##注意啦,我们的fuckshit目录下是没有index.htm这个文件的
}
location = /index.html {
root /etc/nginx/html/test;
index index.html;
}
}
然后我们访问
查看错误日志——
2019/01/08 10:05:25 [error] 6216#6216: *263 directory index of "/etc/nginx/html/fuckshit/" is forbidden, client: 127.0.0.1, server: shit.com, request: "GET / HTTP/1.1", host: "shit.com"
如果存在该文件,那么nginx
服务器内部才继续进行下面的步骤(我们先将nginx
配置文件改回来):首先是将接收到的请求栏shit.com
转换成shit.com/index.html
,然后再次匹配,我们发现正好精准匹配上以下的location
location = /index.html {
root /etc/nginx/html/test;
index index.html;
}
于是,访问的页面就会出现上述的结果。
以上。