2

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
  • 131
  • 6

1 Answers1

1

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;
  }
}
GotBatteries
  • 131
  • 6