0

I have a VM running for development, in this VM I have nginx running on an internal IP (so no hostname) and as such all I can work with is locations rather than separate servers.

The server itself has root /var/www/html;, one of my locations, /site1, located in /var/www/html/site1, has a subdirectory named public that needs to be used whenever it is accessed. Basically anything going to /site1/(.+) needs to load /site1/public/$1 instead (except for PHP files)

The issue I'm having is that no matter what setup I try, I cannot get it to not redirect to /var/www/html/site1/public/site1/, which it does as /site1/ is the current location requested by the browser.

How do I solve this? Is there any way to rewrite without giving Nginx the chance to append the path?

I cannot set the server's root to /var/www/html/site1/public, which would fix this issue (that's the setup we have in the live environment), because then the other locations on this development VM stop working.

Relevant config;

root /var/www/html;

location /site1 {
    alias /public;
}

location ~* ^/site1/.*\.php$ {
    include snippets/fastcgi-php.conf;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;

    fastcgi_pass unix:/run/php/php7.1-fpm.sock;

    fastcgi_param SCRIPT_FILENAME /var/www/site1/application/entry.php;
}
Ieuan
  • 111
  • 4
  • 1
    When you say other locations stop working, that is probably because you have something wrong in your config so that may be the thing to concentrate on. Locations are a hierarchy so it could just be a case of getting them in the right order. – Simon Greenwood Jan 16 '18 at 07:46
  • The other locations stop working if I change my `root` because they're just subdirs in `/var/www/html` that don't need any extra configuration besides the standard/default `/` location and PHP-FPM location block. I tried setting the roots in each block separately (which really shouldn't ever be necessary) but that just threw an error because I had a duplicate `root` – Ieuan Jan 16 '18 at 08:14
  • Your `alias` approach does not seem to be fine. Replace it with the full path to the directory and you should have what you want to achieve. So IMO it should look `alias /var/www/html/site1/public` – Piotr Jan 16 '18 at 09:21
  • @Piotr doing that just makes it route to `/var/www/html/site1/public/site1/` rather than the expected `/var/www/html/site1/public`. Regardless of what I do, it always appends `site1/` to it – Ieuan Jan 16 '18 at 09:38

0 Answers0