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

允许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

图片转代码服务由CSDN问答提供 功能建议

我试图让Laravel响应自定义403错误,而不是显示默认的nginx禁止页面。

我在 app / Exceptions / Handler.php 中有大多数异常类型的自定义处理程序,它们工作得很好,403除外。我添加了 下面代码到 render 函数中的 Handler.php文件来处理403:

  if($ e instanceof HttpException)  && $ e-> getStatusCode()== 403){
返回响应(“403 Forbidden error response”); 
} 
   
 
 

但是我仍然只是看到默认的nginx禁止页面,如果我让导航到 http://myapp.com/images/

此时我相当肯定这是一个我可能缺少的nginx配置,但我不是100%肯定什么,在搜索网络后似乎无法找到解决方案 。

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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!

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsegw3424 2016-09-22 23:12

    You don't have to do anything special.

    Just create 403.blade.php under resources/views/errors/ and you should be good.

    https://laravel.com/docs/5.3/errors#custom-http-error-pages

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题