azev
V2EX  ›  问与答

nginx 中 location 匹配问题

  •  
  •   azev · Jun 11, 2020 · 2002 views
    This topic created in 2164 days ago, the information mentioned may be changed or developed.

    匹配根目录下的 txt 文件
    location ~ /.*\.txt 是不是这样?

    9 replies    2020-06-11 17:03:15 +08:00
    azev
        2
    azev  
    OP
       Jun 11, 2020
    感觉 .* 好像会有问题 要不要换成 .*? 呢
    azev
        3
    azev  
    OP
       Jun 11, 2020
    @noqwerty 不用这么恶心
    google 看了十来篇 总是讲后缀或讲目录 不知是有意还是无意忽略这个情形
    julyclyde
        4
    julyclyde  
       Jun 11, 2020
    你这个其实是“斜杠开头 txt 结尾但中间不一定几层目录”的匹配规则吧?
    wuqingdzx
        5
    wuqingdzx  
       Jun 11, 2020
    你这个不仅会匹配根目录,也会匹配二级目录.

    按我理解这么写
    ```ini

    location ~ ^/[^/]*\.txt? {
    root /var/www/html/;
    }

    location ~ ^/.*\.txt? {
    deny all;
    }

    ```
    /a.txt /var/www/html/ 里匹配
    /a/a.txt 返回 403
    Vegetable
        6
    Vegetable  
       Jun 11, 2020
    aunox
        7
    aunox  
       Jun 11, 2020
    你这个应该是匹配所有的后缀.txt 的 URL
    gesse
        8
    gesse  
       Jun 11, 2020
    @noqwerty
    优秀! 哈哈哈··
    azev
        9
    azev  
    OP
       Jun 11, 2020
    @julyclyde 对 应该是有这个问题

    @Vegetable 工具不错 试了下这个当中的.还是能匹配到多级目录

    @wuqingdzx 这个是可以的 但最后的问号貌似没有必要?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   870 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 21:13 · PVG 05:13 · LAX 14:13 · JFK 17:13
    ♥ Do have faith in what you're doing.