nginx配置用户名密码登陆

 

面试中问到这个问题,之前做过tomcat的登陆认证,nginx如下:

-- 使用模块:

  ngx_http_auth_basic_module :实现让用户只有输入正确的用户名密码才允许访问web内容

-- 安装httpd工具:

yum -y install httpd

-- 用命令htpasswd生成密码文件:-c指定密码文件,后面自定义登陆的用户名

htpasswd -c /etc/nginx/passwd nginxuser

 

--配置nginx: 

  配置文件server内新增加如下两行:

auth_basic "Please input password"; #这里是验证时的提示信息 
auth_basic_user_file /etc/nginx/passwd;

  nginx -t   #验证配置OK

  service nginx restart   #重启生效 

 

--附htpasswd命令:

Usage:
	htpasswd [-cimBdpsDv] [-C cost] passwordfile username
	htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password

	htpasswd -n[imBdps] [-C cost] username
	htpasswd -nb[mBdps] [-C cost] username password
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -b  Use the password from the command line rather than prompting for it.
 -i  Read password from stdin without verification (for script usage).
 -m  Force MD5 encryption of the password (default).
 -B  Force bcrypt encryption of the password (very secure).
 -C  Set the computing time used for the bcrypt algorithm
     (higher is more secure but slower, default: 5, valid: 4 to 31).
 -d  Force CRYPT encryption of the password (8 chars max, insecure).
 -s  Force SHA encryption of the password (insecure).
 -p  Do not encrypt the password (plaintext, insecure).
 -D  Delete the specified user.
 -v  Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.

 

转载于:https://www.cnblogs.com/pythonlee/p/9578616.html

要在公网上部署nginx rtmp docker实现用户名密码认证,可以按照以下步骤进行操作: 1. 首先,在服务器上安装Docker,确保可以正常使用Docker命令。 2. 下载nginx rtmp镜像,并在本地创建一个docker容器: `docker run -d -p 1935:1935 -p 80:80 --name nginx-rtmp tiangolo/nginx-rtmp` 这将创建一个名为nginx-rtmp的容器,并将宿主机的1935端口映射到容器的1935端口,80端口映射到容器的80端口。 3. 进入容器的shell环境: `docker exec -it nginx-rtmp /bin/bash` 4. 在容器中安装htpasswd工具,用于生成密码文件: `apt-get update && apt-get install apache2-utils` 5. 使用htpasswd生成密码文件,并设置用户名密码: `htpasswd -c /etc/nginx/.htpasswd username` 该命令将创建一个.htpasswd文件,并添加一个用户名为"username"的用户,此时会提示输入密码。 6. 编辑nginx配置文件,实现基本的用户名密码认证: `vi /etc/nginx/nginx.conf` 在`rtmp { ... }`或其他相关的块内添加以下配置: ``` application myapp { live on; hls on; hls_path /tmp/hls; deny play all; # 禁止未认证用户播放流 hls_fragment 1s; # 每个片段的时长 hls_playlist_length 3s; # M3U8播放列表文件的长度 auth_basic "Restricted"; # 认证提示信息 auth_basic_user_file /etc/nginx/.htpasswd; # 密码文件路径 } ``` 7. 保存配置文件并重启nginx服务: `service nginx restart` 现在,当访问rtmp流时,用户需要输入用户名密码才能进行验证,并可以播放和查看流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值