donglugou6652 2016-09-22 21:13
浏览 488
已采纳

允许Laravel响应403而不是nginx

I am trying to have Laravel respond a custom 403 error rather than display the default nginx forbidden page.

I have custom handlers for most of my exception types in app/Exceptions/Handler.php and they are working just fine, except for 403. I added the following code to the Handler.php file within the render function to handle 403:

if($e instanceof HttpException && $e->getStatusCode() == 403){
        return response("403 Forbidden error response");
    }

but I still just see the default nginx forbidden page if I lets say navigate to http://myapp.com/images/

At this point I am fairly certain this is an nginx config I may be missing, but am not 100% sure what and after searching the web can't seem to find the solution.

Thanks

  • 写回答

2条回答 默认 最新

  • duanjie3267 2016-09-23 17:46
    关注

    Thanks for the advice and reviews everyone, I have been able to find and fix what was causing the issue. As Elias Soares mentioned above in comment, the issue was obviously happening before Laravel was even reached. I went into my nginx config file and noticed the following line:

    error_page 404 /index.php;
    

    and since Laravel was indeed correctly handling 404 errors, I simply added the exact same thing for 403:

    error_page 403 /index.php;
    

    and now Laravel is handling any 403 errors the way I wanted it to do.

    Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题