如何在 nginx 中覆盖全局 auth_basic

shr*_*ish 5 nginx basic-authentication

我有一种情况,其中我们auth_basic为所有非实时站点设置了全局级别。在 Nginx 中是否可以在auth_basic站点特定文件中覆盖它并auth_basic使用新的身份验证密码申请站点特定路径。

auth_basic 在全球范围内设置为 /etc/nginx/conf.d/auth.conf

特定于站点的配置取决于 /etc/nginx/sites-enabled/sitename.conf

在 nginx.conf 我有:

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
Run Code Online (Sandbox Code Playgroud)

ffe*_*ast 3

是的,你可以做到。

引用文档:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic

语法: auth_basic 字符串 | 离开;

默认值:auth_basic 关闭;

上下文:http、服务器、位置、limit_except

正如您所看到的,指令本身可以在多个级别(上下文)定义,即,如果您在全局级别拥有它,则可以在服务器甚至位置级别禁用它。

auth_basic_user_file指令也是如此http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic_user_file

因此,您可以在全局级别定义auth_basic / auth_basic_user_file并在每个单独的站点上覆盖它

auth.conf:

auth_basic global;                                                          
auth_basic_user_file <path_to_your_auth_file>;                  
Run Code Online (Sandbox Code Playgroud)

站点名称.conf:

auth_basic sitenamereal; # any name, actually you can omit it
auth_basic_user_file <path_to_your_site_specific_file>;
Run Code Online (Sandbox Code Playgroud)