Experiment one, how to achieve authentication of user access with nginx
I. Objectives
通过调整Nginx服务端配置,实现以下目标:
Access the Web page requires user authentication
User name: tom, password is: 123456
Second, the program
通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。
Finally, create a user and password, the server using the htpasswd command: 192.168.4.102, client: 192.168.4.101
Third, the implementation steps (see my install nginx service "to build nginx service")
1、修改Nginx配置文件:vim /usr/local/nginx/conf/nginx.conf
添加两行,红颜色标记
server {
listen 80;
server_name localhost;
auth_basic "Input Password:"; // prompt certification
auth_basic_user_file "/ usr / local / nginx / pass"; // authentication password file
location / {
root html;
index index.html index.htm;
}
}
2, generate a password file, create a user ID and password: create an account file using htpasswd command, you need to ensure that the system has been installed httpd-tools.
yum -y install httpd-tools.x86_64 //安装工具
htpasswd -c /usr/local/nginx/pass tom //创建用户名和密码,后期如果增加新的用户,不需要加-c的参数
3, the test
重启nginx服务,在客户端上192.168.4.101去访问服务器
As out of the box, enter a user name and password to verify whether the landing into
The second experiment, nginx name-based virtual hosting
First, the purpose
沿用上面的实验1
Configure name-based virtual hosting, to achieve the following objectives:
Achieve two name-based virtual hosting, domain names were www.aa.com and www.bb.com
The domain name for user authentication, the user name www.aa.com site for tom, the password is 123456
Second, the program
修改Nginx配置文件,添加server容器实现虚拟主机功能;对于需要进行用户认证的虚拟主机添加auth认证语句
Third, the implementation steps
1, edit the configuration file: vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.aa.com; //指定域名
auth_basic "Input Password:"; //安全的认证
auth_basic_user_file "/usr/local/nginx/pass"; //密码文件路径
location / {
root aa; //网站的根目录
index index.html index.htm;
}
server {
listen 80;
server_name www.bb.com; //指定域名
location / {
root bb; //网站的根目录
index index.html index.htm;
}
}
2. Create a site visit aa user name and password created above, and do not need to create
3. Create a root of the site and the corresponding page file
mkdir /usr/local/nginx/aa mkdir /usr/local/nginx/bb
首页文件自己自行创建
4, restart nginx service
5, the client test
由于没有做DNS解析,故在主机192.168.4.101的/etc/hosts文件,进行域名解析
Add the following line record
192.168.4.102 www.aa.com www.bb.com
Then enter two domain name in the browser to test, get a different page