从子目录Nginx中的Laravel删除index.php



我试图在同一域下运行两个代码库,一个静态的vue-cli网站和一个laravel后端API。

静态站点将用于前端,这将查询Laravel代码库。我很难从laravel URL中删除index.php。

我的文件系统如下;

/var/www/site/frontend/dist/index.html <-- static homepage
                           /another.html <-- another static page
/var/www/site/backend/api/index.php <-- api access

向我的API请求看起来像

/api <-- laravel landing page, only for debugging
/api/autocomplete/artist/{artistName}
/api/autocomplete/artist/{artistName}/album/{albumTitle}

我认为我很亲密,但不是很亲密,我拥有的最好的是LLaravel Landing页面,但是每当我添加路由参数时,我都会得到404,以下是我的配置;

server {
    listen 80 default_server;
    root /var/www/site/frontend/dist;
    index index.html index.htm index.php;
    server_name _;
    # Make index.php in /api url unnecessary
    location /api {
      alias /var/www/site/backend/api;
      try_files $uri $uri/ /index.php?r=$is_args$args;
     location ~ .php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_param SCRIPT_FILENAME $request_filename;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
     }
    }
    location ~ .php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }
}

您需要使用^~修饰符,否则您的其他location ~ .php$块优先于错误的文档根。有关详细信息,请参见此文档。

您不需要使用alias作为文档根的最后一部分与位置匹配 - 这简化了aliastry_files时的问题。

try_files语句的最后一个元素应该是URI,需要包括位置前缀。请参阅此文档以获取更多信息。

例如:

location ^~ /api {
    root /var/www/site/backend;
    try_files $uri $uri/ /api/index.php?r=$is_args$args;
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

最新更新