【问题标题】:Lumen Multisite using subdirectories on nginxLumen Multisite 在 nginx 上使用子目录
【发布时间】:2015-11-18 02:25:43
【问题描述】:

我正在在我们已经使用的域的子目录下部署 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


    【解决方案1】:

    我想通了。重要的不是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;
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2018-01-15
        • 1970-01-01
        • 2012-02-24
        • 2016-06-20
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 2016-03-24
        • 2019-02-17
        相关资源
        最近更新 更多