Johan
Johan

Reputation: 40500

Append auth_basic_user_file for specific location in Nginx

We have configured Nginx to use basic authentication for our entire site:

location / {
    # ... More stuff goes here ...

    # authentication
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
}

Now under a specific location, say /x, I'd like to allow all users defined in /etc/nginx/conf.d/.htpasswd as well as some additional users defined in another password file. I.e. something like this (which doesn't work):

location /x {  
    # authentication
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd, /etc/nginx/conf.d/.otherhtpasswd;
}

How can I achieve this with Nginx?

Upvotes: 4

Views: 1716

Answers (1)

Shawn C.
Shawn C.

Reputation: 6841

As nginx does not accept multiple to the auth_basic_user_file directive, I would create a simple script to concat the two files into a 3rd file. Then reference that file with the auth_basic_user_file directive. You can then put that in a cron job or just run it every time one of the files changes.

Upvotes: 3

Related Questions