使用Rust Warp反向代理时移除指定请求Header的实现问询
使用Warp + warp-reverse-proxy反向代理时解决Header拒绝问题
我在Rust中用Warp框架配合warp-reverse-proxy库做反向代理,请求链路为user -> proxy.com -> example.com,但目标服务器example.com一直拒绝转发过来的请求,排查后发现是两个Header的问题:
- Host Header携带了原请求的域名(proxy.com),不符合example.com的预期
- Content-Length Header值不符合目标服务器要求
我尝试过直接移除这两个Header,但始终没能正确配置Warp的Filter,各种组合都没成功。最初的代码如下:
warp::serve( warp::get() .and(warp::path("graphiql")) .and(juniper_warp::graphiql_filter("/graphql", None)) .or(warp::path("graphql").and(graphql_filter)) // .or(reverse_proxy_filter( // "".to_string(), // "https://example.com/".to_string(), // )) .with(log), ) .run(([127, 0, 0, 1], 8080)) .await
后来通过新增自定义Filter,先移除问题Header再转发请求,成功解决了问题。修改后的关键代码片段:
.or(warp::any().and(request_filter).and_then( |path, query, method, mut headers: Headers, body: Body| { headers.remove("Host"); headers.remove("Content-length"); proxy_to_and_forward_response( "https://example.com".to_string(), "".to_string(), path, query, method, headers, body, ) }, ))
内容的提问来源于stack exchange,提问作者Ainestal
相关产品推荐
相关产品推荐

