PHP不在location指令内工作
我已经设置了一个测试Virtualbox/Debian Wheezy7.1机器,其中有一个裸网安装机器+nginx+php fpm 我有SSL、php、basic_auth和allow/deny在服务器级别上工作 但是,如果我希望auth内容仅用于一个路径,auth可以工作,而PHP内容不能(index.PHP在web浏览器中下载) 我知道这与nginx如何匹配位置指令有关,但我不确定它是什么 这是我的配置文件:PHP不在location指令内工作,php,nginx,Php,Nginx,我已经设置了一个测试Virtualbox/Debian Wheezy7.1机器,其中有一个裸网安装机器+nginx+php fpm 我有SSL、php、basic_auth和allow/deny在服务器级别上工作 但是,如果我希望auth内容仅用于一个路径,auth可以工作,而PHP内容不能(index.PHP在web浏览器中下载) 我知道这与nginx如何匹配位置指令有关,但我不确定它是什么 这是我的配置文件: server { listen 80;
server {
listen 80;
server_name www.test.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
# HTTPS server
server
{
listen 443;
server_name www.test.com;
root /srv/vhosts/www.test.com/html;
index index.php ;
ssl on;
ssl_certificate /etc/nginx/certs/STAR.test.com.crt;
ssl_certificate_key /etc/nginx/certs/STAR.test.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location ^~ /testdir/ {
auth_basic "gib login";
auth_basic_user_file /etc/nginx/htpasswd/www.test.com.htpasswd;
allow 192.168.1.3; # my workstation ip
deny all;
}
}
编辑:请看第一条评论,谢谢 ,位置指令的顺序很重要
要确定哪个location指令与特定查询匹配,请
首先检查文本字符串。文字字符串匹配开头
查询部分-将使用最具体的匹配项。
然后,按照中定义的顺序检查正则表达式
配置文件。匹配的第一个正则表达式
查询将停止搜索。如果未找到正则表达式匹配项
找到时,将使用文本字符串搜索的结果
尝试组织您的位置指令,以便您要保护的目录首先出现,然后是PHP,然后是Try\u文件
指令。我在我的系统上做了一个快速测试,按照下面的顺序构造位置块,这样就可以保护testdir
,并且仍然可以执行index.php
location ^~ /testdir/ {
auth_basic "gib login";
auth_basic_user_file /etc/nginx/htpasswd/www.test.com.htpasswd;
allow 192.168.1.3; # my workstation ip
deny all;
}
location ~ \.php$ {
}
location ~ / {
try_files $uri $uri/ 404;
}
我已设法解决了我的问题,将此:“location^~/testdir/”更改为“location/testdir/”。我仍然不知道为什么会这样,如果有人能详细说明,我将不胜感激。谢谢谢谢你的帮助!你能看一下问题评论部分的评论吗?谢谢看看我提供的文档链接。Nginx按特定顺序处理位置块。提供了更多关于这方面的见解。