Using Fastly to host a site in a subfolder?


#1

Normally we host Discourse sites using a subdomain, e.g.

discourse.example.com

However we have a customer who wants

example.com/discourse

So we’re building out support for that. One thing we’ve run into is that this complicates the hosting scenario considerably, as

  • discourse.example.com can point to a completely different IP address on our servers. This works great for hosting.

  • example.com/discourse has to point to their domain. Not great for hosting. So this means either

    • we host their entire domain (not desirable for many reasons)

    • they proxy just the traffic in the /discourse folder to our server’s IP, which is complex and error prone for them.

We use Fastly as our CDN and have had great results with it, so we’re wondering, can we recommend they also purchase a Fastly plan, and use Fastly in front of their domain both as a CDN and as a proxy solution?

This would keep the complex proxying in the hands of third party experts (namely, Fastly), and we could share access to the Fastly credentials so we can both troubleshoot if something goes wrong.

Any tips for this kind of configuration, where you want to put a subfolder in control of a different IP address on the internet? Are there big price disparities if they need https vs http for this function?


#2

This isn’t possible with DNS, unfortunately. I did see this option on StackOverflow, but it assumes that example.com is being served directly out of origin.

The option you suggested – working within your customer’s Fastly account, is something that we’ve seen other organizations do before.


#3

By “working within fastly” I assume you just set up multiple backends and then use a rule to direct traffic to the correct origin. Is there an example config for this somewhere?


#4

It’s pretty easy, actually. You just add a second backend to the service, and set a conditional on the new backend like this:

This will route all traffic matching the path /discourse to the Discourse backend.


#5

Just a quick note, got this working, one slight amendment is that I used the reverse rule for the other backend.


#6

I have a use case pretty close to this, but I haven’t quite figured out if or how it’s possible.

Site 1 is at one host, and needs to be served at http://www.example.com/
Site 2 is at a second host, and needs to be served at http://www.example.com/blog/ (but has a subdomain of blog.site.com for DNS purposes).

I have no access, alas, to the web server configurations. The usual methods via mod_rewrite aren’t available. I’ve played a bit with Fastly, and it seems like on the surface I could set up two backends (main, blog) and a conditional that looks for any ^/blog requests, sending those to the blog backend. The thing is, though, that method seems to be more of a redirect, sending those requests to the Blog backend results in blog.site.com/blog/ being served.

Is there any way to do this with Fastly?

Thanks!


#7

What’s probably happening is that your blog origin doesn’t like the Host: www.example.com header, and serves the redirect.

There’s two things you can do:

  1. Add www.example.com to the webserver configuration.
  2. Rewrite the Host header to a hostname it does like (i.e. blog.example.com) in your Fastly config.

Just add a Header object to set it, and put the same condition on it as you used for the origin selection.

Cheers,

Doc


#8

Just to clarify, I changed from site.com to example.com, since that is what one is supposed to use in examples. :smile:

Hope that didn’t confuse anyone.