Config nginx for Laravel In a subfolder Config nginx for Laravel In a subfolder nginx nginx

Config nginx for Laravel In a subfolder


Well, I found a solution to very easy config and install Laravel in a subdirectory in a nginx server, in the /etc/nginx/sites-available/yourSite config file, add this:

location ^~ /laravel {    alias /var/www/laravel/public;    try_files $uri $uri/ @laravel;    location ~ \.php {        fastcgi_pass unix:/var/run/php5-fpm.sock;        fastcgi_split_path_info ^(.+\.php)(.*)$;        include /etc/nginx/fastcgi_params;    }}location @laravel {    rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;}

and voila, your routes will work normally how they should.


after I spend some hours on this issue, finally I fixed my problem with a subdomain address like this:

If you want to put your laravel project in a subfolder on a server with ngnix-ubuntu 16-php.7.2, so here is the ngnix config :

  1. your nested(subfolder) isn't inside your main folder

    /var/www/main:/var/www/nested:

then your config should be :

location /nested {        alias /var/www/nested/public;        try_files $uri $uri/ @nested;               location ~ \.php$ {                        include fastcgi_params;                        fastcgi_param SCRIPT_FILENAME $request_filename;                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;                                }   }location @nested {        rewrite /nested/(.*)$ /nested/index.php?/$1 last;}
  1. The laravel-test folder (subfolder) inside the main folder :

    /var/www/main:/var/www/main/nested:

then your config should be :

location /laravel-test {    alias /var/www/main/laravel-test/public;    try_files $uri $uri/ @laravelTest;           location ~ \.php$ {                    include fastcgi_params;                    fastcgi_param SCRIPT_FILENAME $request_filename;                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;                            }  }location @laravelTest {        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;}


This is the workaround that solved my problemWith alias, Nginx does NOT look for files within /var/www/portal/public/portal/foo, like it would with the root directive

location /portal { alias /var/www/html/portal/public; #preferred over root # @portal is a named location try_files $uri $uri/ @portal; location ~ \.php$ {    fastcgi_split_path_info ^(.+\.php)(/.+)$;    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;    fastcgi_index index.php;    include fastcgi_params;    fastcgi_param SCRIPT_FILENAME $request_filename; }}location @portal {rewrite /portal/(.*)$ /portal/index.php last; # Remove ?/$1 since fastcgi_params adds query string}

Additional reference can be found from this article https://gist.github.com/tsolar/8d45ed05bcff8eb75404