-1

我尝试只为一个子目录(laravel 目录)启用 PHP,但我没有设法让它工作。NGINX 总是说 404 File not found 或 php 说“没有指定输入文件”。我究竟做错了什么?

这是我的位置配置:

        location /laravel {
        root   html/laravel/public;
        index   index.php index.html index.html;
        try_files   $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            root   html/laravel/public;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  document_root$fastcgi_script_name;
            #include        fastcgi_params;
            include fastcgi.conf;
         }
     }

UDPATE 1:似乎 nginx 没有正确评估我的位置表达式:

2018/09/12 16:30:44 [error] 26476#24408: *1 CreateFile() "C:/Server/nginx/html/index.php" 失败(2:系统找不到指定的文件),客户端:127.0.0.1,服务器:localhost,请求:“GET /laravel/ HTTP/1.1”,主机:“localhost”

这是错误的路径,至少是 / 位置的根:

   location / {
        root   C:/Server/nginx/html;
        index  index.html index.htm index.php;
    }

我试图移动块但没有任何变化。

更新 2:似乎 nginx 非常有问题。该文档指出:

按指定顺序检查文件是否存在,并使用第一个找到的文件进行请求处理;处理在当前上下文中执行。文件的路径是根据根和别名指令从文件参数构造的。可以通过在名称末尾指定斜杠来检查目录是否存在,例如“$uri/”。如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。

正如我的错误日志所示,try_files 指令不尊重根路径,因为它试图打开相对于另一个位置块的文件。

4

2 回答 2

2

正如@Richard 在链接的 Stackoverflow 线程中指出的那样,这似乎是 nginx 的错误。对我来说,这个解决方案适用于 nginx:

        location  /laravel {
        alias html/laravel/public;
        index   index.php index.html index.html;
        try_files  $uri $uri/  @nested;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi.conf;
            fastcgi_index   index.php;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            #include        fastcgi_params;
         }
     }

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

来源:https ://serversforhackers.com/c/nginx-php-in-subdirectory

于 2018-09-12T16:23:47.900 回答
1

可能需要应用一些更改和更新:

如果你想把你的laravel项目subfolder放在一个服务器上ngnix-ubuntu 16-php.7.2,那么这里是更新 ngnix 配置:

1)您的嵌套(子文件夹)不在您的主文件夹中

/var/www/main:
/var/www/nested:

然后你的配置:

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

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

2)你的 laravel-test 文件夹(子文件夹)在你的 main :

/var/www/main:
/var/www/main/nested:

然后你的配置:

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }


  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}
于 2019-03-04T12:48:17.840 回答