Apache and Nginx Login Authentication configuration detailed (can be certified directory)

Source: Internet
Author: User
Tags auth mrtg

First, Apache Certification

Type of authentication: Basic
Digest Summary
Authentication method: A, container attestation:<directory> ......</directory>
B, hidden file authentication create. htaccess file

Method one, container authentication

A, enter the configuration file vi/etc/httpd/conf/httpd.conf
B, configuration: Approximately 531 lines near the following configuration:

The code is as follows Copy Code
<directory "/VAR/WWW/HTML/MRTG" >
AllowOverride none# #不允许通过隐藏认证, that is, through the container certification
AuthType basic## authentication type is Basic
AuthName "Ajian" # # Certification name is Ajian
Authuserfile/var/www/passwd/pass##pass for the authentication password file, specify the location where the password file is stored.
requirevalid-user## Valid Users (note case, because Word causes some changes in capitalization)
</Directory>

C, create a directory mkdir-p/var/www/passwd
Enter Directory CD/VAR/WWW/PASSWD
D, create Apache user htpasswd-cpassajian# #pass为密码文件Ajian为用户
Change the use of the pass file to Apache:chown Apache.apache Pass
Attach: Then add a user to the pass file: htpasswdpasstt# #添加一个TT的用户到Pass文件中
E, restart the service and test

Methods two, through the hidden authentication

Similar to the top, but not the same.

The code is as follows Copy Code
HTTPD Master configuration file
<directory "/VAR/WWW/HTML/MRTG" >
Allowoverrideauthconfig
</Directory>
Create hidden files and place them in a directory to be authenticated
Eg:vi/var/www/html/mrtg
Authtypebasic
AuthName "Ajian"
Authuserfile/var/www/passwd/pass
Require Valid-user

Second, Nginx login certification

For login authentication access, the virtual host configuration in nginx.conf:

The code is as follows Copy Code

Auth_basic "Web Test";
auth_basic_user_file/www/htpasswd.conf;

Note: 1, auth_basic_user_file best Use absolute path
Then edit the htpasswd.conf file,

Needs to be generated with the Apache HTPASSWD command:
#/USR/LOCAL/APACHE/BIN/HTPASSWD-NB User 123456
User:0mibyk5hqbep

Import output into htppasswd.conf:
User:0mibyk5hqbepy

Note: 1, this file and Nginx users to the same permissions

Add: Password Authentication for directory

For example: based on the entire site certification, Auth_basic in PHP before explaining.

The code is as follows Copy Code
Server
{
Listen 80;
server_name www.111cn.net 111cn.net;
Root/www/111cn.net;
Index index.html index.htm index.php;
Auth_basic "Input you user name and password";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/VHOST/NGINX_PASSWD;
Location ~. php$
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include Fastcgi_params;
}
Location ~/.ht
{
Deny all;
}
Access_log/logs/111cn.net_access.log main;
}


For directory authentication, the PHP file is not executed and will be downloaded in a separate location and nested in the location to explain PHP location. Auth_basic after the nested location.

The code is as follows Copy Code
Server
{
Listen 80;
server_name www.111cn.net 111cn.net;
Root/www/111cn.net;
Index index.html index.htm index.php;
Location ~ ^/admin/.*
{
Location ~. php$
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include Fastcgi_params;
}
Auth_basic "Auth";
Auth_basic_user_file/usr/local/nginx/conf/vhost/auth/admin.pass;
}
Location ~. php$
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include Fastcgi_params;
}
Location ~/.ht
{
Deny all;
}
Access_log/logs/111cn.net_access.log main;
}

Here is a detail that location ~ ^/admin/.* {...} protects all files in the admin directory. If you only set the/admin/then direct input/admin/index.php can still be accessed and run. ^/admin/.* means to protect all files in this directory. Of course, only one certification is required. It does not need to be authenticated every time a request or a file is requested.

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.