nginx location 取反

默北 Nginx226,4661字数 794阅读2分38秒阅读模式

有这么个需求,只允许html、shtml、js、css访问,其它任何文件都不允许访问。

nginx location配置取反,如下:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-location-negated/

location ~* .*\.(?![html|shtml|js|css]$)
{ 
         return 503;
}

nginx的正则使用PCRE(Perl Compatible Regular Expressions)。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-location-negated/

Extension Description
(?# TEXT) This extension lets you add comments to your regular expression. The TEXT value is ignored.
(?:...) This extension lets you add parentheses to your regular expression without causing a pattern memory position to be used.
(?=...) This extension lets you match values without including them in the $& variable.
(?!...) This extension lets you specify what should not follow your pattern. For instance, /blue(?!bird)/ means that "bluebox" and "bluesy" will be matched but not "bluebird".
(?sxi) This extension lets you specify an embedded option in the pattern rather than adding it after the last delimiter. This is useful if you are storing patterns in variables and using variable interpolation to do the matching.
文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-location-negated/文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-location-negated/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 15/05/2015 01:00:10
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-location-negated/
评论  2  访客  2
    • 青年创业网
      青年创业网 1

      博主加油

      • Aceslup
        Aceslup 9

        依旧是强大的正则表达式。
        求博主脚本编写思路。

      评论已关闭!