Carl Bergman
Carl Bergman

Reputation: 97

Nginx configuration for Laravel in subfolder

I have placed my Laravel 5 project in /var/www/my_project/ and I would like to reach it at http://my_domain.com/my_project/. However, I can't figure out how to configure the nginx server block.

What I want is this:

Please note that the Laravel public folder is located at /var/www/my_project/public.

This is my nginx configuration (at /etc/nginx/sites-enabled/):

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/my_project/public;
    index index.php index.html index.htm;

    server_name my_ip;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

What is the best way to achieve the desired configuration?

Upvotes: 2

Views: 3934

Answers (2)

hook zou
hook zou

Reputation: 639

This worked for me. And other answers didn`t woked

location /rmbdatamis/ {
    root            /home/baiduwork/rmb-odp/webroot;
    index index.php;
    include     fastcgi.conf;
    fastcgi_pass $php_upstream;
    if (!-e $request_filename){
        rewrite ^/rmbdatamis/(.*) /rmbdatamis/index.php?/$1 last;
    }
}

Upvotes: 0

Hamid Naghipour
Hamid Naghipour

Reputation: 3625

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

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

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

then your config :

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;
}

2) your laravel-test folder (subfolder) inside your main :

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

then your config :

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;
}

Upvotes: 1

Related Questions