2013-08-29 21 views
0

我已经建立了一个测试VirtualBox的/ Debian的Wheezy7.1机裸净安装机+ nginx的+ PHP-FPMPHP不是一个位置指令

我有SSL,PHP,basic_auth内工作并允许/拒绝在服务器级别上工作。

但是,如果我想在auth东西是只有一条路径中,auth的作品,但PHP的东西不(在index.php获取网页浏览器下载)

我知道它有什么做nginx怎么位置指令比赛,但我不知道它是什么...

这里是我的配置文件:

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; 
    } 
} 

编辑:看的第一个评论,谢谢!

+0

我设法解决了我的问题,将'location ^〜/ testdir /'更改为'location/testdir /'。我仍然不知道这是为什么会起作用,并希望有人能详细说明。谢谢! – Kaurin

回答

2

According to the nginx documentation,位置指令的顺序很重要。

要确定哪个位置指令与特定查询匹配,首先检查 文字字符串。字符串匹配查询的开始部分 - 将使用最具体的匹配。 之后,按照 中定义的顺序检查正则表达式的配置文件。匹配 查询的第一个正则表达式将停止搜索。如果找不到正则表达式匹配 ,则使用文字字符串搜索的结果。

尝试组织您的位置指令,以便首先保护您要保护的目录,然后是PHP,然后是您的try__files指令。我在我的系统上做了一个快速测试,按照以下顺序构建位置块,允许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; 
} 
+0

谢谢你的帮助!你能看看问题评论部分的评论吗?谢谢! – Kaurin

+0

看看我提供的文档链接。 Nginx以特定的顺序处理位置块。 [这个答案](http://stackoverflow.com/questions/1011101/nginx-location-directive-doesnt-seem-to-be-working-am-i-missing-something/1099252#1099252)提供了一些更深入的了解那。 –