0

I have installed nginx to my VPN. And I'm a very beginner with that so it might be a stupid mistake but I was unable to figure our or google it.

What I'm trying to do and keep failing in it is set the root folder for certain addresses. For example: mydomain.com/websiteone/ will have root folder in /var/www/websiteone/public/ and mydomain.com/websitetwo/ in /var/www/websitetwo/public/

Im using Laravel that why I need to do that.

Here is the confing and what I was attempting in different variations.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;

    index index.html index.php index.htm;

    location /websiteone/ {
        root /var/www/websiteone/public/;
        try_files $uri $uri/ 404;
    }

    location /websitetwo/ {
        root /var/www/websitetwo/public/;
        try_files $uri $uri/ 404;
    }
}


Marek Barta
  • 344
  • 1
  • 4
  • 17
  • May be try `location ^~ /websiteone`. Also, what exactly is not working? Did you restart/reload nginx after the config changes? What do the `/var/log/nginx/access.log` & `/var/log/nginx/error.log` show? – Utsav Kesharwani Jan 16 '20 at 12:00
  • `root` directive works by concatenating its value with the URI, so the URI `/websiteone/foo` will be located at `/var/www/websiteone/public/websiteone/foo`. You probably need to use `alias` instead. – Richard Smith Jan 16 '20 at 13:02
  • @UtsavKesharwani yes I did restart nginx. And error log is empty. Its giving me 404 – Marek Barta Jan 16 '20 at 13:03
  • @RichardSmith can you please point me to direction how it would look like. I'm beginner here – Marek Barta Jan 16 '20 at 13:04
  • Try this: https://stackoverflow.com/questions/42443468/nginx-location-configuration-subfolders/42467562#42467562 – Richard Smith Jan 16 '20 at 13:20

1 Answers1

1

No need for alias or anything fancy as long as URL dir path matches to actual directory names of your two Laravel instances (see below).

Also, Laravel has a typical "front controller" URL pattern, which means URLs that do not exist should be bootstrapped through its index.php. So:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;

    index index.html index.php index.htm;

    location /websiteone/ {
        root /var/www/websiteone/;
        try_files /public$uri /public$uri/ /public/index.php$is_args$args;
    }

    location /websitetwo/ {
        root /var/www/websitetwo/;
        try_files /public$uri /public$uri/ /public/index.php$is_args$args;
    }
}
Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
  • Thank you! I made it work for index.html however it doesnt work for php. If its get to index.php it attemps to download something – Marek Barta Jan 17 '20 at 08:15
  • Well, you obviously need a`location ~ \.php$ { ...` specifying how you proxy requests to PHP-FPM. That would be different if you're using UNIX sockets vs TCP for it. The answer cannot accommodate complete NGINX config :) – Danila Vershinin Jan 17 '20 at 21:24
  • I added the ` location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; } ` But no difference what so ever – Marek Barta Jan 22 '20 at 12:50
  • Could you please give me an example how it should look like I'm clueless here – Marek Barta Jan 22 '20 at 12:54