openresty设置error_page


准备:

1.在当前路径建conf、logs、html目录

2.将配置daiyu.conf放在conf

3.将一个404.jpghtml

4.启动命令为nginx -p `pwd` -c conf/daiyu.conf


情形一:没使用proxy_pass,直接通过lua返回

user root;
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}

http {

    log_format  main  '$msec $status $request $request_time '
                      '$http_referer $remote_addr [ $time_local ] '
                      '$upstream_response_time $host $bytes_sent '
                      '$request_length $upstream_addr';

    access_log  logs/access.log main buffer=32k flush=1s;

    server {
        listen 80;

        location / {
            content_by_lua '
                ngx.exit(404)
            ';
            error_page 404 /404.jpg;
        }

        location = /404.jpg {
            root html;
        }
    }

}


情形二:使用了proxy_pass,需要拦截错误

user root;
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}

http {

    log_format  main  '$msec $status $request $request_time '
                      '$http_referer $remote_addr [ $time_local ] '
                      '$upstream_response_time $host $bytes_sent '
                      '$request_length $upstream_addr';

    access_log  logs/access.log main buffer=32k flush=1s;

    server {
        listen 80;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_intercept_errors on;
            error_page 404 /404.jpg;
        }

        location = /404.jpg {
            root html;
        }
    }

    server {
        listen 8080;

        location / {
            content_by_lua '
                ngx.exit(404)
            ';
        }
    }
    
}


运行结果:

openresty设置error_page_第1张图片



原文出自:http://blog.csdn.net/daiyudong2020/article/details/53326743


End;

你可能感兴趣的:(nginx,openresty)