title: Unable to serve static file using cloud front with the origin server as ec2 instance
body: <p>I have a web application running on centos ec2 instance behind Nginx reverse proxy with SSL certification (Let's Encrypt).</p>
  
  <p>I have a javascript file located at the URL for example <code>https://example.com/static/src/js/allEnd.js</code><br>
  I used CloudFront to delivered the static file with the origin server as the HTTP ec2 instance (not using the s3 bucket.)<br>
  My origin server is mapped with the domain name <code>https://example.com</code> I have the following configuration I have made so far:<br>
  1. www.example.com is redirected to example.com in Nginx<br>
  2. The CloudFront URL is an alias with my custom domain ie cdn.example.com<br>
  3. SSL for example.com is done on Nginx whereas SSL for cdn,example.com is done on AWS.</p>
  
  <p>What I have understood so far is the first time the CloudFront will serve the static content by getting the file from my ec2 server and then the next time it will serve from CloudFront but every time the CloudFront redirect to the origin server to get the static file which CloudFront is not serving in my case.  </p>
  
  <p>Here is the header for both the origin server and CloudFront server. </p>
  
  <pre><code>1. Origin server (https://example.com)
  get https://example.com/static/src/js/allEnd.js
  HTTP/2 200
  server: nginx/1.12.2
  date: Sun, 12 May 2019 12:27:50 GMT
  content-type: application/javascript
  content-length: 168435
  etag: "wzsdm-1557567525-168435-283837276"
  cache-control: max-age=604800, public
  expires: Sun, 19 May 2019 12:27:50 GMT
  strict-transport-security: max-age=15768000; includeSubdomains; preload
  x-frame-options: SAMEORIGIN
  x-content-type-options: nosniff
  strict-transport-security: max-age=15768000
  
  2. CloudFront with origin as https://example.com (https://cdn.example.com)
  get https://cdn.example.com/static/src/js/allEnd.js
  HTTP/2 301
  content-type: text/html
  content-length: 185
  location: https://example.com/static/src/js/allEnd.js
  server: nginx/1.12.2
  date: Sun, 12 May 2019 09:17:40 GMT
  strict-transport-security: max-age=15768000; includeSubdomains; preload
  x-frame-options: SAMEORIGIN
  x-content-type-options: nosniff
  strict-transport-security: max-age=15768000
  age: 17
  x-cache: Hit from cloudfront
  via: 1.1 76d9b50884e58e2463b175e34e790838.cloudfront.net (CloudFront)
  x-amz-cf-id: HBbfJXbFgKQz4eYlQSpLPQAk8pxZRKuGsb6YjFu8-AJL0JwPfs8FZw==
  </code></pre>
  
  <p>As you can see the response header that the cdn.example.com (CloudFront) redirect to the origin (example.com).
  Also, I am confused with the <code>content-type:text/html</code> which should be <code>content-type: application/javascript</code></p>
  
  <p>What are the possibilities that I may have misconfigured?  </p>
  
  <p>If anything more you want to know, Please feel free to ask. Thanks.  </p>
  
  <p>P.S: I am new to the Nginx and AWS configuration and most importantly cache control.</p>