23

I have and old project that now requires new functionality, I'm going to use laravel to provide it, everything in working ok in xampp with apache but my server con nginx show me access denied message and cant access my routes, how should be my site config should be if laravel is installed in mysite.com/2015 my site config is the following, what showld I change? I have tried

location /newsection/ { 
   try_files $uri $uri/ /newsection/public/index.php$request_uri;
}

but it causes 500 error

server {
    listen 80;
    server_name am2.aminversiones.com;
    root /home/forge/am2.aminversiones.com;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    client_max_body_size 300M;

    location / {
        #try_files $uri $uri/ /index.php?$query_string;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/am2.aminversiones.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
       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;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    # version 1
    location ^~ /2015 {
        alias /home/forge/am2.aminversiones.com/2015/public;
        try_files $uri $uri/ @2015;
        location ~* \.php {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            include /etc/nginx/fastcgi_params;
        }
    }

    location @2015 {
        rewrite ^/2015/(.*)$ /2015/index.php/$1 last; # THIS IS THE IMPORTANT LINE
    }
    # end version 1

    # version 2
    # this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1`
    location ~ /2015 {
        try_files /2015/$uri /2015/$uri/ /2015/index.php?q=$uri&$args;
    }
    # end version 2

    # PHP FPM configuration.
    location ~* \.php$ {
        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        include                             /etc/nginx/fastcgi_params;
        fastcgi_index                       index.php;
        fastcgi_split_path_info             ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO             $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED       $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
        deny all;
    }
}

9 Answers 9

28

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.

4
  • This fixed an issue i had for a few days now. Thanks
    – cfv1000
    May 28, 2015 at 13:56
  • This works great, but if I do: example.org/laravel/?var=12345 it will give me an 404 error. Any ideas how to solve it, regards!
    – Alberto
    Jun 19, 2015 at 15:31
  • @Alberto make sure to reload nginx and php5-fpm
    – Lu32
    Jun 19, 2015 at 21:07
  • That's not working. it forces php files to download than to be served
    – Sebastian
    Nov 18, 2020 at 21:33
21

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;
}
9
  • Thanks, the first option works. And why rewrite /nested/(.*)$ /nested/index.php?/$1 last; but not rewrite /nested/(.*)$ /nested/public/index.php?/$1 last; ?
    – XNicON
    Apr 24, 2019 at 14:45
  • 2
    There is no difference between the 2 config Dec 27, 2019 at 23:57
  • 1
    Now that's an answer. Thanks for taking the time.
    – Paschalis
    Apr 23, 2020 at 19:21
  • 1
    Thanks for the solution! I was searching a lot for a solution of this problem. Oct 3, 2020 at 11:21
  • 1
    I am happy to report that I tried this and it worked. I am running Homestead which uses nginx.
    – ryantxr
    May 8, 2021 at 19:25
10

This is the workaround that solved my problem With 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

4
  • 1
    This is the only answer I found on the whole internet and beyond that works. The most important part is: remove ?/$1. Don't remove it and your query strings will all fail miserably. Thanks a lot!
    – Bloops
    Apr 5, 2021 at 1:36
  • In my case, this worked because other nginx.conf suggestions use fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;, however it was the use of fastcgi_param SCRIPT_FILENAME $request_filename; which finally fixed my server and brought me peace of mind . Aug 21, 2021 at 20:52
  • Thank you so much for your comment. Removing these 4 characters ?/$1works well for me. Up until some Laravel 8 update my system worked well with these characters, then Laravel added $request->server->get('QUERY_STRING') to the evaluation of a signed route, which got my system into problems. Solved now.
    – LaDude
    Dec 20, 2021 at 10:30
  • This works great for me - Laravel 9 and PHP 8.1 on NGINX 1.22. With the other solutions I can't access the Laravel's API.
    – pa4080
    Jan 20 at 9:58
2

For some reason for me, the alias was causing the issue and didn't work. So maybe this will help others, so here is what I did to make this work. As you can see I took the "alias" out of it and added laravel/public to the equation.

location ^~ /laravel/public {
            index home.php home.html home.htm index.html index.htm index.php;
            try_files $uri $uri/ @laravel;

            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-XSS-Protection "1; mode=block";
            add_header X-Content-Type-Options "nosniff";
            charset utf-8;

            location ~ \.php {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                include fastcgi_params;
            }
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

    location @laravel {
       rewrite /laravel/public/(.*)$ /laravel/public/index.php?/$1 last;
    }
2
  • For more clear and constructive answer, you can add more details on how and why the solution is working for you to make it more understandable. With that, keep it up and thanks for sharing your possible solution to the problem :) Apr 3, 2020 at 14:53
  • I'm not sure why it didn't work with the alias I don't fully understand how the Nginx configuration works. I'm not sure either of the difference it makes with or without alias.
    – Pat M
    Apr 4, 2020 at 17:31
1

it did not work for me, so I got another solution.

  1. I had created a "normal" laravel domain, point to http://generic.laravel root /my/laravel/path/public

  2. After that, I created a location on real domain proxying to my generic Laravel:

    location /laravel {
        rewrite /laravel/?(.*)$ /$1 break;
        proxy_pass http://generic.laravel;
    

    }

  3. Unfortunately, Laravel is going to use the url http://generic.laravel to create links. You may to solve it following this steps Laravel: Change base URL?

0

I have met several case like error 403 or 404, and I think this method works. Don't forget to change php version based on your php version in server (php8.1-fpm.sock). Note your_domain www.your_domain is based on your website.

   server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.html index.htm index.php;
    server_name your_domain www.your_domain;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location /yourlaravelproject {
        alias /var/www/html/yourlaravelproject/public;
        try_files $uri $uri/ /index.php$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}

after you configure, don't forget to add this to your symlink, then systemctl restart nginx. I'm sure for Laravel 8 and 9 still works, because I just recently test Laravel 9 project in subfolder and works fine.

0

In the server directive, specify the public folder in the root parameter

in file /etc/nginx/sites-available/am2.aminversiones.com

server {
    ...
    root /home/forge/am2.aminversiones.com/public;
    ...
}

restart the nginx service: sudo systemctl restart nginx

-2

location /stationery { alias /var/www/html/stationery/public; try_files $uri $uri/ @stationery; location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; } }

location @stationery{ rewrite /stationery/(.*)$ /stationery/index.php?/ last; }

1
  • Please add some description to the answer you have provided.
    – asolanki
    Mar 6, 2020 at 4:20
-3

please use this :

server {
client_body_in_file_only clean;
client_body_buffer_size 32K;

client_max_body_size 300M;

sendfile on;
send_timeout 300s;
# Port that the web server will listen on.
#listen          80;

# Host that will serve this project.
server_name     tsolar.com;

# Useful logs for debug.
access_log      /var/log/nginx/tsolar.com-access.log;
error_log       /var/log/nginx/tsolar.com-error.log;
rewrite_log     on;

# The location of our projects public directory.
root            /home/tom/public_html/demos/;

# Point index to the Laravel front controller.
index           index.php;

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {

    # URLs to attempt, including pretty ones.
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# Remove trailing slash to please routing system.
if (!-d $request_filename) {
    rewrite     ^/(.+)/$ /$1 permanent;
}

# version 1
location ^~ /demo1 {
    alias /home/tom/public_html/demos/demo1/public;
    try_files $uri $uri/ @demo1;

    location ~* \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @demo1 {
    rewrite ^/demo1/(.*)$ /demo1/index.php/$1 last; # THIS IS THE IMPORTANT LINE
}
# end version 1

# version 2
# this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1`
location ~ /demo1 {
    try_files /demo1/$uri /demo1/$uri/ /demo1/index.php?q=$uri&$args;
}
# end version 2


# PHP FPM configuration.
location ~* \.php$ {
    fastcgi_pass                    unix:/var/run/php5-fpm.sock;
    include                             /etc/nginx/fastcgi_params;
    fastcgi_index                       index.php;
    fastcgi_split_path_info             ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO             $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED       $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
}

# We don't need .ht files with nginx.
location ~ /\.ht {
    deny all;
}

# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
    expires 365d;
}

}

1
  • This might solve the OP problem, but it is a complete configuration. It does not help in figuring out what's wrong in the OP's situation and what precisely solves the problem. Can you make those points clearer and more explicit? Sep 12, 2017 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.