大网数据广告活动
服务器运维问答

Nginx rewrite to symfony2 not working

· 52CE 我爱测

I'm a newbie with nginx and it's rewrite commands and I really need some help with this one. I've been trying to solve this the whole day but nothing.. If user gives this url: someurl.com/sub/1.0/healthcheck I would like to rewri…

问题原文

原帖链接 · GotBatteries · CC BY-SA 3.0

I'm a newbie with nginx and it's rewrite commands and I really need some help with this one. I've been trying to solve this the whole day but nothing..

If user gives this url:

someurl.com/sub/1.0/healthcheck

I would like to rewrite it to point into the Symfonys project file here:

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

And the "healthcheck" in the url goes for the Symfony.

But nope. Something is wrong here. It seems that it finds the Symfony but there is something wrong with the url since it always returns:

Route not found

Even if I omit the "healthcheck" from the url, it still returns the same error. (there is an index -action with "/" as the route.)

Here is the current Nginx config:

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;
  }
}

已采纳解答

原帖链接 · GotBatteries · CC BY-SA 4.0

I figured this one out. The problem was that Symfony takes the request uri, which was not affected by the rewrite as far as fastcgi was concerned. I added the fastcgi_param REQUEST_URI $uri?$args; and tadaa! It works!

Here's the fixed config(without the excess lines, like the root location '/'):

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;
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。