如何在Nginx中进行Web服务调用? [英] How do I make web service calls within nginx?

查看:290
本文介绍了如何在Nginx中进行Web服务调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对每一个通过nginx的请求都从nginx内向Web服务发出请求,并根据我从Web服务获得的响应来应用一些过程.

I want to make requests to a web service from within nginx on each request that goes through nginx and apply some process based on the response I get from the web service.

我正在使用nginx作为反向代理,并且有多个Web服务被路由到.我想添加一个充当管理员的附加Web服务(称为AdminService),该服务将处理安全性,计费以及其他流量分析和预处理之类的事情. 对于通过nginx的每个请求,我都需要向AdminService发出请求,然后admin服务将分析该请求以更新一些统计信息等,并使用一些标签进行响应.然后,nginx将根据返回的标签更新一些标头,并将请求转发到相应的url.

I am using nginx as a reverse proxy and have multiple webservices to which traffic is routed to. I want to add an additional webservice (Lets call this AdminService) that would act as admin, this service would handle things like security, billing and other traffic analytics and preprocessing. For every request the goes through nginx I need to make a request to AdminService, admin service will then analyse the request updating some statistics and the likes and respond with some tags. nginx will then update some headers based on the returned tags and forward the request to the appropriate url.

我看过Lua模块,它似乎并没有进行webservice调用. 我还看到有Java,Groovy和Clojure模块可用,这也许是我应该关注的内容吗?否则我应该看什么?

I've taken a look at the Lua module and it doesn't seem to do webservice call. I also see that there are Java, Groovy and Clojure modules available, is this perhaps what I should be looking at? Otherwise what should I be looking at?

推荐答案

一种选择是使用

One option is to use the auth_request module. It's not designed with your scenario in mind and is not a default Nginx module so you need to build from source to compile it in with ./configure --with-http_auth_request_module.

auth_request用于通过远程HTTP调用对Nginx请求进行预身份验证.只要响应头是HTTP 200,初始请求就照常处理.这可以用于将请求发送到您的AdminService,然后响应可以确定接下来发生的情况.

auth_request is used to pre-authenticate Nginx requests via an remote HTTP call. As long as the response header is HTTP 200, then the initial request is processed as normal. This could be used to send the request to your AdminService and the response would be able to determine what happened next.

类似:

# Default location
location / {
    auth_request /AdminService;

    # Look for X_UpstreamHost: header in the response
    auth_request_set $x_upstreamhost $upstream_http_x_upstreamhost;

    # Use the value of the response header to choose the internal processing host
    proxy_pass http://$x_upstreamhost;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;

}

# Send requests for AdminService to the AdminService
# This expects AdminService to be listening on a path called AdminService
# and based at ##adminip##
location /AdminService {
     proxy_pass http://##adminip##;
}

这将首先将传入请求发送到AdminService定义的主机.该服务必须使用普通的200标头和x_upstreamhost进行响应:#internalHost#.其中#internalHost#是您要处理请求的主机的ip或dn.

This will send incoming requests first to the host defined by AdminService. This service must response with a normal 200 header and also x_upstreamhost: #internalHost#. Where #internalHost# is the ip or dn of the host you want to handle the request.

尝试一下,如果遇到问题,请发布服务器{}块,然后有人来看看.

Try it out and if you run into issues, post your server {} block and somebody will take a look.

这篇关于如何在Nginx中进行Web服务调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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