我的 R plumber API 托管在 EC2 实例上的连接问题

Connectivity Issue with my R plumber API hosted on an EC2 Instance

我创建了一个 R plumber API,我在 AWS EC2 实例上托管,但是我公司的 Web 开发人员试图在我的 API 上执行 GET 请求时收到了一个连接错误。我不确定为什么。这是他们 运行 从他们的开发服务器尝试获取托管在我的 API 端点的数据:

curl -X GET http://12.345.678.90:8003/path-to-endpoint

这是他们收到的...

About to connect() to 12.345.678.90 port 8003 (#0)  
Trying 12.345.678.90...   
Connection refused      
Failed connect to 12.345.678.90:8003; Connection refused
Closing connection 0 

。 .

如果我只是在我的本地浏览器或任何浏览器中访问端点,那么它可以工作并显示数据,但由于某种原因它没有使用 curl 程序连接。另外,这些相同的 URL 端点正在公司的生产服务器上运行,而不是在他们的开发服务器上运行 - 所以我不确定我的 EC2 实例是否根本不允许访问开发服务器 IP。

如何解决

从 EC2 实例,我尝试制定自定义 TCP 规则以允许入站 来自任何地方的连接。我的实例具有以下入站和出站安全规则:

尽管如此,我公司的 Web 开发人员仍然无法连接以从我位于 http://12.345.678.90:8003/path-to-endpoint 的端点获取 API。有什么我没有做的事情允许他们访问 API?

如果能帮助解决这个问题,我们将不胜感激!!

Edit1:来自与我合作的开发人员的评论 - "If i use http URL from a local browser then it works and is showing the CSV, but for some reason it's not showing using curl program. We added 12.345.678.90 IP in our firewall also but still no luck."

Edit2:这与主题相关但与问题没有直接关系 - 我如何允许 public 访问/通过 https 访问我的端点(而不是 http)。即使在我自己的本地浏览器中,我也无法通过 https URL?

访问端点

一如既往的感谢!

我 运行 内部有许多管道工 API。

通常,这是防火墙问题。

你可以打开那些端口。但是每次你想提供新服务时,你都被迫回到网络,要求他们打开一个新端口。

但这正是反向代理的亮点所在。

我用nginx.

如果您将流量路由到端口 80 上的新端点,您可以拥有与端口一样多的端点(无需返回并请求更多防火墙)。

一个工作示例

以下示例在以下位置配置端点:

http://12.345.678.90/myplumberapi/path-to-endpoint

这 nginx.conf 在端口 80

上提供服务

(nginx高手请注意,此处可能存在一些低效之处,敬请见谅)

http {
        default_type application/json;
        sendfile   on;
        tcp_nopush on;
        server_names_hash_bucket_size 128;

        # note I only list one server here, but if you want
        # to serve your endpoints on more locations, change this
        upstream myplumberapi_pool {
                least_conn;
                server localhost:8003 weight=1;
        }


        map $http_upgrade $connection_upgrade {
                default upgrade;
                ''      close;
        }

        server {
                listen 80;
                listen  [::]:80 default_server;
                location /myplumberapi/ {
                            access_log /var/log/nginx/app.log;
                            rewrite ^/myplumberapi/(.*)$ / break;
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection $connection_upgrade;
                            proxy_set_header Host $host;
                            proxy_set_header X-Real-IP $remote_addr;
                            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                            proxy_pass http://myplumberapi_pool;
                            proxy_read_timeout 20d;
                            proxy_next_upstream error timeout http_500;
                    }

         }
}

events {
        worker_connections 4096;
}

您可以用同样的方式添加更多的API


对编辑的响应:

关于如何支持 https,你需要一个证书和一个域名(不能从 IP 做 https),但如果你有两者,那么可以使用 nginx 轻松演练。