需求:对默认的根目录访问,默认是index.html,不需要用户密码权限控制。
index.html页面上有跳转连接,该连接访问的是jns下资源,需要用户密码控制。
如何配置?
对网上各种配置,真是看花眼。
想到简单方案,亲测可以:
server {
listen 80;
server_name www.xxx.xxx;
location = / {
root html;
index index.html index.htm;
}
location /jns/ {
auth_basic "auth_name";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
上面根目录是精确匹配,根目录下访问的是index.html
当需要跳转到jns目录下时,就要求验证用户名和密码。
关于htpasswd如何生成,请参考本人的文章 https://www.jianshu.com/p/7a394b52f8e3