nginx 自定義 404、50x 錯誤頁面的實現(xiàn)
環(huán)境 centos7.6、 nginx 1.16.1
1、安裝 nginx,需要配置 epel 源(略)
yum install -y nginx
2、啟動 nginx
systemctl start nginx systemctl enable nginx
3、配置 nginx 反向代理 http://10.2.1.16。
開通 nginx 服務(wù)器訪問 10.2.1.16 端口權(quán)限(略)
這里配置 worker_processes 4,一般根 cpu 核數(shù)一致
worker_connections 1024,跟內(nèi)存大小相關(guān)
修改 404 html 頁面到 custom_404.html
修改 50x html 頁面到 custom_50x.html
[root@ansible002 ~]# cat /etc/nginx/nginx.conf user nginx; worker_processes 4; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 90; server_name localhost; include /etc/nginx/default.d/*.conf; location / { proxy_pass http://10.2.1.16; } #修改 404 html 頁面到 custom_404.html error_page 404 /custom_404.html; location /custom_404.html { root /usr/share/nginx/html; internal; } #修改 50x html 頁面到 custom_50x.html error_page 500 502 503 504 /custom_50x.html; location /custom_50x.html { root /usr/share/nginx/html; internal; } } }
4、編輯 404、50x html 文件
[root@ansible002 ~]# cat /usr/share/nginx/html/custom_404.html <h1 style='color:red'>Error 404: Not found :</h1> <p>I have no idea where that file is, sorry. Are you sure you typed in the correct URL?</p>
[root@ansible002 ~]# cat /usr/share/nginx/html/custom_50x.html <h1>Oops! Something went wrong...</h1> <p>We seem to be having some technical difficulties. Hang tight.</p>
5、更新 nginx 配置
nginx -s reload
6、測試
訪問一個不存在的頁面測試 404,比如 http://nginx_ip/xxx
。
修改 proxy_pass http://10.2.1.16
為proxy_pass http://10.2.1.16:9099;
,代理一個不存在的服務(wù),nginx -s reload
, 訪問 http://nginx_ip
測試 50x
到此這篇關(guān)于nginx 自定義 404、50x 錯誤頁面的實現(xiàn)的文章就介紹到這了,更多相關(guān)nginx 404、50x 錯誤頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx+lua(openresty)實現(xiàn)黑/白名單權(quán)限控制的示例
本文介紹了如何使用Openresty進行權(quán)限控制和灰度發(fā)布,具體通過定時器定期更新黑名單數(shù)據(jù),進行用戶過濾和權(quán)限管控,具有一定的參考價值,感興趣的可以了解一下2024-09-09nginx http響應(yīng)限速的具體實現(xiàn)
本文主要介紹了nginx http響應(yīng)限速的具體實現(xiàn),可以使用limite_rate和limit_rate_after來限制HTTP響應(yīng)的速度,具有一定的參考價值,感興趣的可以了解一下2024-05-05nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法
今天小編就為大家分享一篇nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08nginx負載均衡下的webshell上傳的實現(xiàn)
本文主要介紹了nginx負載均衡下的webshell上傳的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03