Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override global auth_basic in nginx

I have a situation wherein we have set auth_basic in global level for all non live sites. Is it possible in Nginx to override this auth_basic in site specific file and apply auth_basic for site-specific path with new authentication password.

auth_basic in global level is set on /etc/nginx/conf.d/auth.conf

site-specific configuration lies on /etc/nginx/sites-enabled/sitename.conf

in nginx.conf I have:

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
like image 521
shrish Avatar asked Oct 18 '22 15:10

shrish


1 Answers

Yes you can do it.

Quoting 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 multiple levels (contexts), i.e. if you have it at global level you can disable it at server or even 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

So you can have auth_basic/auth_basic_user_file defined at a global level and have it overridden per individual 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>;
like image 82
ffeast Avatar answered Oct 21 '22 04:10

ffeast