1

Our web service is running on EC2 with Apache. We have the following setup:

Service A -> ELB -> Service B API 

In this case, Apache on Service B shows in rewrite.log that the rewrite rule was matched and responds with 200 OK.

However, when we access the same endpoint through CloudFront:

Service A -> CloudFront -> ELB -> Service B API 

Then Apache on Service B still shows in rewrite.log that the same rewrite rule was matched, but Apache responds with 404 Not Found, even though the corresponding PHP file exists

Does anyone know what could cause this behavior?

I suspect it might be related to headers or URL rewriting when requests come through CloudFront, but I’m not sure.

1 Answer 1

1

By default, CloudFront sets the Host header to the distribution’s domain name (e.g., d1234.cloudfront.net) rather than the origin’s (e.g., api.example.com).

If your Apache rewrite rules or PHP code depend on the Host value (for example using %{HTTP_HOST} or $SERVER['HTTP_HOST']), you may end up rewriting to a non-existent path.

Example:

RewriteRule ^api/(.*)$ /var/www/%{HTTP_HOST}/api/$1 [L]

If %{HTTP_HOST} = d1234.cloudfront.net, that path won’t exist.

Fix:

In CloudFront > Behavior > Origin Request Policy, enable “Include Host header” or manually forward the Host header from CloudFront to your origin (EC2).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.