title: Nginx Proxy_Pass to CDN vs hitting CDN directly. Pro's, Con's, Is it slower or are there negative effects on the server?
body: <h2>Situation</h2>
  
  <p>CDN's are great but I found a need to dynamically populate files into one. Below I describe how I setup NGINX to do this. My question is <code>Is it any slower or are there any performance issues for doing a proxy_pass to a resource than hitting the resource directly</code>? For example.. proxy_pass via URL to <a href="http://domain.com/file.jpg" rel="nofollow">http://domain.com/file.jpg</a> vs loading <a href="http://domain.com/file.jpg" rel="nofollow">http://domain.com/file.jpg</a> directly. Also, <code>are you saving any server server resources by doing a proxy_pass to another image location vs simply serving up the image</code>?</p>
  
  <h2>What I did</h2>
  
  <p>I setup a Nginx server so a request to <em>http://domain.com/image/XYP.jpg</em> would do a Proxy_pass to a CDN, effectively loading <em>http://cdn.com/XYP.jpg</em>. I configured Nginx to check to see if there was an error code and then do something else. IF (error page) 404, nginx can pass you to a local resource which then might trigger the creation of the file on the CDN.</p>
  
  <p>Next time, you request <em>http://domain.com/image/XYP.jpg</em>, you'll be accessing the CDN.</p>
  
  <h1>So the steps are :</h1>
  
  <ul>
  <li>Request Attempt # 1 for <code>xyz.jpg</code></li>
  <li>Proxy_pass to CDN</li>
  <li>If File 200, load file : If 404, load local file</li>
  <li>Trigger file generation of <code>xyz.jpg</code> on CDN</li>
  <li>Request Attempt # 2 for <code>xyz.jpg</code></li>
  <li>Proxy_pass to CDN</li>
  <li>File exists (200).. load file from CDN indefinitely and not locally</li>
  </ul>
  
  <h1>Technology being used</h1>
  
  <ul>
  <li>PHP</li>
  <li>Ubuntu</li>
  <li>Nginx</li>
  <li>CDN</li>
  </ul>