作者nevikw39 (☆牜攵☆犬羊)
看板Web_Design
标题[问题] nginx 错误处理
时间Sat Feb 9 22:23:53 2019
如题,nginx 好像比较推荐一个错误一个网页,但是我以为这样十分没有效率,因此想用一
个网页透过参数来处理不同的错误。
#default.conf
server {
...
error_page 400 401 402 ... 504 505 @err;
location @err {
try_files error/base.html?code=$status error/base.html;
#internal;
}
}
<!---base.html--->
...
<script>
$.getJSON('/error/error.json', j => {
code = new URL(location.href).searchParams.get('code');
if (code === null) {
return 0;
}
$('#title').text(code + ' ' + j[code]['name']);
$('#code').text(code);
$('#name').text(j[code]['name']);
$('
#description').text(j[code]['description']);
});
</script>
如果直接 get error/base.html?code=411 之类的,都可以达到预期的结果,但是真的遇到
404 时,却显示的是原本的空白内容。不晓得是哪里出错了?
发在本板不知道合不合适?若不妥再自删
--
Sent from my Sony Xperia XZ1
○ PiTT
// PHJCI
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 106.107.176.158
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Web_Design/M.1549722235.A.949.html
1F:→ Sunal: error/base.html 的location怎麽设定的?02/10 00:43
原先没有设定,照 s 大的意思是改成这样吗:
location error/base.html {
root #网站的根目录 ;
}
location error/error.json {
root #网站的根目录 ;
}
还是一样欸
※ 编辑: nevikw39 (106.107.176.158), 02/10/2019 10:19:54