子请求未发送或请求挂起

时间:2015-05-25 18:52:31

标签: c nginx

我试图在C中编写自己的模块(必须是C),用于Nginx 1.7.12。 当代理请求到达Nginx时,它意味着向另一个服务器发送附加请求。

为此,我在config中创建了两个位置:

/Item/Details/12-FT-CHAIN-W-HOOK/cbb1eb65b100459283d15102606208c2/400
/Item/Details/12-INCH-FUSION-SUBWOOFER/534c4d677b2547fb814668b7d061df5d/400
/Item/Details/18-Gold-Chain-14K-Yellow-Gold-2-03g/0aaf2e1e5532461884cb44e786329e80/400
/Item/Details/1820-HANDMADE-STRAIGHT-RAZOR/ed0ba44f98224067b595b726bf01f5ab/400
/Item/Details/2-PAIRS-OF-POCKET-PLIERS-LEATHERMAN/410bcb9e4321426487bee7639b3cb96e/400
/Item/Details/20TH-CENTURY-FOX-Motorcycle-Helmet-RACING-HELMET/e12a75dc7e004e5aa43698c1edf87773/400
/Item/Details/30-CLUBS/a65f1cbff00c4d59ac998dee96eed98b/400
/Item/Details/30-STEEL-CHAINSAW-BLADE/daaca24ede1341c58bb0d0cd32051646/400
/Item/Details/5-GALLON-GLASS-JUG-BREWING-JUG-CHANGE-JAR/dde9b1bfea2a4a23ad93da098ffc674d/400
/Item/Details/5150-SNOWBOARDS-Snowboard-5150-155CM/bcaa07c71c8c4b499a70d34459244f75/400
/Item/Details/6-FT-STEEL-CHAIN/7c24fb1a16ac46e7b9e91f99883652f6/400
/Item/Details/6-5-CUSTOM-HUNTING-KNIFE/ffda1685b2324abe96e3fb7cb6f7f265/400
/Item/Details/95150/39cb080edd474eb6b770b26b40e3dc6b/400
/Item/Details/ACER-Monitor-P201W/ff03d9c33ca747e08e4646d2c3d5143e/400
/Item/Details/ACOUSTIC-RESEARCH-Monitor-Speakers-RESEARCH-AW825/856ff1d8beb9480d893f94d9d49a8642/400
/Item/Details/ACTIVISION-Microsoft-XBOX-360-CALL-OF-DUTY-BLACK-OPS-2-XBOX-360/aef62055b4f14e379f2eea154d162551/400
/Item/Details/ACTIVISION-Video-Game-Accessory-DJ-HERO-95837809/41e3c7f0114e497caf23d8a50fe1f547/400
/Item/Details/ACTIVISION-Video-Game-Accessory-WII-FIT/7daee2a759a54dd7a4e2b6acd37b9c3e/400
/Item/Details/AIMTECH-1911-SCOPE-MOUNT/ac69ae1c40fe4d7db8c53a8ebf842d7d/400
/Item/Details/AIRCO-TIG-WELDING-TUNGSTEN-Arc-Welder-ELECTRODE/70b9b35db0c547c29eb90e02ef60d91a/400
/Item/Details/AIWA-Portable-CD-Player-XP-SP911/75761bfff9a44093be51e4d70410bd85/400
/Item/Details/ALESSI-Gent-s-Wristwatch-KARIM-RASHID/251c3f95173f49078722b301e1d920fe/400
/Item/Details/ALL-AMERICAN-RIDER-Motorcycle-Part-SADDLE-BAGS/87634c0c08d2458ba5b84fa39c9bc3fc/400
/Item/Details/ALL-AMERICAN-RIDER-Motorcycle-Part-SADDLE-BAGS/803f6dfdc9f44326a5a52b63681779ad/400
/Item/Details/ALLY-SKATEBAORD-USED/716cec1588d9408e859718f5961e1ec6/400
/Item/Details/ALPINE-ARCHERY-Bow-FRONTIER/e73dda8034cf4cdb8ebeeebc9683b55d/400
/Item/Details/AMAZON-Tablet-KINDLE-D01100/ea9ac5b291ef487ea6f75ca328e05750/400
/Item/Details/AMAZON-Tablet-KINDLE-FIRE-D01400/ebe0e7001ac744ffa030fd153942a548/400
/Item/Details/APPLE-Computer-Accessories-A1023/6a38f60d2e034dc597043cc42282246e/400
/Item/Details/APPLE-Cell-Phone-Smart-Phone-IPHONE-5C-A1532-AT-T/cc65c513e848475c8000b6e10b6855e5/400
/Item/Details/APPLE-IPOD-IPOD-A1199-2GB/d1003297dbe7443c8953750f0c96c62a/400
...................................................

我已使用here中的示例创建了一个过滤器,如下所示:

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

起初,好像从未发送过子请求。我曾尝试使用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 */); } NGX_AGAIN,但我只能将请求挂起。

然后,事实证明,在subrequest->done调用之后添加return NGX_AGAIN;时,子请求实际上已发送(因此配置正常)。

不幸的是,原始请求永远不会完成。

应如何做到这一点?

1 个答案:

答案 0 :(得分:2)

这可行吗?

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 */);
}

<强>更新

经过一番调查后,这是一个新版本: gist

不幸的是,我目前没有机会测试我的代码。

<强>更新

以上代码收到全身后发送子请求。 另一种可能的方法是在第一个块上发送它并在上下文中存储标志。