Skip to content

WordPress and CloudFront

0

I’m having issues with policies that need to be set in place with CloudFront. I was able to access WordPress without any problem and do anything that was required on the site, but now that site is behind ClodFront and I try to edit/create a page in WordPress this doesn’t work anymore (getting errors on the console when inspecting).

I’ve set the origin server which is up and running (same server where site was running before CloudFront was set in place). I’ve added the below behavior patterns and this something that I may need input with so this works with no problem:

0 /wp-admin* Redirect HTTP to HTTPS Managed-CachingDisabled (Cache policy name) Managed-AllViewer (Origin request policy name)

1 /* Redirect HTTP to HTTPS Managed-CachingDisabled Managed-AllViewer

2 Default (*) Redirect HTTP to HTTPS CachePolicy-With-Host-Header Managed-AllViewer

I’m sure this has been done before (enabling CloudFront for a WordPress site and be able to edit anything on the site with no problem). Any custom policy that needs to be created? I’ve created one custom policy that includes the “Host” headers (CachePolicy-With-Host-Header) and is part of the default behavior pattern.

I’ll appreciate the input so I can get this working – thanks!

asked a year ago987 views

2 Answers
0

When putting WordPress behind CloudFront, you're encountering common issues related to the WordPress admin functionality. The problems you're seeing in the console are likely related to how CloudFront is handling requests to your WordPress admin area.

Your current setup has the right general approach with separate behaviors for wp-admin, but there are a few adjustments needed:

  1. For /wp-admin behavior*:
  • Using Managed-CachingDisabled is correct since you don't want to cache admin content
  • Using Managed-AllViewer is appropriate to forward all headers, cookies, and query strings
  1. For the / behavior*:
  • This is currently set to no caching (Managed-CachingDisabled), which negates much of CloudFront's benefit
  • This should be changed to a caching policy that allows caching but respects WordPress's dynamic content needs
  1. For Default (*) behavior:
  • Your custom CachePolicy-With-Host-Header is a good start, but may need additional headers

Here are the recommended changes:

  1. For wp-admin behavior: Keep as is, but ensure it has the highest precedence (lowest number)

  2. For dynamic WordPress content:

  • Create a behavior for patterns like /wp-json/ and /wp-login.php with Managed-CachingDisabled
  • These endpoints need to bypass caching completely
  1. For static assets:
  • Create behaviors for /wp-content/uploads/, */wp-includes/, and /wp-content/themes/ with a caching policy that enables longer caching times
  • These are static files that can be safely cached
  1. For the default behavior:
  • Use a cache policy that forwards cookies that WordPress needs (like wordpress_, wp-settings-, etc.)
  • Ensure the Host header is forwarded (as you've done with your custom policy)
  • Forward query strings that WordPress uses
  1. Additional settings to check:
  • Ensure your origin response timeout is sufficient (WordPress admin can be slow)
  • Check that your CloudFront distribution allows all HTTP methods needed (GET, POST, PUT, DELETE, etc.)
  • Verify CORS settings if you're seeing related errors in the console

The most common issue with WordPress admin behind CloudFront is related to cookies and headers not being properly forwarded to the origin. Your Managed-AllViewer policy should handle this for the admin area, but you may need to create a more specific policy for the rest of the site that forwards only WordPress-specific cookies rather than all viewer cookies.
Sources
Backend and Frontend on same domain Cloudfront error | AWS re:Post
CloudFront 502 Error - Previously working setup with a Google Cloud Run origin suddenly starts failing, experienced across 2 separate google accounts and servers, the common denominator is CloudFront. | AWS re:Post
Caching web page images when origin is set to alb for Cloudfront | AWS re:Post

answered a year ago

0

I try to edit/create a page in WordPress this doesn’t work anymore (getting errors on the console when inspecting).

Do you have a valid HTTPS cert installed on your EC2 instance, and have configured HTTPS Only in your Origin Protocol Policy?

If yes, login to WordPress and change your Site Address (URL) and WordPress Address (URL) to your CloudFront URL.

AWS
EXPERT

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.