首页 > 服务器    日期:2021-03-02 / 来自互联网 / 浏览

前言

设置laravel项目的域名站点的时候,需要对nginx做一些对应的重写rewrite配置,用来做相关路由,否则会报404。

nginx.conf配置

server {
    listen 80;
    server_name xxx.com;  #域名
    root /data/www/myProject/blog/public;  #站点目录,请求到laravel项目的public目录
    index index.html index.htm index.php;  #默认请求的文件
    
    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; # 这一句是laravel部署必须的,将index.php隐藏掉
    }

    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 = /50x.html {
          root   html;
    }
}

操作及实例

1.对nginx.conf重写站点后,要重启nginx:

sudo nginx -s reload

2.以laravel5.2版本为例,模拟输出hello world,可以在laravel项目中app/Http/routes.php中定义一个hello的路由:

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

3.浏览器输入xxx.com/hello即可在浏览器打印出hello world

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章