Three methods for setting directory password protection for Nginx in linux

Source: Internet
Author: User

When some website paths are not allowed for public access, such as the website background password, we encrypt and authenticate the paths on the Web server. Only the correct user name and password can be entered for access. Apache has such a function, so can Nginx. The following describes how to encrypt certain paths in Nginx.

Generate password

Method 1: Use Apache's htpasswd tool to create a user password file

To use htpasswd, you must have installed apache.


# Htpasswd-B-c filename username passwd
Adding password for user ******

Filename is the created File. Place the file in a directory that cannot be accessed through the WEB path and provide the absolute path address of the file in the Nginx configuration file. Username is the user name and passwd is the password.

Method 2: use perl to create a password

Considering that users who have installed Nginx may not be able to install Apache (httpd), if they only want to generate a password to install it, it will not be easy to use. The following describes how to use a perl program to generate a password.

Create a program:


# Vim genpwd. pl

Write the following content:


#! /Usr/bin/perl
Use strict;
My $ genpwd = $ ARGV [0];
Print crypt ($ genpwd, $ genpwd). "\ n ";

Execute:


# Chmod u + x genpwd. pl

Generate password:


#./Genpwd. pl password
PapAq5PwY/QQM

Write the output password to a password file in the following format:


Username: the encrypted password.
One account per row.
Method 3: online generation
It is convenient to use online tools provided by the open-source Chinese community. Click here to jump

 
Set Nginx

If the multi-domain virtual host configuration is used to encrypt a directory, modify the configuration file:
 
Location /{
Root/data/www/domain.com;
Index. php index.html index.htm;
Try_files $ uri // index. php? Q = $ uri & $ args;
 
Location ~ ^/Demo/book /.*{
Autoindex on;
Autoindex_exact_size off;
Autoindex_localtime on;
Charset UTF-8, gbk, big5;
Auth_basic "Password Needed ";
Auth_basic_user_file/usr/local/nginx/vhosts/auth/authuser;
       }      
     }
"Location ~ ^/Demo/book /.*{...}" In this way, you cannot directly access the files in this directory without a password to achieve the desired effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.