使用Nginx Ingress 優(yōu)雅顯示錯誤頁面
更新時間:2023年09月24日 10:41:36 作者:moon
這篇文章主要為大家介紹了使用Nginx Ingress 優(yōu)雅顯示錯誤頁面實現示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
一. Nginx Ingress 優(yōu)雅顯示錯誤頁面
- 讓所有經過 Nginx Ingress 暴露出去的域名,在請求到錯誤頁面的時候(狀態(tài)碼為:4xx,5xx等)給用戶一個優(yōu)雅的頁面顯示,而不直接顯示4xx或5xx的報錯,避免給用戶不好的使用體驗
1. 鏡像制作
- 將顯示的頁面全部放在 www 目錄中
[root@yunwei-k8s-addon1-test custom-error]# ll www/ -rw-r--r--. 1 yeemiao yeemiao 415 3月 13 2019 403.html -rw-r--r--. 1 yeemiao yeemiao 412 3月 13 2019 404.html -rw-r--r--. 1 root root 412 8月 16 13:40 500.html -rw-r--r--. 1 root root 412 8月 16 13:40 501.html -rw-r--r--. 1 yeemiao yeemiao 415 3月 13 2019 502.html -rw-r--r--. 1 yeemiao yeemiao 409 3月 13 2019 503.html -rw-r--r--. 1 root root 412 8月 16 13:40 504.html # 目錄結構 [root@yunwei-k8s-addon1-test custom-error]# ll -rw-r--r--. 1 root root 72 8月 16 13:37 Dockerfile drwxr-xr-x. 2 root root 118 8月 16 13:41 www
FROM registry.k8s.io/ingress-nginx/nginx-errors:v20230505 COPY www /www
2. nginx-error
- 放在和 Nginx-Ingress 控制器同樣的名稱空間中
apiVersion: v1
kind: Service
metadata:
name: nginx-errors
namespace: ingress-nginx
labels:
app.kubernetes.io/name: nginx-errors
app.kubernetes.io/part-of: ingress-nginx
spec:
selector:
app.kubernetes.io/name: nginx-errors
app.kubernetes.io/part-of: ingress-nginx
ports:
- port: 80
targetPort: 8080
name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-errors
namespace: ingress-nginx
labels:
app.kubernetes.io/name: nginx-errors
app.kubernetes.io/part-of: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: nginx-errors
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: nginx-errors
app.kubernetes.io/part-of: ingress-nginx
spec:
containers:
- name: nginx-error-server
image: harbor.yeemiao.com:8443/library/custom-nginx-error:20230816
ports:
- containerPort: 8080配置
- 修改Nginx-Ingress 的啟動參數
# 加入參數,如果 nginx-error 和 Nginx-Ingress在同一名稱空間,直接加入參數即可,如果 nginx-error 服務不在 Nginx-Ingress 控制器同樣的 Namespace,請將 POD_NAMESPACE 改為他所在的名稱 - --default-backend-service=$(POD_NAMESPACE)/nginx-errors # 修改 Nginx-Ingress 控制器的configmap [root@k8s-alone-1 manifest]# kubectl -n ingress-nginx edit cm nginx-configuration apiVersion: v1 data: allow-snippet-annotations: "true" custom-http-errors: 403,404,413,500,501,502,503,504 # 增加這個配置
3. 驗證
- 進入 nginx-ingress的控制器查看nginx 配置文件
/etc/nginx/nginx.conf, 過濾 error_page 是否有相關配置
error_page 403 = @custom_upstream-default-backend_403;
error_page 404 = @custom_upstream-default-backend_404;
error_page 413 = @custom_upstream-default-backend_413;
error_page 500 = @custom_upstream-default-backend_500;
error_page 502 = @custom_upstream-default-backend_502;
error_page 503 = @custom_upstream-default-backend_503;
error_page 504 = @custom_upstream-default-backend_504;以上就是使用Nginx Ingress 優(yōu)雅顯示錯誤頁面的詳細內容,更多關于Nginx Ingress顯示錯誤頁面的資料請關注腳本之家其它相關文章!
相關文章
通過Nginx代理轉發(fā)配置實現跨域的方法(API代理轉發(fā))
這篇文章主要給大家介紹了關于如何通過Nginx代理轉發(fā)配置實現跨域(API代理轉發(fā))的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-11-11

