NGINX:如何从URI中提取一些值并在auth_basic_user_file中使用它?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了NGINX:如何从URI中提取一些值并在auth_basic_user_file中使用它?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1592字,纯文字阅读大概需要3分钟。
内容图文
![NGINX:如何从URI中提取一些值并在auth_basic_user_file中使用它?](/upload/InfoBanner/zyjiaocheng/965/3c5817c0c9fe4b47abfd41c244352877.jpg)
我为不同的客户提供了几个下载文件夹.每个文件夹必须使用自己的.htaccess文件进行密码保护.
为了简化我的nginx配置,我有以下问题:
如何从URI中提取客户名称(“customerA”)并在auth_basic_user_file中使用它?
这是我现在使用的配置:
server {
root /usr/share/nginx/www;
server_name localhost;
autoindex on;
location /build/customerA {
index build.json;
auth_basic "CustomerA Access Only!";
auth_basic_user_file /usr/share/nginx/www/build/customerA/.htpasswd;
}
location /build/customerB {
index build.json;
auth_basic "CustomerB Access Only!";
auth_basic_user_file /usr/share/nginx/www/build/customerB/.htpasswd;
}
}
谢谢,
塞巴斯蒂安
解决方法:
如果未找到auth_basic_user_file,则nginx将故障转移到403(在1.7.5版中测试).所以你可以试试下面的正则表达式位置.缺点是你需要关注它在配置中相对于其他正则表达位置的位置,因为只匹配第一个:
location ~ ^/build/(?<customer>[^/]+) {
index build.json;
auth_basic "$customer Access Only!";
auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
}
要避免这种情况,请考虑使用前缀位置包装正则表达式位置.然后它可以出现在您的配置中的任何位置,它将更加优雅地扩展:
location /build/ {
location ~ ^/build/(?<customer>[^/]+) {
index build.json;
auth_basic "$customer Access Only!";
auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
}
}
或者将正则表达式放在服务器节之外的map指令中:
map $uri $no_customer {
default 1;
~^/build/(?<customer>[^/]+) 0;
}
然后使用以下前缀位置:
location /build/ {
if ($no_customer) { # optionally forbid if no customer specified
return 403;
}
index build.json;
auth_basic "$customer Access Only!";
auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
}
内容总结
以上是互联网集市为您收集整理的NGINX:如何从URI中提取一些值并在auth_basic_user_file中使用它?全部内容,希望文章能够帮你解决NGINX:如何从URI中提取一些值并在auth_basic_user_file中使用它?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。