nginx 某个目录访问设置账户密码访问

技术分享 · Fecmall · 于 6年前 发布 · 3100 次阅读

有时候我们的文档,一般分为对内和对外开发两部分,但是文档内容是静态html,因此需要在nginx层做访问权限控制

譬如: /doc/site-description-guide 路径下的url都不让访问:

1.在nginx中添加配置

location /doc/site-description-guide
{
    auth_basic "Please enter Password";
    auth_basic_user_file nginx_htpasswd.pl;
}

2.我的nginx安装在/usr/local/nginx/,配置文件路径为:/usr/local/nginx/conf

cd /usr/local/nginx/conf
touch nginx_htpasswd_generate.pl
vim nginx_htpasswd_generate.pl

vim打开文件后,在文件里面填写如下内容:

#!/usr/bin/perl 
use strict; 
my $pw=$ARGV[0]; 
print crypt($pw,$pw)."\n"; 

:wq 保存退出后,执行chmod 755 nginx_htpasswd_generate.pl

然后执行:

./nginx_htpasswd_generate.pl fecshop
feIffq1p5TTrU   // 执行结果

然后创建文件 nginx_htpasswd.pl,写入账号密码

touch nginx_htpasswd.pl
chmod 755 nginx_htpasswd.pl
vim nginx_htpasswd.pl

打开文件后,写入内容

myfecaccount:feIffq1p5TTrU

:wq 保存退出,然后重启或者重新加载nginx配置

/etc/init.d/nginx reload

然后访问 /doc/site-description-guide 路径下的url ,就会出来一个账号密码确认,

填写上面设置的账号密码即可登录

账号:myfecaccount
密码:fecshop

然后就可以愉快的登录了

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics