Lumen Multisite 在 nginx 上使用子目录

Lumen Multisite using subdirectories on nginx

我正在努力在我们已经使用的域的子目录下部署 Lumen (Laravel) 站点,因为我们希望在不创建新子域的情况下保留对当前的遗留支持.

我在网上搜索了一下,试图弄清楚如何在了解到它不仅仅是设置 root 参数(多么不幸)之后立即执行此操作,并最终想出了这个,感觉如此接近,虽然还没有完全到位,因为我的 none 路由有效(给出 NotFoundHttpException):

location ^~ /v2 {
    alias /var/www/ver2/public;
    try_files $uri $uri/ /v2/v2/index.php?$query_string;

    location ~* \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_param SCRIPT_FILENAME $request_filename;

        include fastcgi_params;
    }
}

当我 var_dump bootstrap 中的 $_SERVER 信息时,query_string 没有发送到 php- fpm:

array(31) {
  ["USER"]=>
  string(8) "www-data"
  ["HOME"]=>
  string(8) "/var/www"
  ["FCGI_ROLE"]=>
  string(9) "RESPONDER"
  ["SCRIPT_FILENAME"]=>
  string(34) "/var/www/ver2/public/index.php"
  ["QUERY_STRING"]=>
  string(0) ""
  ["REQUEST_METHOD"]=>
  string(3) "GET"
  ["CONTENT_TYPE"]=>
  string(0) ""
  ["CONTENT_LENGTH"]=>
  string(0) ""
  ["SCRIPT_NAME"]=>
  string(13) "/v2/index.php"
  ["REQUEST_URI"]=>
  string(4) "/v2/"
  ["DOCUMENT_URI"]=>
  string(13) "/v2/index.php"
  ["DOCUMENT_ROOT"]=>
  string(24) "/var/www/ver2/public"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["HTTPS"]=>
  string(2) "on"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["SERVER_SOFTWARE"]=>
  string(11) "nginx/1.6.2"
  ["REMOTE_ADDR"]=>
  string(14) "139.182.18.248"
  ["REMOTE_PORT"]=>
  string(5) "49352"
  ["SERVER_ADDR"]=>
  string(13) "139.182.74.19"
  ["SERVER_PORT"]=>
  string(3) "443"
  ["SERVER_NAME"]=>
  string(13) "139.182.74.19"
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["HTTP_HOST"]=>
  string(13) "139.182.74.19"
  ["HTTP_USER_AGENT"]=>
  string(82) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0"
  ["HTTP_ACCEPT"]=>
  string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  ["HTTP_ACCEPT_LANGUAGE"]=>
  string(14) "en-US,en;q=0.5"
  ["HTTP_ACCEPT_ENCODING"]=>
  string(13) "gzip, deflate"
  ["HTTP_CONNECTION"]=>
  string(10) "keep-alive"
  ["PHP_SELF"]=>
  string(13) "/v2/index.php"
  ["REQUEST_TIME_FLOAT"]=>
  float(1440429882.5512)
  ["REQUEST_TIME"]=>
  int(1440429882)
}

因此,我的 none 路线似乎正在解决,我不知道从这里去哪里。

我明白了。重要的不是 query_string,而是 request_uri。当 Lumen 的路由器尝试匹配它时,它基于此,但每次它前面都会有一个 /v2/,因此它会认为路由不匹配。可以在路由中修复(通过在所有路由前加上 /v2)或在 nginx 中修复。

我丑陋的配置看起来像这样(并且可能需要一些工作,因为它使用了一个邪恶的 if 语句和 SCRIPT_FILENAME 参数的硬编码文件名):

if ($request_uri ~ ^/v2(.*)$ ) {
    set $request_url ;
}

location /v2/ {
    alias /var/www/ver2/public;
    try_files $uri $uri/ /v2/v2/index.php?$query_string; # doubled path works around an nginx bug, though I believe it's patched in recent versions

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

location ~ /v2/(.*)$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME "/var/www/ver2/public/index.php";

    fastcgi_param REQUEST_URI $request_url;
}

目录 one 是我的 wwwroot 目录中的子目录。

location /one {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /one/public/index.php?$query_string;
                # Uncomment to enable naxsi on this location
        }

location ~ /one.*\.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/home/jamlee/etc/fpm/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        if ($request_uri ~ ^/one(.*)$ ) {
                set $request_lumen ;
        }
        fastcgi_param REQUEST_URI $request_lumen;
}