【问题标题】:Using Hapi h2o2 with nginx ('worker_connections are not enough' error)将 Hapi h2o2 与 nginx 一起使用('worker_connections are not enough' 错误)
【发布时间】:2016-05-02 16:55:29
【问题描述】:

我有一个使用 Hapi 开发的 API 服务器,它将特定端点上的 POST 请求代理到处理长时间运行的异步作业的不同服务器(也是用 Hapi 编写的)。
对:
http://my-api-staging.northeurope.cloudapp.azure.com/api/v1/importer/items
的请求被代理到:
http://my-services-staging.northeurope.cloudapp.azure.com/api/v1/importer/items
保留原始请求的所有 HTTP 标头。

接收代理请求的服务器也是用 Hapi 编写的,用于处理长时间运行的作业。
在 API 服务器上,Hapi h2o2 插件处理请求代理。代理路由的代码如下所示:

{
    method: 'POST',
    path: '/importer/items',
    config: {
      payload: {
        parse: false
      },
      handler: {
          proxy: {
            passThrough: true,
            xforward: true,
            host: SERVICES_HOST,
            port: SERVICES_PORT,
            protocol: SERVICES_PROTOCOL,
            onResponse: function(err, res, request, reply, settings, ttl) {
              var response = (err) ? err : res;

              return reply(response);
            }
          }
        }
     }
}

当我在localhost 上运行两个服务器时(将 API 服务器和作业服务器绑定在不同的端口上),一切正常。

我已经在不同的 Microsoft Azure 虚拟机上设置了两台服务器,将它们绑定在端口 3000 上,前面的 nginx 充当反向代理。
我的 nginx 配置如下所示(两台服务器上相同,只是 server_name 更改):

server {
   listen 80;
   server_name my-api-staging.northeurope.cloudapp.azure.com;
   index index.php index.html index.htm;


   location / {
    proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
   }
}

请求由 nginx 正确处理,其他路由不涉及在不同的远程计算机上代理请求。
当我通过 Postman 发送请求或在涉及远程代理的某个端点上的 VM 上使用 curl 时,问题就开始了:

curl -XPOST -H 'Bearer: foobar' 
            -H 'Authorization: token somerandomstring' 
            -H "Content-type: application/json"      
     'http://127.0.0.1:3000/api/v1/importer/items'

nginx 挂起几秒钟,发回 500 内部服务器错误。
尾随/var/log/nginx/error.log:

worker_connections are not enough while connecting to upstream, 
client: 127.0.0.1, 
server: my-api-staging.northeurope.cloudapp.azure.com, 
request: "POST /api/v1/importer/items HTTP/1.1", 
upstream: "http://127.0.0.1:3000/api/v1/importer/items", 
host: "my-api-staging.northeurope.cloudapp.azure.com"

设置 worker_connections 20000; 不能解决问题,产生 400 Bad Request。 尾随/var/log/nginx/error.log

accept4() failed (24: Too many open files)

我猜有某种循环在发生,nginx 无法使用我提供的配置正确处理多个重定向。

我希望能够处理这种请求流:

(Request) -> nginx:80 -> API server:3000 -> nginx:80 -> Jobs server:3000
                                        h2o2

有可能吗?

【问题讨论】:

    标签: node.js nginx proxy reverse-proxy hapijs


    【解决方案1】:

    使用reply.proxy(options) API 显然解决了开箱即用的问题,没有对 nginx 配置进行任何更改。
    我还删除了xforward 选项,当不存在时默认为false

    {
        method: 'POST',
        path: '/importer/items',
        config: {
          payload: {
            parse: false
          },
          handler: function(request, reply) {
          return reply.proxy({
            host: SERVICES_HOST,
            port: SERVICES_PORT,
            protocol: SERVICES_PROTOCOL,
            passThrough: true,
            onResponse: function(err, res, request, reply, settings, ttl) {
              var response = (err) ? err : res;
    
              return reply(response);
            }
          });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2016-07-09
      • 2016-02-16
      • 2023-01-27
      • 2020-06-19
      相关资源
      最近更新 更多