Skip to content

Nginx reverse proxy with Cloudfront

0

I have a Cloudfront distribution pointing to S3 based website. I have Nginx setup on EC2 instance and wanted to reverse proxy example.com/app to my cloudfront distribution. I am using below on Nginx and while the main page loads but all assets failed to load with 404 resulting in blank page. Any help would be appreciated.

location /app/ {
   resolver 8.8.8.8 valid=30s;
   proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
   set $dd abc.cloudfront.net;
   proxy_ssl_server_name on;
    proxy_set_header Host $dd;
    proxy_hide_header x-amz-id-2;
    proxy_hide_header x-amz-request-id;
    proxy_set_header       Connection '';
    proxy_http_version     1.1;
    proxy_hide_header Set-Cookie;
    proxy_ignore_headers "Set-Cookie";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Access-Control-Allow-Credentials true;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_intercept_errors on;
    proxy_buffering        off;
    proxy_redirect off;
    proxy_pass https://$dd;
    add_header Cache-Control public;
    add_header Last-Modified "";
    add_header ETag "";
    gzip_static on;
    gzip_http_version 1.0;
    log_not_found off;
    proxy_set_header Content-Encoding gzip;
 }

asked 2 years ago1.7K views

3 Answers
0

Hello.

Is the target file located in the CloudFront origin?
For example, if you access it like "https://example.com/app/test.html", I think the S3 path needs to be like app/test.html.
https://repost.aws/knowledge-center/cloudfront-troubleshoot-404-errors

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • If the file is in the root folder of the S3 bucket, try adding "/" to the end of the "proxy_pass" URL.

    proxy_pass https://$dd/;
    
  • CloudFront passes the URL unchanged to your origin. Therefore, a URL like "/app/test.html" will behave like opening "/app/test.html" in the S3 bucket.

  • How do I rewrite the URL to match the origin settings?

0

I have all of the files like index.html in root of the bucket. Within the root - I have assets folder which has js files. Origin path is cloudfront distribution is set to empty.

answered 2 years ago

0

I even tried using API gateway and setting /app to my cloudfront URL but the same issue. All assets throw 404 error. Is there anything to do with cloudfront?

answered 2 years 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.