Assume we have Nginx installed on Ubuntu 12.04 server. Now I will show you how to configure HTTP basic authentication in Nginx.
step1:
Open conf/nginx.conf file, and add some directives into locatiion / section:
location / {
auth_basic "Restricted";
auth_basic_user_file pwd;
...
}
Restricted
word will appear in the popup dialog when you try to access the Nginx site the first time.
pwd is a file located at conf folder. Note if you type conf/pwd here, the pwd file must be located at /conf/conf folder.
step2: Create a pwd file. Add your user name and password(plaintext). The password should be replaced by the step3.
chenshu:770328
step3: Install Apache2 tools.
apt-get install apache2-utils
step4: Update password using apache2 tools.
htpasswd /usr/nginx/conf/pwd chenshu
You will be asked to input password twice. Now the pwd file's content changed: chenshu:$apr1$I2FIVtPG$I51oSU4eatH.tJdnmxG6K0
step5:Restart Nginx service.
service nginx restart
step6: Log in your site. Very easy!