Authentication Nginx-密码保护PHP脚本
如何在Nginx中对单个phphp脚本进行密码保护。我使用nginx作为web服务器,并代理到php fastcgi。我无法使位置块按预期运行 这是我正在尝试的一个片段Authentication Nginx-密码保护PHP脚本,authentication,passwords,nginx,Authentication,Passwords,Nginx,如何在Nginx中对单个phphp脚本进行密码保护。我使用nginx作为web服务器,并代理到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
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;
}
位置/管理\.php${
auth_basic“需要有效用户”;
auth_basic_user_file/etc/nginx/http auth;
+root/var/www/nginx/vhosts/site;
+包括/etc/nginx/fastcgi_参数;
+fastcgi\参数脚本\文件名$document\根$fastcgi\脚本\名称;
+fastcgi_pass phpfcgi;
}
这可能对您有所帮助。第一期: 您正在匹配前缀字符串而不是正则表达式:
~\.php$
)。如果匹配,则忽略前缀字符串匹配~
以执行正则表达式匹配:~/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;
}
参考资料: 请参阅本主题中的有关位置块优先级的说明和Nginx
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;
}