0

I have an app which serves a dynamic html and some static files written in NodeJS/Express.

I deployed an AWS CloudFront distro in front of it, however only the HTML goes through, all the static files result in 404. The headers of the requests look like:

Age:116 Connection:keep-alive Content-Length:170 Content-Security-Policy:default-src 'self' Content-Type:text/html; charset=utf-8 Date:Mon, 09 Oct 2017 14:37:23 GMT Server:nginx/1.6.2 Via:1.1 523db8f46d98334ac6b5debbf315e15b.cloudfront.net (CloudFront), 1.1 proxy1.xxx.yy (squid/4.0.17) X-Amz-Cf-Id:5UYpluGn8TxUxsxmmDYYiZnjbOWbZ7iFFit55mmgcN6IbAJHCEAX6Q== X-Cache:MISS from proxy1.xxx.yy X-Cache:Error from cloudfront X-Cache-Lookup:MISS from proxy1.xxx.yy:3128 X-Content-Type-Options:nosniff X-Powered-By:Express

For info, my nodejs app runs in some port, and nginx reverse-proxies it to the domain I specified using proxy_pass.

As you can see I'm behind another proxy, but this cannot be the problem.

What I believe is happening is that my origin looks like mydomain.com/path/app_id and express serves static files from mydomain.com/.

Has anyone successfully deployed CloudFront in front of NodeJS/Express for static files? I really don't understand what the problem is..

Thanks!

Jo Colina
  • 1,870
  • 7
  • 28
  • 46
  • I tried adding both origins `mydomain/path/app_id` and `mydomain` but it does not make any difference – Jo Colina Oct 09 '17 at 14:59
  • 1
    Review your server's logs to see what's actually being requested. Also, set your [Error Caching Minimum TTL](https://stackoverflow.com/a/35541525/1695906) to 0 while testing. Otherwise CloudFront cached error responses for 5 minutes by default. As above, `Age: 116` is almost 2 minutes old, and if you solved the problem between now and then, you wouldn't see it and there would be no origin server log entry. – Michael - sqlbot Oct 10 '17 at 00:21

1 Answers1

0

To serve files from another path the flow is as follows:

  • Add all necessary origins (in this case mydomain.com/path/app_id AND mydomain.com (without trailing /))
  • Add behaviours on your ditribution for every file type. In this case static files are stored in different folders (like /img). So we can add behaviours for img/*, js/* and css/*. Then each behaviour can be set to a single origin. In this case we choose mydomain.com which was previously named Static files origin.
Jo Colina
  • 1,870
  • 7
  • 28
  • 46