How to override global auth_basic in nginx

I have a situation where we installed auth_basic on a global level for all inactive sites. Is it possible in Nginx to override this auth_basic in the site file and apply auth_basic to the site path with a new authentication password.

auth_basic is installed globally at /etc/nginx/conf.d/auth.conf

Site configuration depends on /etc/nginx/sites-enabled/sitename.conf

in nginx.conf I have:

  include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; 
+5
source share
1 answer

Yes you can do it.

Quote from the docs: http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic

Syntax: auth_basic string | off

Default: auth_basic off;

Context: http, server, location, limit_except

As you can see, the directive itself can be defined at several levels (contexts), i.e. if you have it at the global level, you can disable it at the server level or even at the location level.

The same is true for the auth_basic_user_file directive http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic_user_file

That way you can have auth_basic / auth_basic_user_file defined globally and override it on a separate site

auth.conf:

 auth_basic global; auth_basic_user_file <path_to_your_auth_file>; 

sitename.conf:

 auth_basic sitenamereal; # any name, actually you can omit it auth_basic_user_file <path_to_your_site_specific_file>; 
+2
source

Source: https://habr.com/ru/post/1265502/


All Articles