If you are looking for Nginx configuration for Laravel, here is the configuration below.
Official documentation
server {
listen 80;
server_name server_domain_or_IP;
root /var/www/travellist/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
doom
2
My laravel app root is /public. So when I try to access website it show 403 error. How do i fix that?
You need to rewrite your root / to /public subdirectory.
rewrite ^(.*)$ /public/$1 break;
Your location block for / still be the same
location / {
try_files $uri $uri/ /index.php?$query_string;
}
doom
4
Thanks. It’s working. I can see the website but root / returning 404 error header other page working fine
You can set 404 as 200 and let PHP handle the error.
error_page 404 =200 /index.php;
nazmun
6
I think you guys should add the rewrite like below
if (!-e $request_filename){
rewrite ^(.*)$ /public/$1 break;
}
So the config should look like below
index index.php server.php;
if (!-e $request_filename){
rewrite ^(.*)$ /public/$1 break;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 =200 /index.php;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
1 Like