CloudFront如何在S3的现有分发服务网站上设置反向代理 [英] CloudFront how to setup reverse proxy on an existing distribution serving website from S3

查看:213
本文介绍了CloudFront如何在S3的现有分发服务网站上设置反向代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个S3存储桶,用于托管网站并随CloudFront一起交付

I have a S3 bucket which hosts a website and is delivered with CloudFront

现在,我已将发行版附加到我的根目录根域,例如-www.xyz.com

and right now I have attached the distribution to my apex root domain like - www.xyz.com

因此,以前我们使用Nginx从同一域(www.xyz.com)上的Web服务器根提供静态前端,并且还设置了反向代理(www.xyz.com/api/**)来路由流量到同一台计算机上的上游后端服务器.

So, previously we were using Nginx to serve a static frontend from a webserver root on the same domain - www.xyz.com and had also setup a reverse proxy - www.xyz.com/api/** which routed traffic to upstream backend server on the same machine.

现在,我想将网站移至S3,但仍要在同一台计算机上运行后端API,为此,我必须更改DNS记录并将其指向CloudFront发行版.

Now, I would like to move the website to S3 but still run the backend API on the same machine and to do so I will have to change my DNS records and point them to the CloudFront distribution.

但是,使用www.xyz.com/api作为后端服务的现有和以前部署并正在运行的服务将中断 因此,我想将此路径模式上的所有请求转发到http:///api,以使现有应用程序不会中断.

But, then the existing and previously deployed and running services which use www.xyz.com/api for backend services will break So, I want to forward all request on this path pattern to http:///api so that the existing applications don't break.

有没有办法可以做到这一点?即-

Is there a way we can achieve this ? i.e -

来自CloudFront分发的子路径的转发请求 从S3向外部应用程序服务器提供静态前端?

Forward request from a subpath of CloudFront distribution delivering a static frontend from S3 to an external application server ?

-更新- --- Nginx conf重定向请求---

---UPDATE--- ---Nginx conf to redirect requests---

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

这是在服务器指令中,该服务器指令公开了根域&当前的前端,但现在我想将前端迁移到S3,因此仅出于兼容性目的保留此位置块/api,直到我在所有客户端上更新配置为止.

this is within a server directive which exposes the root domain & frontend to the world currently but now I want to migrate the frontend to S3 and thus only keep this location block /api for compatibility purposes until I update the configuration on all clients.

如果是这样,请建议如何完成此工作,或者从我这方面您需要什么信息来帮助完成此工作?

If so, please suggest how this can be done or what information you need from my side that could help out in getting this done ?

谢谢

推荐答案

在DNS中创建api.example.com,指向您的API.

Create api.example.com in DNS, pointing to your API.

在CloudFront中创建第二个Origin,指向api.example.com.将原始路径"留为空白,因为它没有达到您的预期.

Create a second Origin in CloudFront, pointing to api.example.com. Leave "Origin Path" blank, because it does not do what you might assume.

在CloudFront中使用/api*的路径模式创建新的缓存行为.将此指向新创建的原点.

Create a new Cache Behavior in CloudFront, with the Path Pattern of /api*. Point this to the newly-created origin.

CloudFront会将对/api*的所有请求发送到api.example.com,并将其他所有请求发送到默认的缓存行为来源"(即存储桶).

CloudFront will send all requests for /api* to api.example.com and everything else to the default Cache Behavior Origin, which would be the bucket.

这篇关于CloudFront如何在S3的现有分发服务网站上设置反向代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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