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 sh
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.
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;
}
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
it did not work for me, so I got another solution.
I had created a "normal" laravel domain, point to http://generic.laravel root /my/laravel/path/public
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;
}
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?
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; }
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;
}
}