问题:

  • 访问异常
root@cloud:/usr/local/nginx# curl -i http://localhost/test.html
curl: (52) Empty reply from server
  • 错误日志
2016/09/11 13:17:03 [alert] 63560#0: worker process 63663 exited on signal 11 (core dumped)
  • dmesg信息
[265950.220943] nginx[63663]: segfault at 128 ip 000000000048259d sp 00007ffde898eab0 error 4 in nginx[400000+a5000]
  • core dump设置

    默认Worker进程用户nobody:nogroup,无法写coredump. 需在nginx.conf配置:
worker_rlimit_core 1024m; #1G
working_directory /tmp/core #保证nobody:nogroup有W权限

执行"sbin/nginx -s reload"重新加载配置。

  • core dump分析
  • 使用readelf工具

    core dump是ELF格式文件,你可以用readelf -a 查看core dump文件,但哪只是一堆数据。
  • 使用gdb工具
gdb <command> <core>

一般会直接显示出错的代码位置

  • 位置1
    conf = ngx_http_conf_get_module_loc_conf(r,ngx_http_myfilter_module); #应该是ngx_http_get_module_loc_conf(...)
  • 位置2
    myfilter_ctx_t *ctx;
    ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);
    if (ctx->add_prefix != 1) { 应该先判断ctx == NULL
    return next_body_filter(r, in);
    }

Nginx http filter示例源码

ngx_addon_name=ngx_http_myfilter_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_myfilter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_myfilter_module.c"
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static ngx_str_t filter_prefix = ngx_string("[my filter prefix]");
static ngx_http_output_header_filter_pt next_header_filter;
static ngx_http_output_body_filter_pt next_body_filter; typedef struct {
ngx_flag_t enable;
} myfilter_conf_t; typedef struct {
ngx_int_t add_prefix;
} myfilter_ctx_t; static ngx_int_t myfilter_header_filter(ngx_http_request_t *r);
static ngx_int_t myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in);
static ngx_int_t myfilter_init(ngx_conf_t *cf); static void *myfilter_create_loc_conf(ngx_conf_t *cf);
static char *myfilter_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_http_module_t myfilter_conf = {
NULL,
myfilter_init,
NULL,
NULL,
NULL,
NULL,
myfilter_create_loc_conf,
myfilter_merge_loc_conf
}; static ngx_command_t myfilter_commands[] = {
{
ngx_string("add_prefix"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(myfilter_conf_t, enable),
NULL
},
ngx_null_command
}; ngx_module_t ngx_http_myfilter_module = {
NGX_MODULE_V1,
&myfilter_conf,
myfilter_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static ngx_int_t myfilter_init(ngx_conf_t *cf){
next_header_filter = ngx_http_top_header_filter;
next_body_filter = ngx_http_top_body_filter; ngx_http_top_header_filter = myfilter_header_filter;
ngx_http_top_body_filter = myfilter_body_filter; return NGX_OK;
} static ngx_int_t myfilter_header_filter(ngx_http_request_t *r){
myfilter_conf_t *conf;
myfilter_ctx_t *ctx; if(r->headers_out.status != NGX_HTTP_OK){
return next_header_filter(r);
} ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);
if(ctx){
return next_header_filter(r);
} conf = ngx_http_get_module_loc_conf(r,ngx_http_myfilter_module);
if(conf == NULL || conf->enable == 0){
return next_header_filter(r);
} ctx = ngx_pcalloc(r->pool, sizeof(myfilter_ctx_t));
if(ctx == NULL){
return NGX_ERROR;
}
ctx->add_prefix = 0;
ngx_http_set_ctx(r, ctx, ngx_http_myfilter_module); r->headers_out.status = NGX_HTTP_MOVED_TEMPORARILY;
ngx_str_t keys = ngx_string("Location");
ngx_str_t vals = ngx_string("http://www.163.com");
ngx_table_elt_t *headers = ngx_list_push(&r->headers_out.headers);
if(headers){
headers->key.data = keys.data;
headers->key.len = keys.len;
headers->value.data = vals.data;
headers->value.len = vals.len;
headers->hash = 1;
} // if (r->headers_out.content_type.len >= sizeof("text/plain") - 1
// && ngx_strncasecmp(r->headers_out.content_type.data,
// (u_char *) "text/plain", sizeof("text/plain") - 1) == 0) {
ctx->add_prefix = 1;
if(r->headers_out.content_length_n > 0){
r->headers_out.content_length_n += filter_prefix.len;
}
// } return next_header_filter(r);
} static ngx_int_t myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in) {
myfilter_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);
if (ctx == NULL || ctx->add_prefix != 1) {
return next_body_filter(r, in);
} ctx->add_prefix = 2;
ngx_buf_t *b = ngx_create_temp_buf(r->pool, filter_prefix.len);
b->start = b->pos = filter_prefix.data;
b->last = b->pos + filter_prefix.len; ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);
cl->buf = b;
cl->next = in;
return next_body_filter(r, cl);
} static void *myfilter_create_loc_conf(ngx_conf_t *cf) {
myfilter_conf_t *mycf;
mycf = (myfilter_conf_t *)ngx_pcalloc(cf->pool, sizeof(myfilter_conf_t));
if(mycf == NULL) return NULL;
mycf->enable = NGX_CONF_UNSET;
return mycf;
} static char *myfilter_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){
myfilter_conf_t *prev = (myfilter_conf_t *)parent;
myfilter_conf_t *conf = (myfilter_conf_t *)child; ngx_conf_merge_value(conf->enable, prev->enable, 0);
return NGX_CONF_OK;
}

Nginx http filter异常排查的更多相关文章

  1. redis 异常排查

    异常排查 redis-server redis.windows.conf D:\redis-2.8.17>redis-server.exe redis.windows.conf[4692] 27 ...

  2. Nginx range filter模块数字错误漏洞修复 (Nginx平滑升级) 【转】

    对线上生产环境服务器进行漏洞扫描, 发现有两台前置机器存在Nginx range filter模块数字错误漏洞, 当使用nginx标准模块时,攻击者可以通过发送包含恶意构造range域的header ...

  3. Nginx range filter模块数字错误漏洞修复 (Nginx平滑升级)

    对线上生产环境服务器进行漏洞扫描, 发现有两台前置机器存在Nginx range filter模块数字错误漏洞, 当使用nginx标准模块时,攻击者可以通过发送包含恶意构造range域的header ...

  4. 一次django内存异常排查

    起因 Django 作为 Python著名的Web框架,相信很多人都在用,自己工作中也有项目项目在用,而在最近几天的使用中发现,部署Django程序的服务器出现了内存问题,现象就是运行一段时间之后,内 ...

  5. #研发解决方案#基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案

    郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...

  6. 【转】数据库系统异常排查之DMV

    数据库系统异常是DBA经常要面临的情景,一名有一定从业经验的DBA,都会有自己一套故障排查的方法和步骤,此文为为大家介绍一下通过系统 性能视图(SQLServer05以上版本)来排查系统异常的基本方法 ...

  7. The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. 异常

    异常信息如下: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without t ...

  8. 基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案 郑昀 基于杨海波的设计文档(转)

    郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...

  9. CPU负载过高异常排查实践与总结

    昨天下午突然收到运维邮件报警,显示数据平台服务器cpu利用率达到了98.94%,而且最近一段时间一直持续在70%以上,看起来像是硬件资源到瓶颈需要扩容了,但仔细思考就会发现咱们的业务系统并不是一个高并 ...

随机推荐

  1. 多事务运行并发问题spring学习笔记——数据库事务并发与锁详解

    多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...

  2. 【30.49%】【codeforces 569A】Music

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 浅谈python字符串存储形式

    http://blog.csdn.net/zhonghuan1992 钟桓 2014年8月31日 浅谈python字符串存储形式 记录一下自己今的天发现疑问而且给出自己现有知识有的回答. 长话短说,用 ...

  4. js如何实现动态点击改变单元格颜色?

    js如何实现动态点击改变单元格颜色? 一.总结 1.通过table的rows属性,遍历表格所有行,然后通过cells属性,遍历每一行中的单元格. 2.遍历的过程中,动态的为每一个单元格定义单击事件,改 ...

  5. 剑指Offer面试题10(Java版):二进制中的1的个数

    题目:请实现一个函数,输入一个整数.输出该数二进制表示中1的个数. 比如把9表示成二进制是1001,有2位是1.因此假设输入9.该函数输出2. 1.可能引起死循环的解法 这是一道非常主要的考察二进制和 ...

  6. KMP练习——KMP模式匹配 一(串)

    Description 求子串的next值,用next数组存放,所有输出 Input 输入一个字符串 Output 输出全部next值 Sample Input abaabcac Sample Out ...

  7. Android有用的任务管理器—tractor

    在平时的android开发工作中,我们常常须要运行耗时操作,有时为了用户体验还须要显示个等待框,我之前的做法都是开一个线程,然后用handler发消息进行显示和关闭等待框以及相关的ui操作.假设任务比 ...

  8. 并发编程--CAS自旋锁

    在前两篇博客中我们介绍了并发编程--volatile应用与原理和并发编程--synchronized的实现原理(二),接下来我们介绍一下CAS自旋锁相关的知识. 一.自旋锁提出的背景 由于在多处理器系 ...

  9. UUID不失精度,长度改进

    在使用到uuid的时候,往往头疼于它的长度(如1bfe50d8-544e-4e8a-95b8-199ceff15268),于是乎就有了改写uuid的各种方法 1.去除"-"的uui ...

  10. mysql常用控制台命令

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1.登陆mysql,语法为:mysql -u[用户名] -p[密码],示例:mysql -uroot -p123456 ...