如何匹配nginx中的所有位置,以进行身份​​validation?

无论如何,我需要一个expression式来匹配所有请求。

这足够好吗?

location ~ ^/ 

我担心其他位置优先,绕过我的身份validation。

您可以将ngx_http_auth_basic_module设置放入以下任何环境中:

 http, server, location, limit_except 

你的版本

 location ~ ^/ 

只有在server部分没有另外声明的位置时才能使用
例:

 server { ... #some server settings location / { # full equivalent for "~ ^/" auth_basic on; auth_basic_user_file /path/to/some/file; } location /other_location { # here http_auth not inherited } } 

只要把你的http_auth设置放到server部分,所有为这个server描述的位置就会继承这个设置。
例:

 server { ... # some server settings auth_basic on; auth_basic_user_file /path/to/some/file; location / { # HERE http_auth settings would be # inherited from previous configuration level. } }