如何在Nginx中密码保护单个ph php脚本。我正在使用nginx作为网络服务器并代理到php-fastcgi。我无法让位置块按预期运行。
这是我正在尝试的片段。
location /admin\.php$ {
auth_basic "Valid User Required";
auth_basic_user_file /etc/nginx/http-auth;
}
location ~\.php$ {
root /var/www/nginx/vhosts/site;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass phpfcgi;
}
答案 0 :(得分:0)
location /admin\.php$ { auth_basic "Valid User Required"; auth_basic_user_file /etc/nginx/http-auth; + root /var/www/nginx/vhosts/site; + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_pass phpfcgi; }
这可能对你有帮助。
答案 1 :(得分:0)
第一期:
您要匹配前缀字符串而不是正则表达式:
~\.php$
)进行搜索。如果匹配,则前缀字符串匹配将被忽略。发布问题1的解决方案:
添加~
来执行正则表达式匹配:~/admin\.php$
。
第二个问题:
现在,您的块匹配了,您想将其中的php脚本传递给fastcgi,否则它们将被用作文本文件,而不会被解析。
问题2的解决方案:
在~\.php$
位置块内嵌套一个~/admin\.php$
位置块,得到如下所示的最终结果:
location ~/admin\.php$ {
auth_basic "Valid User Required";
auth_basic_user_file /etc/nginx/http-auth;
location ~\.php$ {
root /var/www/nginx/vhosts/site;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass phpfcgi;
}
}
location ~\.php$ {
root /var/www/nginx/vhosts/site;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass phpfcgi;
}
参考: