user168983
user168983

Reputation: 844

Serve static files from cloudfront CDN with nginx

I have set a CDN with amazon cloudfront for my website. I now have files on my cdn with the same location of folders in my website.

I tried using a plugin to setup cdn for my website but it didn't work as expected.

So now I am trying to setup nginx to server the files from my CDN(an external URL). I have seen several posts for this but none of them worked for me.

this is what i have done so far

location ~* \.(gif|png|jpg|svg|woff|js|css)$ {

             rewrite ^ https://mycdnurl.net$request_uri permanent;
             proxy_pass https://mycdnurl.net;

       }

both didn't work.

To simply help

https://mywesite.com/images/abcd.jpg should become https://mycdnurl.net/images/abcd.jpg

and the path for the file can be anything and is not constant.

any help is greatly appreciated.Thanks in advance

Upvotes: 1

Views: 3104

Answers (1)

Richard Smith
Richard Smith

Reputation: 49792

I don't understand the second block. The first block contains two conflicting directives. If you want to redirect to your CDN, you should use:

location ~* \.(gif|png|jpg|svg|woff|js|css)$ {
    return 301 https://mycdnurl.net$request_uri;
}

Which is just a simplified version of your rewrite ^ https://mycdnurl.net$request_uri permanent; statement. See this document for more.

If that does not work, you need to update your question with more details of the problem.

Thanks to Michael for pointing out that the above scheme will only work if CloudFront is not using this server and the same URI to obtain its authoritative copy.

Upvotes: 2

Related Questions