0

我正在尝试使用 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;
        }
    }
}

因为在/controllocation 块中有一个重写指令,所以auth_basic永远不会被触发,因为 Nginx 在身份验证之前执行重写。我应该以哪种方式修改配置,该身份验证是否有效?

PS:try_files似乎不起作用,因为它从根(/)网络文件夹提供文件!?当我将其替换为404 时if,我得到一个 404,因为 Nginx 尝试提供该文件(查看缺少的斜杠和根文件夹而不是别名)。rewritetry_files $uri /my-project/index.php?$query_string;/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;
    }
}
4

1 回答 1

1

采用:

error_page 404 =200 /my-project/index.php;

而不是丑陋的重写:

if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;

参考:

由于错误, PStry_files无法使用:http: //trac.nginx.org/nginx/ticket/97,但您可以用指令替换:http: //nginx.org/r/rootaliasaliasroot

于 2013-09-15T16:20:34.760 回答