Nginx Proxy_Pass to CDN vs hitting CDN directly. Pro's, Con's, Is it slower or are there negative effects on the server? Nginx Proxy_Pass to CDN vs hitting CDN directly. Pro's, Con's, Is it slower or are there negative effects on the server? nginx nginx

Nginx Proxy_Pass to CDN vs hitting CDN directly. Pro's, Con's, Is it slower or are there negative effects on the server?


There isn't much point in proxy_pass to a CDN. When you consider what proxy_pass does, it should be apparent.

proxy_pass simply means Nginx says to the backend (CDN in your case) fetch the resource and return to me so that I can serve it to the client.

So in your case, what is happening is ...

  1. Client makes request for x.jpg
  2. Nginx resolves location of CDN
  3. Nginx asks CDN for x.jpg
  4. CDN sends x.jpg to Nginx
  5. Nginx receives and buffers x.jpg etc
  6. Nginx forwards x.jpg to client

The round trip to the CDN is essentially a waste since Nginx still has to send the file to the client. Far better to just send it directly ...

  1. Client makes request for x.jpg
  2. Nginx gets x.jpg from local disc
  3. Nginx sends x.jpg to client

If you want to use a CDN, then you should redirect the request to the CDN ...

  1. Client makes request for x.jpg
  2. Nginx redirects client to CDN
  3. CDN sends x.jpg to client

Obviously, you will not be able to do the 404 test in the case of the redirection as Nginx is no longer involved in the transaction after the redirection.