【Nginx】Laravel URL重写无效空白页面的解决办法

在Nginx上部署Laravel时,发现页面空白了。中间解决了好几个问题,于是记录一下。

1、Nginx根目录需要配置成public目录

1
root /data/www/laravel/public; #网站根目录

2、Nginx配置URL重写规则

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
location / {
    try_files $uri $uri/ /index.php?$query_string;
    #try_files $uri $uri/ @rewrite;
}
 
# 去除末尾的斜杠,SEO更加友好
if (!-d $request_filename)
{
     rewrite ^/(.+)/$ /$1 permanent;
}
 
# 去除index action
if ($request_uri ~* index/?$)
{
     rewrite ^/(.*)/index/?$ /$1 permanent;
}
 
# 根据laravel规则进行url重写
if (!-e $request_filename)
{
     rewrite ^/(.*)$ /index.php?/$1 last;
     break;
}
 
location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     #fastcgi_param PATH_INFO       $fastcgi_script_name;
     include fastcgi_params;
}

3、修改fastcgi_params,增加以下配置

1
2
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_script_name;

4、通过systemctl status nginx.service查看nginx服务是否正常,如不正常参考nginx配到的问题来重新启动nginx;中间碰到以上配置怎么修改都无效就是这个问题,因为启动了其他的nginx进程导致本身的服务没有正常启动

5、在Laravel目录下运行composer install

6、确保storage目录存在,并且下面存在以下目录结构,且权限为777

      • framework
        • cache
          • data
        • sessions
        • views
      • logs

7、确保根目录下.env文件存在,且配置正确

也算是碰到了各种问题,祝各位看官配置顺利!

猜你喜欢

转载自www.cnblogs.com/thspz/p/12047943.html