Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match all locations in nginx, for auth?

Tags:

nginx

I need an expression to match all requests, no matter what.

Is this good enough?

location ~ ^/

I'm worried about other locations taking precedence, bypassing my auth.

like image 267
ChocoDeveloper Avatar asked Aug 24 '12 02:08

ChocoDeveloper


1 Answers

You can put ngx_http_auth_basic_module settings into any of the following contexts:

http, server, location, limit_except

Your version

location ~ ^/

Would work only if you don't have another declared locations in your server section
example:

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
    }
}

Just put your http_auth settings into server section and all locations described for this server would inherit this settings.
example:

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. 
    }
}
like image 61
CyberDem0n Avatar answered Oct 11 '22 12:10

CyberDem0n