nginx:使用 fastcgi 的多个文档根目录

nginx:使用 fastcgi 的多个文档根目录

当我在 http 指令中使用单个文档根目录时,一切都正常。但是,我想添加一个带有附加指令的位置指令,我无法让 fastcgi 与这个附加根目录一起工作(访问时我收到一个白页http://localhost/sqlbuddy)。

以下是我的 nginx.conf 的摘录:

server {

root /home/tman/dev/project/trunk/data;
index index.php;

location /sqlbuddy {
    root /srv/http;
    index index.php;
}

location ~* \.php {
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi.conf;
}
}

我的 fastcgi.conf 如下:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

nginx 的 error.log 和 php-fpm 的日志都没有显示任何错误。我宁愿不要把所有内容都放在同一个文档根目录中。

答案1

当你改变根目录时,你需要设置第二个位置来传递给 php:

server {
  root /home/tman/dev/project/trunk/data;
  index index.php;

  # Use location ^~ to prevent regex locations from stealing requests
  location ^~ /sqlbuddy {
    root /srv/http;

    # This location will handle requests containing .php within /sqlbuddy
    # and will use the root set just above
    location ~* \.php {
      include fastcgi.conf;
      fastcgi_pass 127.0.0.1:9000;
    }
  }

  location ~* \.php {
    include fastcgi.conf;
    fastcgi_pass 127.0.0.1:9000;
  }
}

此外,除非您使用路径信息样式的 URL(例如 /index.php/foo/bar),否则您可能需要将 .php 更改为 .php$ 以将匹配固定在 uri 的末尾。

答案2

原因是 Nginx 会选择“最佳”location块:

如果我错了请纠正我。目前,Nginx 不支持 fastcgi 的全局设置。因此,您必须重新定义fastcgi_pass

    location /sqlbuddy {
        root /srv/http;
        index index.php;
    }
    location /sqlbuddy/.+\.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
    }

或者您可以检查第二个中的$request_uriwith指令:iflocation

    location ~ \.php$ {
        if ($request_uri ~ /sqlbuddy/.*$) {
            root /srv/http;
        }
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
    }

相关内容