在Nginx上设置proxy_pass以对API网关进行API调用 [英] Setting up proxy_pass on nginx to make API calls to API Gateway

查看:528
本文介绍了在Nginx上设置proxy_pass以对API网关进行API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我已经在API网关后面设置了Lambda函数,该函数运行良好.我有一个托管站点,我只希望某个位置可以访问API.

示例

https://www.example.com/(从托管服务器提供HTML)

https://www.example.com/foobar (返回生成的JSON有效负载由Lambda并由AWS返回)

这是我的服务器块:

   location = /foobar {
            proxy_pass     https://someawsuri;
            proxy_redirect default;
    }

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

我已经尝试过查看文档并阅读了多个SO帖子,但是我却无法做我想做的事情.我尝试过的所有操作都产生了错误502 Bad Gateway.

问题:

如何配置nginx向API网关发出请求?

谢谢.

解决方案

我认为您需要这两行:

proxy_set_header Host "XXXXXX.execute-api.REGION.amazonaws.com";
proxy_ssl_server_name on;

以下是原因的解释:

这里

Amazon API Gateway终端节点.此值必须是区域下列出的与区域有关的端点之一和端点.在美国东部(弗吉尼亚北部)地区,它是apigateway.us-east-1.amazonaws.com.

proxy_ssl_server_name on;

在与代理HTTPS服务器建立连接时,允许通过TLS服务器名称指示扩展名(SNI)传递服务器名称.

Problem:

I've set up a Lambda function behind API gateway which works beautifully. I have a hosted site that I want only a certain location to hit the API.

Example

https://www.example.com/ (Serves up html from hosted server)

https://www.example.com/foobar (Returns a JSON payload that is generated by Lambda and returned by AWS)

Here is my server block:

   location = /foobar {
            proxy_pass     https://someawsuri;
            proxy_redirect default;
    }

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

I've tried reviewing the docs and read multiple SO posts, but I haven't been able to do what I want. Everything that I've tried has produced the error 502 Bad Gateway.

Question:

How do I configure nginx to make a request to API gateway?

Thanks.

解决方案

I think you need those two lines:

proxy_set_header Host "XXXXXX.execute-api.REGION.amazonaws.com";
proxy_ssl_server_name on;

Here is the explanation about the why:

The HOST header is required as is described here

The Amazon API Gateway endpoint. This value must be one of the region-dependent endpoints listed under Regions and Endpoints. In the US East (N. Virginia) region, it is apigateway.us-east-1.amazonaws.com.

The proxy_ssl_server_name on;

Enables passing of the server name through TLS Server Name Indication extension (SNI) when establishing a connection with the proxied HTTPS server.

这篇关于在Nginx上设置proxy_pass以对API网关进行API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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