使用Nginx+lua返回错误码并且返回自定义内容。
2016-12-28 16:48
417 查看
使用Nginx+lua返回错误码并且返回自定义内容。
使用ngx.exit(后端服务返回的httpcode),如果后台返回的是500 等错误码的时候如下用法会有错误:
上面的代码在后端服务器返回非200的时候,会返回客户端 200,内容则是错误信息;日志显示大体是:在返回200之后,又设置成500 。
上面的代码,会返回 500错误码和后端app返回的错误信息;
结论:
ngx.status 是设置http 相应码的。
ngx.exit(code):官方文档如下
ngx.exit
syntax: ngx.exit(status)
context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, ngx.timer., balancer_by_lua, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*
When status >= 200 (i.e., ngx.HTTP_OK and above), it will interrupt the execution of the current request and return status code to nginx.
When status == 0 (i.e., ngx.OK), it will only quit the current phase handler (or the content handler if the content_by_lua* directive is used) and continue to run later phases (if any) for the current request.
The status argument can be ngx.OK, ngx.ERROR, ngx.HTTP_NOT_FOUND, ngx.HTTP_MOVED_TEMPORARILY, or other HTTP status constants.
To return an error page with custom contents, use code snippets like this:
使用ngx.exit(后端服务返回的httpcode),如果后台返回的是500 等错误码的时候如下用法会有错误:
ngx.say(res.content) ngx.exit(res.httpCode);
上面的代码在后端服务器返回非200的时候,会返回客户端 200,内容则是错误信息;日志显示大体是:在返回200之后,又设置成500 。
ngx.status = res.httpCode; ngx.say(res.content); ngx.exit(200);
上面的代码,会返回 500错误码和后端app返回的错误信息;
结论:
ngx.status 是设置http 相应码的。
ngx.exit(code):官方文档如下
ngx.exit
syntax: ngx.exit(status)
context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, ngx.timer., balancer_by_lua, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*
When status >= 200 (i.e., ngx.HTTP_OK and above), it will interrupt the execution of the current request and return status code to nginx.
When status == 0 (i.e., ngx.OK), it will only quit the current phase handler (or the content handler if the content_by_lua* directive is used) and continue to run later phases (if any) for the current request.
The status argument can be ngx.OK, ngx.ERROR, ngx.HTTP_NOT_FOUND, ngx.HTTP_MOVED_TEMPORARILY, or other HTTP status constants.
To return an error page with custom contents, use code snippets like this:
ngx.status = ngx.HTTP_GONE ngx.say("This is our own content") -- to cause quit the whole request rather than the current phase handler ngx.exit(ngx.HTTP_OK)
相关文章推荐
- [转]使用自定义HttpMessageConverter对返回内容进行加密
- 使用自定义HttpMessageConverter对返回内容进行加密
- 使用Nginx+Lua实现自定义WAF
- Asp.Net 使用jquery json,注意后台返回的内容。
- ABAP–如何在’REUSE_ALV_GRID_DISPLAY’使用自定义F4帮助,返回多个字段以及计算修改其他字段
- 不使用Views,自定义drupal输出内容
- 使用SNMP发送自定义内容
- webwok中使用自定义的返回类型(result_type)
- 使用jQuery动态创建一个表格(根据用户输入的内容添加一行数据,并且能逐行删除)
- ABAP--如何在'REUSE_ALV_GRID_DISPLAY'使用自定义F4帮助,返回多个字段以及计算修改其他字段
- Hibernate高级使用:查询返回自定义对象
- 使用Lua脚本为wireshark编写自定义通信协议解析器插件
- 使用自定义的facet返回千位分隔符的浮点数据格式
- 在VFP(Visual FoxPro)中使用Msxml2.ServerXMLHTTP读取返回内容出现乱码(问题?)问题
- Nginx自定义404错误页面并返回404状态码
- 在Action中查询出SQL SERVER表的数据,返回一个list。怎么在JSP页面中使用Struts标签,显示出表中的内容?(转)
- 在Module中使用自定义过滤器,来统一对站内所有请求响应的输出内容进行采集或更改。
- Ethereal 也可以使用lua做一些自定义的功能了--编写lua脚本扩展ethereal的功能
- 一个使用c++在lua中创建自定义数据类型的简易方法
- 简单标签的使用自定义标签控制页面内容(标签体)是否输出、