尝试反转目录结构时更新nginx配置

时间:2018-04-09 21:13:48

标签: laravel nginx fastcgi vhosts nginx-location

我正在尝试翻转包含目前具有以下目录结构的Laravel子应用程序的PHP应用程序的目录结构:

/-
    /directory1-
        script1.php
    /laravel-
        /public-
            /css-
                some-file.css
    ... other laravel files ...

我正在尝试将现在将父目录中的“遗留”代码翻转到Laravel应用程序中,以便目录结构现在如下所示:

/-
    /legacy-
        /directory1-
            script1.php
    /public-
        /css-
            some-file.css
    ... other laravel files ...

我遇到了nginx配置的困难,我能够成功运行/legacy脚本并加载Laravel中的静态公共文件(例如上面的some-file.css),但我没有Laravel路线工作正常。这很复杂,因为我不希望任何遗留路径发生变化(我仍然希望/转到现在的/legacy/index.php)并且我希望Laravel应用程序以/ laravel为前缀(因为我有前端代码调用/ laravel / api / v1中的API,我还不想更新。

这是我对两个不同位置的nginx配置所拥有的:

server {
    # ...
    root /var/www/legacy;
    index index.php index.html index.htm;

    location ~ \.php$ {
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ $uri.html $uri.php?$query_string;
    }

    location /laravel {
        fastcgi_index index.php;
        include fastcgi_params;
        rewrite ^/laravel/public/(.*) /../public/$1;
        try_files $uri /../public/index.php?$query_string;
    }
}

我从nginx获得的错误是:FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,我认为这是在谈论SCRIPT_FILENAME FastCGI参数。我尝试为此添加一个命名位置,类似于this answer中的方法:

location / {
    try_files $uri $uri/ $uri.html $uri.php?$query_string @laravel;
}

location @laravel {
    fastcgi_pass php-upstream;
    fastcgi_index index.php;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    fastcgi_param SCRIPT_FILENAME /var/www/public/index.php?$query_string;
    include fastcgi_params;
}

但这具有相同的效果。如果某人有一个解决方案,可以将来自/laravel/api/v1/.*/api/v1/.*的请求路由到Laravel应用程序,这也是值得赞赏的。

1 个答案:

答案 0 :(得分:1)

假设父目录是www,我认为以下配置(来自this post)将起作用:

server {
    # ...
    root /var/www/legacy;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ $uri.html $uri.php?$query_string;
    }

    location /laravel {
        alias /var/www/public;

        try_files $uri $uri/ @laravel;

        location ~ \.php {
            fastcgi_index index.php;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass php-upstream;
         }
    }

    location @laravel {
         rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
    }

    location ~ \.php$ {
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php-upstream;
    }
}

要处理重复的fastcgi_*声明,您可以在server块中声明它们或将它们复制到文件中并包含它,this article解释Nginx中的继承如何工作。