6

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/*; 

1 Answer 1

3
+25

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>; 
Sign up to request clarification or add additional context in comments.

3 Comments

doesn't work as expected. the global user credentials always overrides the later ones.
could you share your config files reproducing it? + what's your nginx version?
Here's an example working for me with sample config files and detailed output, it would be nice if you could share yours: gist.github.com/ffeast/d21bb4751cc994f3ba48300458496f9f

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.