Nginx重写为symfony2无效

时间:2016-10-19 13:47:32

标签: symfony nginx

我是nginx的新手,它是重写命令,我真的需要一些帮助。我一整天都试图解决这个问题,但没有。

如果用户提供此网址:

someurl.com/sub/1.0/healthcheck

我想重写它以指向Symfonys项目文件:

/var/www/sub/1.0/web/app_dev.php

网址中的“健康检查”适用于Symfony。

但是不。这里不对劲。它似乎找到了Symfony但是url有问题,因为它总是返回:

  

未找到路线

即使我从网址中省略了“healthcheck”,它仍会返回相同的错误。 (有一个索引 - 以“/”作为路径。)

这是当前的Nginx配置:

server {
  server_name localhost;
  root /var/www/sub/1.0/web;

  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;

  location / {
        root /var/www/html/;
        index index.html;
  }

  location /sub/1.0/ {
    index app_dev.php;

    rewrite ^/sub/1.0/ /app_dev.php last;
  }

  location ~ (app|app_dev).php {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  }
}

1 个答案:

答案 0 :(得分:0)

我想出了这个。问题是Symfony接受了请求uri,就fastcgi而言,它不受重写的影响。我添加了" fastcgi_param REQUEST_URI $ uri?$ args;"和tadaa!它有效!

这里是固定配置(没有多余的行,如根位置' /'):

server {
  root /var/www/sub/1.0/web;

  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;

  # If user writes the app_xxx.php into the url, remove it:
  rewrite ^/app_dev\.php/?(.*)$ /$1 permanent;

  location /sub/1.0/ {
        index app_dev.php;
        rewrite ^/sub/1.0/(.*)$ /app_dev.php/$1 last;
        return  403; # If the rewrite was not succesfull, return error.
  }

  location ~ (app|app_dev).php {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param REQUEST_URI $uri?$args;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  }
}