当前位置:首页 > 编程笔记 > 正文
已解决

Nginx(七) root和alias的区别及详细测试

来自网友在路上 176876提问 提问时间:2023-11-19 19:51:35阅读次数: 76

最佳答案 问答题库768位专家为你答疑解惑

        本篇文章只讲root和alias的区别,配置文件详解请参考 Nginx(三) 配置文件详解,下面开始进行测试。

        Nginx配置如下:

server {listen 8688 default_server;server_name www.read******.cn;access_log  logs/access.log  format2;root pages;set $sn 1;# 测试1location ^~ /test1/ {root pages/one/;}# 测试2location ^~ /test2/ {alias pages/one/;}# 测试3location ^~ /test3/page {alias pages/one/;}# 测试4location ^~ /test4/page/ {alias pages/one/;}location = /favicon.ico {log_not_found  off;access_log off;}location / {index  index.html index.htm;}
}

测试1:root pages/one/;

请求地址host:8688/test1/one.htmllocation uri/test1/请求结果404

error.log

日志输出

*920 open() "/usr/local/nginx/pages/one/test1/one.html" failed (2: No such file or directory)文件路径

host:8688/usr/local/nginx/pages/one/test1/one.html

测试2:alias pages/one/;

请求地址host:8688/test2/one.htmllocation uri/test2/请求结果200

error.log

日志输出

文件路径host:8688/usr/local/nginx/pages/one/one.html

测试3:alias pages/one/;

请求地址host:8688/test3/page/two/one.htmllocation uri/test3/page请求结果404

error.log

日志输出

*924 open() "/usr/local/nginx/pages/one//two/one.html" failed (2: No such file or directory)文件路径host:8688/usr/local/nginx/pages/one//two/one.html

测试4:alias pages/one/;

请求地址host:8688/test4/page/two/one.htmllocation uri/test4/page/请求结果404

error.log

日志输出

*932 open() "/usr/local/nginx/pages/one/two/one.html" failed (2: No such file or directory)文件路径host:8688/usr/local/nginx/pages/one/two/one.html

结论

        对比测试1和测试2,可以得出的结论:

                ①使用root指令时,请求URI部分不会改变,最终文件访问路径是 root path + 完整请求URI

                ②使用alias指令时,请求URI部分内容会被alias path替换掉。

        通过测试2、3、4,可以得出的结论:

                使用alias指令时,请求URI与location uri匹配的部分会被alias path替换掉

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Nginx(七) root和alias的区别及详细测试":http://eshow365.cn/6-39578-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!