子请求不被发送或请求挂起 [英] Subrequests are not sent or the request hangs

查看:168
本文介绍了子请求不被发送或请求挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写我自己的模块中的C(它必须是C)为Nginx的1.7.12。
它的目的是发送额外的请求到另一个服务器,代理请求时到达Nginx的。

有关这个我在Config中创建两个位置中:

 位置= / {
                 proxy_pass $ backend_url;
        }
        位置/ internal_url {
                 proxy_pass $ log_server_url;
                 内部;
        }

我用从例子这里创建过滤器$ p $以下psented:

 静态ngx_int_t my_http_body_filter(ngx_http_request_t * R,
                                     ngx_chain_t *中)
{
  INT RC;
  ngx_str_t URI = SOME_INTERNAL_URI;
  ngx_http_request_t * SR;  RC = ngx_http_next_body_filter(R,在);  如果(RC == NGX_ERROR)
    返回RC;  返回ngx_http_subrequest(R,&安培; URI,NULL / * * ARGS /,&安培; SR,
                           NULL / * *回调/ 0 / * *标志/);
}

起初,好像子请求不会发送。我曾与 NGX_AGAIN 试验和 subrequest-&GT;完成但我可以只发出请求挂<。 / p>

然后,事实证明,子请求,事实上,发送(这样的配置是OK)当我添加返回NGX_AGAIN; ,在 ngx_http_subrequest 电话。

不幸的是,那么原来的请求无法完成。

这个应该怎么做呢?


解决方案

可能这会不会起作用?

 静态ngx_int_t my_http_body_filter(ngx_http_request_t * R,
                                     ngx_chain_t *中)
{
  INT RC;
  ngx_str_t URI = SOME_INTERNAL_URI;
  ngx_http_request_t * SR;  如果相关(r = R-&GT;!主){/ *子请求* /
    返回ngx_http_next_body_filter(R,在);
  }  返回ngx_http_subrequest(R,&安培; URI,NULL / * * ARGS /,&安培; SR,
                           NULL / * *回调/ 0 / * *标志/);
}

更新:

经过一番调查,这是一个新的版本:
要点

不幸的是我没有机会的时刻来测试我的code。

更新

以上code发送子请求在收到完整的身体。
另一种可能的方法是发送它在上下文中第一个块和存储标志。

I'm trying to write my own module in C (it has to be C) for Nginx 1.7.12. It is meant to send additional request to another server, when proxied request arrives to Nginx.

For this I've created two locations in config:

        location = / {
                 proxy_pass                 $backend_url;
        }
        location /internal_url {
                 proxy_pass                 $log_server_url;
                 internal;
        }

I've used examples from here to create a filter presented below:

static ngx_int_t my_http_body_filter(ngx_http_request_t* r,    
                                     ngx_chain_t* in)
{
  int                 rc;
  ngx_str_t           uri = SOME_INTERNAL_URI;
  ngx_http_request_t *sr;

  rc = ngx_http_next_body_filter(r, in);

  if (rc == NGX_ERROR)
    return rc;

  return ngx_http_subrequest(r, &uri, NULL /* args */, &sr,
                           NULL /* callback */, 0 /* flags */);
}

At first, it seemed as if subrequests are never sent. I had experimented with NGX_AGAIN, and subrequest->done but I was able only to make the request hang.

Then, it turned out that subrequests are, in fact, sent (so the configuration is OK) when I add return NGX_AGAIN;, after ngx_http_subrequest call.

Unfortunately, then the original request never finishes.

How this should be done?

解决方案

May be this will work?

static ngx_int_t my_http_body_filter(ngx_http_request_t* r,
                                     ngx_chain_t* in)
{
  int                 rc;
  ngx_str_t           uri = SOME_INTERNAL_URI;
  ngx_http_request_t *sr;

  if (r != r->main) { /* subrequest */
    return ngx_http_next_body_filter(r, in);
  }

  return ngx_http_subrequest(r, &uri, NULL /* args */, &sr,
                           NULL /* callback */, 0 /* flags */);
}

Update:

After some investigation this is a new version: gist

Unfortunately I don't have a chance to test my code at the moment.

Update

Code above sends subrequest upon receiving full body. Another possible approach is to send it on first chunk and store flag in context.

这篇关于子请求不被发送或请求挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆