Nginx-----通过Nginx实现web页面的用户认证

本文详细介绍如何在Nginx环境中配置用户认证,包括环境搭建、配置文件修改、密码文件生成及配置重载等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.模拟环境

访问Web页面需要进行用户认证,用户名为:tom,密码为:123456

2.步骤

步骤一:Nginx环境配置及修改Nginx配置文件
[root@server0 nginx-1.12.2]# useradd -s /sbin/nologin nginx

[root@server0 lnmp_soft]# cd nginx-1.12.2/
[root@server0 nginx-1.12.2]# yum -y install gcc openssl-devel pcre-devel
[root@server0 nginx-1.12.2]# ./configure \

–user=nginx
–group=nginx
–with-http_ssl_module

make && make install
1).[root@server0 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
auth_basic “Input Password:”; //认证提示符信息
auth_basic_user_file “/usr/local/nginx/pass”; //认证的密码文件
location / {
root html;
index index.html index.htm;
}
}

2).生成密码文件,创建用户及密码 # 使用htpasswd命令创建账户文件,需要确保系统中已经安装加了httpd-tools.

[root@server0 nginx-1.12.2]# yum -y install httpd-tools
[root@server0 nginx-1.12.2]# htpasswd -c /usr/local/nginx/pass tom //创建密码文件
New password:
Re-type new password:
Adding password for user tom
[root@server0 nginx-1.12.2]# htpasswd /usr/local/nginx/pass jerry //追加用户,不使用-c选项
New password:
Re-type new password:
Adding password for user jerry
[root@server0 nginx-1.12.2]# cat /usr/local/nginx/pass

3.重新加载配置
[root@server0 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s reload //重新加载配置文件
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值