简体   繁体   中英

Kubernetes Ingress redirect of Express.js /public folder

I've been reading the past few days on how to do this, tried server-snippets and didn't work (or I did it wrong). Anyway, I would like to have my Ingress for my QA & Prod clusters redirect calls to my public folder in Express.js to my CDN instead of the local folders (/js, /css, /images, etc).

ie https://www.example.com/js/all.js -> https://cdn.example.com/assets/js/all.js

Has anyone done this with success and would like to give me some pointers?

This is not how CDNs work. If the traffic reaches your backend it is already "too late" for CDNs. Your static assets must be delivered directly via your CDN. Only non-static page requests, REST-API calls and Websocket connections should make it to your cluster.

What you have to do now is pretty simple. Inside your (generated) HTML, replace the URLs to your assets that point to your own server with the ones pointing to your CDN. Eg the following:

<script type='text/javascript' src='https://www.example.com/js/all.js'></script>

becomes:

<script type='text/javascript' src='https://cdn.example.com/js/all.js'></script>

In fact , your own server must respond to calls to your assets (eg /js, /css, /images, ...) with its own local resources in order to let your CDN fetch and cache the resources properly. Redirecting traffic would probably break your CDNs caching mechanisms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM