在Nginx上使用子目录的Lumen Multisite

问题描述 投票:0回答:2

我正在将Lumen(Laravel)网站部署在我们正在使用的域的子目录下,因为我们希望保留当前的旧版支持而不创建新的子域。

我搜寻了互联网后试图找出正确的方法来解决这个问题,因为它不仅仅是设置root参数(多么不幸的是),最终想到了这个方法,它感觉非常接近,尽管还不很完美,因为我的路线都不起作用(给出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$_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)
}

结果,我的路线似乎都无法解决,我为从这里出发感到困惑。

laravel nginx multisite lumen
2个回答
0
投票

我想到了。 query_string是query_string,而是request_uri 。 当Lumen的路由器尝试基于该路由器进行匹配时,但是每次它前面都带有/v2/时,因此它会认为路由不匹配。 可以在路由中(通过为所有路由添加/v2前缀)或在nginx中修复。

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

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

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

0
投票

目录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 $1;
        }
        fastcgi_param REQUEST_URI $request_lumen;
}
© www.soinside.com 2019 - 2024. All rights reserved.