Configuring any CDN to deliver only one file no matter what url has been requested

后端 未结 9 1809
日久生厌
日久生厌 2021-02-01 22:29

I am currently working on a new project where the entire page should be implemented in HTML5/JS working against an API/JSON. Since the entire application should only consist of

相关标签:
9条回答
  • 2021-02-01 22:31

    We recently contacted edgecast.com (which is a cdn like cloudfront) and through their support they told me that this is indeed something that they can do, but not through their standard interface. I was told to simply contact them when we needed a wildcard route to a single file.

    As for your your question: yes, it's possible. Just contact them through their live chat and they'll help you out, good luck!

    Some more (negative) information: A catch-all-rule like this means the stupid favicon.ico-forced-request some browsers (read IE) do will be caught and the regular html-page will be downloaded again. In fact, all automatic requests (iframes also request a favicon, for example) against the root-domain will be caught and the regular html-file will be downloaded. This may or may not be a problem for you but for me all these hidden requests is making me rethink the solution and using a webserver behind to do the actual catch-all. Shame really.

    0 讨论(0)
  • 2021-02-01 22:33

    In case you have your own domain that point at the CDN (I know CloudFront let you do that), you could use CloudFlare ( https://www.cloudflare.com/ ) as a reverse proxy between your users and the CDN.

    Thanks to their free plan, you can create a rule that redirects everything to index.html. I think this is the only way to achieve what you want, given that CDNs are configured to serve only static existing files as you know.

    0 讨论(0)
  • 2021-02-01 22:36

    This guy had a similar problem and used S3 / CloudFront. All his urls redirect to index.html with a statuscode 200.

    This is a workaround as it involves setting index.html as an error page.

    See details: https://kkob.us/2015/11/24/hosting-a-single-page-app-on-s3-with-proper-urls/

    0 讨论(0)
  • 2021-02-01 22:38

    CDNs are intended to deliver static content by serving the static resource from the closest geographical point possible to the client. CDN technology is not intended to do a redirect or server side processing of the request. You'll need something else involved here to do that part. The question is just whether that is a server side technology or some sort of load balancer/firewall request re-writing (to avoid having a server side technology).

    I don't think there is a truly platform agnostic way of doing this. You'll always be tied to either a server side technology or a load balancer/firewall platform. But it also sounds like you may already have this constraint as you need somewhere to host your JSON API? Even if you haven't decided on the platform, pretty much any platform should allow you to do some basic routing. If you can serve JSON Http requests, you should be able to do some page routing too.

    As a side note, I don't believe you want to return your "index.html" from absolutely all possible URLs at your domain. You'll want some list of valid URLs and invalid URLs. In which case you'll need to be pinging your back end anyway to validate the request URL. That further suggests to me that a server side technology will be better suited for this task then a blind "catch-all" redirect at a lower level.

    My personal preference would be to use your favorite MVC framework to serve indexable content with your desired URL structure (pretty much all page loads) and then use your JSON api to work with that content after page load (any dynamic stuff you want to be able to do). The whole thing, both page loads and API, being served from the same server platform/environment.

    0 讨论(0)
  • 2021-02-01 22:40

    Any CDN should have the capability of defining an origin server. This server gets contacted by the CDN to serve the file if the edge location doesn't have it.

    The good news is that the origin server can be anything that serves web pages, such as Apache, Nginx, etc. This means that you can apply any kind of rewriting rules you wish.

    If you don't wish to set up the origin server yourself, you could look at hosting your (static) site on S3. Recently they have introduced web redirects which may help you to to serve the same file under a different "alias". Failing that, you could look at redefining the standard error document, but I'm not sure whether an error status code will still be sent.

    0 讨论(0)
  • 2021-02-01 22:40

    Nginx http server can do this like:

    location /{
        # serve a file
    }
    

    or you can customize your links like

    location /my_html{
         # serve html file
    }
    
    location /cdn/{
         # serve rest files
    }
    

    you can even check urls by regexps

    location ~ /cdn/.*\.js${
        # serve cdn
    }
    
    0 讨论(0)
提交回复
热议问题