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


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

            Nginx配置如下:

    1. server {
    2. listen 8688 default_server;
    3. server_name www.read******.cn;
    4. access_log logs/access.log format2;
    5. root pages;
    6. set $sn 1;
    7. # 测试1
    8. location ^~ /test1/ {
    9. root pages/one/;
    10. }
    11. # 测试2
    12. location ^~ /test2/ {
    13. alias pages/one/;
    14. }
    15. # 测试3
    16. location ^~ /test3/page {
    17. alias pages/one/;
    18. }
    19. # 测试4
    20. location ^~ /test4/page/ {
    21. alias pages/one/;
    22. }
    23. location = /favicon.ico {
    24. log_not_found off;
    25. access_log off;
    26. }
    27. location / {
    28. index index.html index.htm;
    29. }
    30. }

    测试1:root pages/one/;

    请求地址host:8688/test1/one.html
    location 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.html
    location uri/test2/
    请求结果200

    error.log

    日志输出

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

    测试3:alias pages/one/;

    请求地址host:8688/test3/page/two/one.html
    location 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.html
    location 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替换掉

  • 相关阅读:
    软文为什么成为企业降本增效的营销利器?
    docker学习(一)
    【字符串处理函数】sprintf与snprintf
    ABAP(ALV部分)
    第22章_瑞萨MCU零基础入门系列教程之DMA控制器
    BigDecimal不会丢失精度的浮点数
    javaweb实现的在线鲜花商城源码(电商购物系统)
    鸿蒙项目实战-月木学途:1.编写首页,包括搜索栏、轮播图、宫格
    前端基础:协商缓存
    HAL库笔记(重要库函数)
  • 原文地址:https://blog.csdn.net/ShenDaiSun/article/details/134492193