我试图让我的管理员控制器在auth_basic身份验证下。这就是我的nginx配置的样子:
location /index.php {
if ($arg_r ~ admin) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/mywebsite/.htpasswd;
}
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass php;
}
当请求在arg r
中包含'admin'时,我需要提示身份验证,例如:
但是没有工作,我做错了什么?
答案 0 :(得分:0)
根据文档,“if”s中不允许使用auth_ *。也许这可以解决问题:
location = /index.php {
if ($arg_r ~ admin) {
rewrite ^ /_admin/index.php$is_args$args last;
}
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass php;
}
location /_admin/index.php {
internal;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/mywebsite/.htpasswd;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass php;
}
启用了身份验证的重复位置。 “internal”指令限制对此位置的直接请求。 请注意我在第一个位置添加了“=”,它允许避免将/index.phpadasdasdsdfadsf重写为/_auth/index.php