Nginx Password protection example for a directory _nginx

Source: Internet
Author: User
Tags password protection

Sometimes do not want a directory to be accessed, so to add a password to protect, the previous use of Apache seems very simple to achieve directory encryption. I used the Nginx, used for so long Nginx really did not try to encrypt ...
Reference to some information, and then found that this can be: (Debian system, Configuration "# ..." means that there are other configurations)
1. Configure the Web site (if yousite.com is placed in/home/www, then encrypt the Ooxx directory)

Copy Code code as follows:

server {
Listen 80;
server_name yousite.com;
root/home/www;
Index index.php index.html;
Location ^~/ooxx/{
Auth_basic "Authorized users only";
AUTH_BASIC_USER_FILE/HOME/.HTPASSWD;
}

#......

}

2. Install Apache 2 tool (requires HTPASSWD command)

Copy Code code as follows:

Apt-get Install Apache2-utils

3. CD to/home directory (other directories are OK, but pay attention to auth_basic_user_file consistent with above), use HTPASSWD to generate user name and password
Copy Code code as follows:

HTPASSWD-BDC. htpasswd Username Password

(note: the generated. htpasswd file may need to be set to run by the Nginx user)

In this way, you direct access to Http://yousite.com/ooxx will prompt you to enter the user and password, input just use htpasswd set the user and password can go in!

But then you will find that direct access to the PHP file will become a download rather than a run-------

Checked the Nginx to the PHP configuration:

Copy Code code as follows:

Location ~ \.php$ {
Include/etc/nginx/fastcgi_params;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
}

That's right.

This is how I solved it: in the location ^~/ooxx/{...}, add a section of PHP configuration, become

Copy Code code as follows:

server {
Listen 80;
server_name yousite.com;
root/home/www;
Index index.php index.html;
Location ^~/ooxx/{
Location ~ \.php$ {
Include/etc/nginx/fastcgi_params;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
}
Auth_basic "Authorized users only";
AUTH_BASIC_USER_FILE/HOME/.HTPASSWD;
}

#......

}


Finish the work, please refer to Nginx Official document, I casually play (anyway is successful).

Side Note: If you remove the ^~ of the location ^~/ooxx/, you will have to enter a password to access the directory, but you can access the file directly, note.

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.