2

I'm having a big headache while configuring Nginx to work inside a location block.

I'm developing a web application with Laravel, and it is located at /srv/http/zenith. With Laravel, the index is inside the public folder, so I'm trying to reach it using the following configuration:

location /zenith/ {
  root /srv/http/zenith/public;
  try_files $uri $uri/ /index.php?$query_string;

  location ~ \.php$ {
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

But it gets me 404 error everytime. As I read from Nginx documentation, Nginx does not remove the path from the URI, so even inside /zenith/ block, all URIs still start with /zenith/. This way, example.com/zenith points to /srv/http/zenith/public/zenith when I want /srv/http/zenith/public.

How do I fix this error? I expected that Nginx removed this unwanted part automatically, but it seems to be not this way.

ranieri
  • 2,030
  • 2
  • 21
  • 39

1 Answers1

1

You need to understand the difference between a root and alias. A root maps the URI / to the directory mentioned and expects all URI parts after it to match the on disk tree. Alias maps the location of the block it's part of to the directory mentioned and expects all URI parts after this location to match the on disk tree. Since root inside a location block still maps the / URI, the part after / needs to exist on disk for things to work. In the common case you will use root for the document root and alias for location blocks.

  • It fixes one part of the problem, but now I get 403 errors. When I read the logs, I found it: `2014/02/03 13:19:50 [error] 14310#0: *24 FastCGI sent in stderr: "Unable to open primary script: /srv/http/zenith/public/zenith/info.php (No such file or directory)"`. So it's still prepending `/zenith/` to the script name :/ – ranieri Feb 03 '14 at 15:23
  • Can you post the entire server block? –  Feb 03 '14 at 18:13
  • Over 3 years later: So, did you fix this? – Parziphal Mar 10 '17 at 16:28
  • Sorry I didn't follow up. The bug in OP's case is that `/zenith/` is part of `$fastcgi_script_name` cause he told it to be. `fastcgi_split_path_info` doesn't chop off the location part it's used in. –  Mar 13 '17 at 16:58