Nginx中的用户认证配置及阻止用户使用代理访问的方法
linux  /  管理员 发布于 8年前   202
nginx用户认证配置( Basic HTTP authentication)
ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。
默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。
nginx basic auth指令
语法: auth_basic string | off;
默认值: auth_basic off;
配置段: http, server, location, limit_except
默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。
语法: auth_basic_user_file file;
默认值: ―
配置段: http, server, location, limit_except
用户密码文件,文件内容类似如下:
ttlsauser1:password1
ttlsauser2:password2:comment
nginx认证配置实例
server{ server_name .net.cn; index index.html index.php; root /data/site/; location / { auth_basic "nginx basic http test for .net.cn"; auth_basic_user_file conf/htpasswd; autoindex on; }}备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。
生成密码
可以使用htpasswd,或者使用openssl
# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd# cat conf/htpasswd ttlsa:xyJkVhXGAZ8tM
账号:ttlsa
密码:123456
reload nginx
# /usr/local/nginx-1.5.2/sbin/nginx -s reload
效果如下:
完成~
Nginx阻止用户代理
些时候,需要阻止某些用户代理访问网站,比如ab,wget,curl等等,这就需要使用到$http_user_agent变量。
修改nginx.conf
if ($http_user_agent ~* (Wget|ab) ) { return 403;} if ($http_user_agent ~* LWP::Simple|BBBike|wget) { return 403;}重启nginx
# /usr/local/nginx-1.7.0/sbin/nginx -s reload
test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
