nginx 自定義 404、50x 錯(cuò)誤頁面的實(shí)現(xiàn)
環(huán)境 centos7.6、 nginx 1.16.1
1、安裝 nginx,需要配置 epel 源(略)
yum install -y nginx
2、啟動(dòng) 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、測試
訪問一個(gè)不存在的頁面測試 404,比如 http://nginx_ip/xxx。
修改 proxy_pass http://10.2.1.16 為proxy_pass http://10.2.1.16:9099;,代理一個(gè)不存在的服務(wù),nginx -s reload, 訪問 http://nginx_ip 測試 50x
到此這篇關(guān)于nginx 自定義 404、50x 錯(cuò)誤頁面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nginx 404、50x 錯(cuò)誤頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
lnmp環(huán)境中如何為nginx開啟pathinfo
這篇文章主要介紹了lnmp環(huán)境中如何為nginx開啟pathinfo的方法,操作很簡單,需要的朋友可以參考下2015-01-01
centos7下基于nginx+uwsgi部署Django項(xiàng)目的實(shí)現(xiàn)
Django是一個(gè)開源的Web應(yīng)用框架,使用Python語言編寫,主要用于搭建Web項(xiàng)目,本教程介紹如何在centos7下基于nginx+uwsgi部署Django項(xiàng)目的實(shí)現(xiàn),感興趣的可以了解一下2024-04-04
keepalived+nginx實(shí)現(xiàn)雙服務(wù)器主備方案
本文主要介紹了使用keepalived和nginx實(shí)現(xiàn)雙服務(wù)器主備方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Ubuntu+Nginx+Mysql+Php+Zend+eaccelerator安裝配置文字版
把我架設(shè)lnmp網(wǎng)站的過程寫出來,希望對想架設(shè)網(wǎng)站的朋友有所幫助,如有更好的辦法請?zhí)岢鰜?/div> 2012-02-02
Nginx報(bào)錯(cuò)“Too many open files”的問題解決
本文主要介紹了Nginx報(bào)錯(cuò)“Too many open files”的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05最新評論

