Nginx HTTP basic auth – allowing IP or password-based access

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

nginx uses similar syntax to that of Apache to allow users to access a directory, depending if their IP or username/pass matches (i.e. you want all users to present a valid username/pass match, but want to let some automated checks coming from specified IPs through, i.e. from ldirectord or nagios). Here is an example to use both HTTP basic auth and IP based auth.

location / {
index index.php;
satisfy any;
allow 10.0.0.0/8;
auth_basic "example.com intra";
auth_basic_user_file /etc/nginx/htpasswd/example.com.htpasswd;
}


You can create/modify the auth_basic_user_file with htpasswd program (typically found in apache2-utils or apache-base package, depending on the distribution):

htpasswd /etc/nginx/htpasswd/example.com.htpasswd someusername