nginx的error_page配置选项

本文详细介绍了nginx的error_page配置选项,从基本语法到如何跳转其他网站、本地文件,以及在访问php文件出错时的错误页面跳转,最后提供了一个完整的配置示例,帮助读者全面掌握这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.语法

句法:	error_page code ... [=[response]] uri;
默认:	-
内容:	http,server,location,if in location

2.跳转其他网站

[root@web01 /etc/nginx/conf.d]# vim linux.web.com.conf 

server {
    listen 80;
    server_name linux.web.com;

    location / {
        root /code/web;
        index index.html;
        error_page 403 404 http://www.baidu.com;
    }
}

#error_page配置的是http这种的网络地址
#访问linux.web.com报错403、404跳转到百度页面

3.跳转本地文件

[root@web01 /etc/nginx/conf.d]# vim linux.web.com.conf 

server {
    listen 80;
    server_name linux.web.com;

    location / {
        root /code/web;
        index index.html;
        error_page 403 404 /404.jpg;
    }
}

4.访问php文件错误页面跳转

[root@web01 /etc/nginx/conf.d]# vim linux.web.com.conf
server {
    listen 80;
    server_name  linux.web.com;
    root /code/web;
    index index.php;
    error_page 404 403 /404.html;

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        if (!-e $request_filename) {
            rewrite (.*) http://linux.web.com/404.jpg;
        }
    }
}

5.完整配置

[root@zzc /blog/wordpress]# vim /etc/nginx/conf.d/wordpress.conf 

server {
    listen       80;
    server_name  localhost;
    root /blog/wordpress;

    location / {
        index  index.php;
        error_page 403 404 /404.jpg;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        if (!-e $request_filename){
            rewrite (.*) http://zzcblog.top/404.jpg;
            }
        }

    location ~* \.(jpg|png)$ {
        root /blog/wordpress;
        error_page 403 404 /404.jpg;
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值