Skip to content

Instantly share code, notes, and snippets.

@yorzi
Created May 16, 2013 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorzi/5590462 to your computer and use it in GitHub Desktop.
Save yorzi/5590462 to your computer and use it in GitHub Desktop.
Nginx with basic authentication

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment