我正在尝试使用Nginx将项目添加到现有网络服务器的子文件夹中。这是我的简单配置:
server {
listen 80 default_server;
server_name localhost;
root /var/www;
[...]
location = /my-project { return 301 /my-project/; }
location /my-project/ {
alias /var/www/my-project/web/;
index index.php;
location ~ /[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
}
if (-f $request_filename) { break; }
rewrite ^ /my-project/index.php last;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
}
因为/control
位置块中有重写指令,auth_basic
永远不会被触发,因为Nginx在认证之前执行重写。我应该以哪种方式修改配置,auth是否有效?
PS:try_files
似乎不起作用,因为它提供来自root(/)webfolder的文件!?当我用if
替换rewrite
和try_files $uri /my-project/index.php?$query_string;
后,我得到了404,因为Nginx尝试提供文件/var/wwwindex.php
(看看缺少斜杠和根文件夹) /var/www
代替别名。
编辑18.09.2013: 正如VBart建议我现在使用以下配置来使身份验证工作
location ~ ^/(?<dir>my-project)(?<path>/.*)$ {
root /var/www/$dir/web;
location ~ ^/[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
try_files $path /$dir/index.php?$query_string;
}
try_files $path /$dir/index.php?$query_string;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
答案 0 :(得分:1)
使用:
error_page 404 =200 /my-project/index.php;
而不是丑陋的重写:
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
参考:
P.S。由于错误http://trac.nginx.org/nginx/ticket/97,try_files
无法与alias
一起使用,但您可以使用alias
指令替换root
:http://nginx.org/r/root