Score:0

Redirect url to the CDN storage

bt flag

I have structure folders on my nginx server: Image URL

https://example.com/wp-content/uploads/image.jpg

https://example.com/wp-content/uploads/WP-data/data/employees/per-005/9032.jpg

I want it redirect to my CDN and get the picture on that CDN all time..

https://cdn.example.com/image.jpg

https://cdn.example.com/WP-data/data/employees/per-005/9032.jpg

I try to use with this setting but it doesn't work.

location ~ ^(/wp-content/themes|/wp-content/uploads)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3)$ {
             rewrite ^ http://cdn.example.com$request_uri?
             permanent;
             access_log off;
      }
us flag
You should replace the URLs in your content, not issue 301 redirects. You are adding one extra request to the flow with the 301 redirect, which decreases the benefit of having a CDN.
Score:0
sv flag

Welcome to ServerFault!

In order to remove /wp-content/uploads (and /wp-content/themes) and redirect the resulting URL to a CDN, the following code should work...

location ~ ^(/wp-content/themes|/wp-content/uploads)(?'short_url'/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3))$ {
     return 301 http://cdn.example.com$short_url;
     access_log off;
}

This uses "named capture" method while evaluating a regular expression. There is no need of rewrite statement as we already had a regular expression in location. Nginx supports named captures using the following syntax:

?<name>     Perl 5.10 compatible syntax, supported since PCRE-7.0
?'name'     Perl 5.10 compatible syntax, supported since PCRE-7.0
?P<name>    Python compatible syntax, supported since PCRE-4.0
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.