分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

Nginx实现基于用户的访问控制(Ngx_http_auth_basic_module模块)

发布时间:2023-09-06 01:18责任编辑:顾先生关键词:http

Nginx基于用户的访问控制(Ngx_http_auth_basic_module)

官方文档:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

官方示例:The ngx_http_auth_basic_module module allows limiting access to resources by validating the user name and password using the “HTTP Basic Authentication” protocol.基于HTTP,适用basic机制进行用户认证;

Example Configurationlocation / {    auth_basic           "closed site";    auth_basic_user_file conf/htpasswd;}
Syntax:auth_basic string | off;
Default:
auth_basic off;
Context:httpserverlocationlimit_except
Syntax:auth_basic_user_file file;
Default:
Context:httpserverlocationlimit_except

Context:适用配置段

演示环境:

Server:192.168.47.140[root@GaoServer ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@GaoServer ~]# uname -r3.10.0-327.el7.x86_64[root@GaoServer ~]# uname -o[root@GaoServer ~]# nginx -Vnginx version: nginx/1.10.2......

相关配置:

#httpd-tools是Apache的工具包[root@GaoServer ~]# yum install httpd-tools -y#htpasswd命令是Apache的web服务器内置工具,用于创建和更新存储用户名密码的文本文,用于HTTP用户的basic认证[root@GaoServer ~]# htpasswd -hhtpasswd: illegal option -- h......#常用选项: -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.    #在命令行中使用口令,而不是提示口令;    -m  Force MD5 encryption of the password (default).    #MD5算法加密 -d  Force CRYPT encryption of the password (8 chars max, insecure).    #CRYPT算法加密 -s  Force SHA encryption of the password (insecure).    #SHA算法加密 -p  Do not encrypt the password (plaintext, insecure).    #使用明文密码 -D  Delete the specified user.    #删除指定用户 -v  Verify password for the specified user. ......  #创建一个加密文件MD5算法加密:(创建隐藏文件)[root@GaoServer ~]# htpasswd -c -m  /etc/nginx/.htpasswd gningNew password: Re-type new password: Adding password for user gning[root@GaoServer ~]# cat /etc/nginx/.htpasswd gning:$apr1$pTwVGCrf$BCWZFeQXXjS8Yb.JflpiL.#修改配置文件:[root@GaoServer ~]# vim /etc/nginx/conf.d/Vhost.confserver {        listen 80;        location /server/ {                root /data/html/;                auth_basic "User is";        #访问认证输入信息显示给用户;                auth_basic_user_file /etc/nginx/.htpasswd;    #定义使用什么账号文件;        }}[root@GaoServer ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@GaoServer ~]# nginx -s reload

访问测试:

浏览器访问IP:[Port]/server

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/A7/34/wKioL1njYXmAZRwUAAAnThS6G70678.png-wh_500x0-wm_3-wmp_4-s_3419484223.png" title="访问测试" alt="wKioL1njYXmAZRwUAAAnThS6G70678.png-wh_50" />

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/08/83/wKiom1njZFzji82RAAARBDGM6Oc940.png-wh_500x0-wm_3-wmp_4-s_1272194369.png" title="访问测试" alt="wKiom1njZFzji82RAAARBDGM6Oc940.png-wh_50" />

本文出自 “Gning丶” 博客,请务必保留此出处http://gning.blog.51cto.com/11847592/1972553

Nginx实现基于用户的访问控制(Ngx_http_auth_basic_module模块)

原文地址:http://gning.blog.51cto.com/11847592/1972553

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved