Detailed explanation of nginx configuration site in laravel framework

01-28-2024

What this article brings to you is a detailed explanation of nginx configuration site in laravel framework, which has certain reference value. Friends who need it can refer to it and hope to help you. Foreword When setting up the domain name site of laravel project, you need to do some corresponding rewrite configuration for nginx to do related routing, otherwise it will be reported to 404.

Nginx.conf configuring server { listen 80; server_name xxx.com ; # domain name root /data/www/myProject/blog/public; # site directory, request to the public directory of laravel project. index index.html index.htm index.php ; # Default requested file location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php ; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location / { try_files $uri $uri/ /index.php? $query_string; # This sentence is necessary for laravel's deployment. Hide index.php. } if (! -d $request_filename) { rewrite ^/(.+)/$ /$1 permanent; } # Remove index action if ($request_uri ~* index/? $) { rewrite ^/(.*)/index/? $ /$1 permanent; } # url rewriting according to laravel rule if (! -e $request_filename) { rewrite ^/(.*)$ /index.php? /$1 last; break; } location = /50x.html { root html; } }

Operations and examples After rewriting the site for nginx.conf, to restart nginx:

sudo nginx -s reload

Take laravel version 5.2 as an example to simulate the output of hello world, and you can define a hello route in the Laravel project app/Http/routes.php:

Route::get('/hello', function(){ return 'hello world'; });

Enter xxx.com/hello in the browser to print hello world in the browser.

Related recommendations:

Settings of laravel framework rewrite under nginx

Configuration file for running PHP framework Laravel in Nginx

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us