CloudFront how to setup reverse proxy on an existing distribution serving website from S3

家住魔仙堡 提交于 2020-02-28 06:27:56

问题


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

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

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.

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.

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 -

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

---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;
        }

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 ?

Thanks,


回答1:


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

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

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

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.




回答2:


I don't sure if this would work, but this is the first thing that came to my mind.

  1. Register additional domain api.xyz.com and point it to your old machine.

  2. Use following nginx server configuration block:

    server {
        server_name api.xyz.com;
        ...
        location / {
            proxy_pass http://localhost:4040;
            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;
        }
    }
    
  3. Redirect all requests from www.xyz.com/api/... to api.xyz.com/... with the following Amazon S3 redirection rules:

    <RoutingRules>
      <RoutingRule>
        <Condition>
          <KeyPrefixEquals>api/</KeyPrefixEquals>
        </Condition>
        <Redirect>
          <HostName>api.xyz.com</HostName>
          <ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
        </Redirect>
      </RoutingRule>
    </RoutingRules>
    

You can also take a look at AWS Lambda Edge. I am not familiar with it so cannot tell if it can be used for this case.



来源:https://stackoverflow.com/questions/53966672/cloudfront-how-to-setup-reverse-proxy-on-an-existing-distribution-serving-websit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!