[問題] nginx 錯誤處理

看板Web_Design作者 (☆牜攵☆犬羊)時間6年前 (2019/02/09 22:23), 6年前編輯推噓0(001)
留言1則, 1人參與, 6年前最新討論串1/1
如題,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://www.ptt.cc/bbs/Web_Design/M.1549722235.A.949.html

02/10 00:43, 6年前 , 1F
error/base.html 的location怎麼設定的?
02/10 00:43, 1F
原先沒有設定,照 s 大的意思是改成這樣嗎: location error/base.html { root #網站的根目錄 ; } location error/error.json { root #網站的根目錄 ; } 還是一樣欸 ※ 編輯: nevikw39 (106.107.176.158), 02/10/2019 10:19:54
文章代碼(AID): #1SNk9xb9 (Web_Design)
文章代碼(AID): #1SNk9xb9 (Web_Design)