简体   繁体   English

关于nginx模块的输出问题?如何解决?

[英]An output problem about nginx module?How to fix it?

I'm new to nginx and I'm trying to develope a simple nginx module,a handler module specifically. 我是nginx的新手,我正在尝试开发一个简单的nginx模块,特别是处理程序模块。 Although it's not what I really wanna do,I try to finish this task first. 尽管这不是我真正想做的,但我还是先尝试完成此任务。 I'm trying to get the socketfd when a browser(or a client) connects to nginx.And I have get it successfully.However, when I tried to output something using dup2(),the nginx is always pending and just outputs nothing.Sometimes I can get output after a long time and once I stop nginx like nginx -s stop,and the output appears immediately. 当浏览器(或客户端)连接到nginx时,我试图获取socketfd。并且我已经成功获取它。但是,当我尝试使用dup2()输出某些内容时,nginx始终处于挂起状态,什么也不输出。有时我会在很长一段时间后获得输出,并且一旦停止nginx,如nginx -s stop,输出就会立即出现。

Like this: reach http://100.100.60.199/nc?search=123456 get search=123456 HTTP/1.l HOST 像这样:到达http://100.100.60.199/nc?search=123456得到search = 123456 HTTP / 1.l HOST

output 输出

I have read some blogs about nginx module and I found that handler module has its own pattern (to my understanding?).For example, the output should be nginx_chain_t,and I should construct that chain instead of using dup2 like a regular c code.So I wonder if it's feasible to get output like the function below. 我读过一些有关nginx模块的博客,发现处理程序模块有自己的模式(据我所知?)。例如,输出应为nginx_chain_t,我应该构造该链而不是像常规C代码那样使用dup2。所以我想知道像下面的函数那样获取输出是否可行。

Here is my handler function: 这是我的处理函数:

static ngx_int_t ngx_http_nc_handler(ngx_http_request_t *r){
    //ngx_int_t rc;
    ngx_socket_t connfd = r->connection->fd;
    int nZero=0;
    //if(setsockopt(connfd,SOL_SOCKET,SO_SNDBUF,(const void*)&nZero,sizeof(nZero))==0)
    if(setsockopt(connfd,IPPROTO_TCP,TCP_NODELAY,(const void*)&nZero,sizeof(int))==0){
        setbuf(stdout,NULL); 
        setbuf(stdin,NULL);
        setbuf(stderr,NULL);
        dup2(connfd,STDOUT_FILENO); 
        dup2(connfd,STDERR_FILENO);
        dup2(connfd,STDIN_FILENO);
        printf("%s\n", r->args.data);
        //close(connfd);
    }
    return NGX_OK;
}

So I wonder if it's feasible,how can I get things right using the method above or can anybody just say it's impossible and construct a chain is the only way? 因此,我想知道这是否可行,如何使用上述方法使事情正确,或者有人可以说这是不可能的,而构建链是唯一的方法?

我终于通过尝试理解nginx的工作原理解决了这个问题。总之,我要做的就是在输出中添加http标头。但这并不像我所描述的那么容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM