Nginx – Setup HTTP Authentication

HTTP Authentication is the easiest way to prevent anonymous user access to your website. If you are on Apache, you can refer to the following post.
.htaccess – Setting Password For Your Web Folder

After you have created the password file, you could add the following line in the .htaccess or in the Apache VirtualHost.

AuthName "Password Required"
AuthType Basic
AuthUserFile /home/ykyuen/public_html/.htpasswd
require valid-user

 

But if you are working with Nginx, you should add the following lines on the VirtualHost instead.

server {
  listen 80;
  server_name www.ykyuen.info ykyuen.info;
  root /var/www/webroot;

  location / {
    # HTTP Authentication
    auth_basic "Restricted";
    auth_basic_user_file /home/ykyuen/public_html/.htpasswd;
  }
}

 

Restart the Nginx service and your website is now under protection.

Done =)

Reference: Basic HTTP Authentication With Nginx

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.