我使用下面的nginx配置, 看起来好像没问题:
server {
listen 80;
# listen somename:80;
# server_name somename alias another.alias;
location /mock/pay/ {
root html/mock-pay;
index index.html index.htm;
}
}
但是一直访问出现404 :
# curl http://114.132.63.169/mock/pay/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
我的文件确实是存放在 /usr/local/nginx/html/mock-pay 目录下的, 为什么就访问不到呢? 把 location /mock/pay/ 改成 location /mock/pay,
或者把root html/mock-pay 改成root html/mock-pay/ , 仍然还是一样的错误。
看看 错误日志, 发现了原因:
[root@VM-0-12-centos nginx]# tailf logs/error.log
2022/10/14 11:47:06 [error] 15205#0: *61238 open() "/usr/local/nginx/html/mock/pay" failed (2: No such file or directory), client: 114.132.63.169, server: , request: "GET /mock/pay HTTP/1.1", host: "114.132.63.169"
2022/10/14 11:47:35 [error] 15205#0: *61239 "/usr/local/nginx/html/mock-pay/mock/pay/index.html" is not found (2: No such file or directory), client: 114.132.63.169, server: , request: "GET /mock/pay/ HTTP/1.1", host: "114.132.63.169"
2022/10/14 11:47:57 [error] 15205#0: *61240 open() "/usr/local/nginx/html/images/jftc_18.jpg" failed (2: No such file or directory), client: 106.52.223.241, server: , request: "GET /images/jftc_18.jpg HTTP/1.1", host: "114.132.63.169"
原来如此啊, 路径拼接错误了!!
怎么办? 其实改成alias 就好了 :
location /mock/pay/ {
alias html/mock-pay/;
index index.html index.htm;
}
好了, o゜▽゜)o☆[BINGO!] !!
但是呢, alias html/mock-pay/ 的末尾不能不加斜杠, 否则, 一样是返回 404 :
[root@VM-0-12-centos nginx]# curl http://114.132.63.169/mock/pay/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@VM-0-12-centos nginx]# curl http://114.132.63.169/mock/pay
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@VM-0-12-centos nginx]#
[root@VM-0-12-centos nginx]# curl http://114.132.63.169/mock/pay/index.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
此时的nginx 错误日志是:
2022/10/14 11:49:00 [error] 15856#0: *61244 "/usr/local/nginx/html/mock-pay/mock/pay/index.html" is not found (2: No such file or directory), client: 114.132.63.169, server: , request: "GET /mock/pay/ HTTP/1.1", host: "114.132.63.169"
2022/10/14 11:53:42 [error] 16416#0: *61256 directory index of "/usr/local/nginx/html/" is forbidden, client: 114.132.63.169, server: , request: "GET / HTTP/1.1", host: "114.132.63.169"
2022/10/14 11:55:48 [error] 16416#0: *61259 open() "/usr/local/nginx/html/mock-payindex.html" failed (2: No such file or directory), client: 114.132.63.169, server: , request: "GET /mock/pay/index.html HTTP/1.1", host: "114.132.63.169"
2022/10/14 11:53:42 [error] 16416#0: *61256 directory index of "/usr/local/nginx/html/" is forbidden, client: 114.132.63.169, server: , request: "GET / HTTP/1.1", host: "114.132.63.169"
总结:
其实在 location 后面跟有path 的时候, 我们需要搞清楚 location 中 root 和 alias 的含义, 否则就拼接错误.. 当然, 如果是 location / {... } 这样的配置, 其中使用root 或alias 都一样的效果, 因为拼接的结果也是一样的。