NGINX位置回退到Symfony

时间:2018-08-01 15:28:43

标签: php symfony nginx nginx-location

我正在使用纯PHP编写的旧API项目,现在尝试将其移至Symfony 4,并且需要对NGINX配置进行一些更改。

我需要同时使新旧端点同时工作,因此我的计划是设置NGINX,使其首先尝试服务旧端点(如果不存在)(因为我计划在我迁移到Symfony时将其删除),它将重定向到Symfony的前端控制器。

当前目录结构是这样:

Directory structure

到目前为止,我的NGINX conf如下:

server {

  root /project/www;
  index index.php;

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php-fpm:9000;
    include fastcgi_params;
    fastcgi_param  HTTPS on;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

}

工作正常,如果我请求/api/client_status使用index.php目录下的client_status,并且如果我删除该文件夹,则使用Symfony的前端控制器。  事实是,其他应用程序有时会在这些enpoints上附加index.php,例如/api/client_status/index.php,在这种情况下,我得到的是404,而不是重定向到Symfony的前端控制器。

我尝试使用重写^/api/(.*)/index.php /api/$1;在服务器块的顶部进行重写,但没有帮助。任何意见,甚至对另一种方法的建议都非常欢迎。

1 个答案:

答案 0 :(得分:1)

附加了index.php的URI将由location ~ \.php$处理。您需要向此块添加try_files语句以避免404响应,而是将请求发送到Symfony的前端控制器。

例如:

location ~ \.php$ {
    try_files $uri /index.php$is_args$args;
    ...
}