user606521
user606521

Reputation: 15434

Can I reverse-proxy POST requests while using CloudFront CDN for GET requests?

Currently I have CloudFront distribution that serves SPA (spa.myapp.com) from S3 bucket. I have also web server deployed in k8s on AWS.

  1. Is it possible to forward all POST spa.myapp.com/* requests to my web server instead? What components / configuration I need to do this?

  2. Is it possible to forward all spa.myapp.com/api/* requests to my web server instead (regardless of http method)? What components / configuration I need to do this?

Generally I want to serve SPA from CDN but also have few endpoints server side on same domain.

Upvotes: 0

Views: 855

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

  1. On your CloudFront Behavior for * path you should allow all kind of methods:

enter image description here

From the docs:

CloudFront always caches responses to GET and HEAD requests. You can also configure CloudFront to cache responses to OPTIONS requests. CloudFront does not cache responses to requests that use the other methods.

This means that POST requests are always redirected to the origin (back-end), responses to these requests wont be cached. GET and HEAD requests may also be redirected, if there is a cache miss. Otherwise, the data from the cache is returned.

  1. When you crate a CloudFront distribution, you have to set up a behavior be default. Usually, you leave the default path pattern as it is, so *:

enter image description here

You can have multiple behaviors, which can have different properties for caching. These behaviors are differentiated by their path pattern. They can also have a priority, the incoming requests are evaluated against higher precedence behaviors first. For example:

enter image description here

To answer your question, what you have to do is to create a different behavior for the /api/* path and apply a different configuration for this behavior. This has to have a higher precedence compared to the default behavior. Keep in mind that CloudFront caches GET, HEAD and OPTIONS requests only.

Upvotes: 2

Related Questions