nginx: auth_basic for everything except a specific location

后端 未结 4 1859
再見小時候
再見小時候 2021-02-05 04:03

How can I enable HTTP Basic Auth for everything except for a certain file?

Here is my current server block configuration for the location:

locat         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 04:58

    I'm doing the following:

    location = /hc.php {
      auth_basic "off";
    }
    
    location / {
      try_files $uri $uri/ =404;
    }
    
    • The narrow match:location = /somefile.txt {} comes first, so location / {} can capture the remaining requests
    • auth_basic "off" requires the quotes around it as far as I know
    • I also use the exact (full, if you like) match, in order to stop iteration over the other locations defined in the config (read below quote for more info on what it does)

    Probably this would work in different orders, and/or without the double quotes also, but why not try to do things as correct and complete as possible, if possible.

    The most important modifiers are:

    (none) No modifier at all means that the location is interpreted as a prefix. To determine a match, the location will now be matched against the beginning of the URI.

    =: The equal sign can be used if the location needs to match the exact request URI. When this modifier is matched, the search stops right here.

    ~: Tilde means that this location will be interpreted as a case-sensitive RE match.

    ~*: Tilde followed by an asterisk modifier means that the location will be processed as a case-insensitive RE match.

    ^~: Assuming this block is the best non-RE match, a carat followed by a tilde modifier means that RE matching will not take place.

    quoted from here: https://www.keycdn.com/support/nginx-location-directive

提交回复
热议问题