Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I used rewrite directives below in nginx to rewrite urls of static files to external CDN server.

rewrite ^/static/(css|images|js)/([a-z_\-\.]+)$ http://cdn.domain.com/$1_$2 last;

It works but it redirect the url automatically in browsers.

How can I do the above rewrite without redirects?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You need to change the links inside your application to point to the CDN for static includes.

When you rewrite to an http location there's nothing nginx can do but redirect the browser (since the CDN is outside of nginx, and the browser needs to get the files from the CDN). You would have the same issue with Apache or any other URL rewriter, as the CDN is not an "internal" location to the web server.

One option might be the nginx subsitution module, which can replace content as it is delivered. But that doesn't handle regular expressions, and would slow down every request. It is better just to change your application's HTML to reference the CDN URLs directly.

share|improve this answer

nginx is case sensitive (feature or bug), use HTTP:// instead of http:// for example:

rewrite ^/static/(css|images|js)/([a-z_\-\.]+)$ HTTP://cdn.domain.com/$1_$2 last;

but request will be send to proxy_pass server, and it only works if server in proxy_pass is also web-proxy server, who understands

GET HTTP://cdn.domain.com/ HTTP/1.0
share|improve this answer

If you want to hide URL of external CDN server you must find CDN with aliases support. Then point your subdomain (cdn.yourdomain.com) to CDN.

share|improve this answer
    
This answer is offtopic , he is asking how to server Static content from CDN. He wants to re-write URL of static files to that of CDN –  BigSack Aug 16 '13 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.