使用渲染500页处理nginx中的lua错误

使用渲染500页处理nginx中的lua错误,nginx,lua,openresty,Nginx,Lua,Openresty,我发现自己试图通过在nginx中呈现一个500页(而不是openresty中的白色屏幕说存在内部错误)来解决lua错误,但目前没有任何成功。有人知道我如何重定向到错误路径(或其他方式)以显示自定义500页吗 这是我的nginx配置: worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http{ server { server_name localh

我发现自己试图通过在nginx中呈现一个500页(而不是openresty中的白色屏幕说存在内部错误)来解决lua错误,但目前没有任何成功。有人知道我如何重定向到错误路径(或其他方式)以显示自定义500页吗

这是我的nginx配置:

worker_processes  1;
error_log logs/error.log;

events {
  worker_connections 1024;
 }

http{
    server {
        server_name localhost;

        root /usr/local/openresty/nginx/html

        # Any route that starts with gallery/posts
        location ~ ^/gallery/(.+) {
          set $encoded_post_info $1;

          set_by_lua_block $decoded_post_info {
            # if this code breaks how can I get it handled?
            local base64 = require 'base64';
            local decodedPostInfo = base64.decode(ngx.var.encoded_post_info);

            return decodedPostInfo;
          }

          rewrite ^ /index.html break;
        }

        # Any route that doesn't have a file extension (e.g. /devices)
        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}
您可以尝试自定义错误页

location /lua_error {
  content_by_lua_block {
    # Some error by lua
  }
  error_page 500 /500.html
}


如果您想捕获错误,可能需要使用pcall/xpcall查看Alexander的注释。Doc

您知道pcall/xpcall Lua API吗?