search

Home  >  Q&A  >  body text

Puzzled nginx rewrite rules

The picture above is a simple example that I configured according to the nginx longest match principle. The purpose is to deny all access to /, but to be able to jump normally when accessing /hello. However, for cashback, access to /hello is still 403, but The rewrite log records that nginx did match my rules, why did it still return 403 to me? Please help me understand, thank you very much!

習慣沉默習慣沉默2771 days ago898

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-05-27 17:47:03

    location / can match all requests

    You can modify it as follows

     location = / {
        deny all;
     }

    Grammar rules: location [=|~|~*|^~] /uri/ { … }
    = The beginning means an exact match
    ^~ The beginning means the uri starts with a regular string, which can be understood as matching the url path. nginx does not encode the URL, so the request is /static/20%/aa, which can be matched by the rule ^~ /static/ /aa (note the space). The beginning of
    ~ indicates case-sensitive regular matching
    ~* The beginning indicates case-insensitive regular matching
    !~ and !~* are case-sensitive non-matching and case-insensitive non-matching regular matching respectively
    / universal matching, Any request will be matched

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-27 17:47:03

    Because /test00 after rewrite matches location /

    You should add a location ~ ^/test00 and enable access

    reply
    0
  • Cancelreply