CodeMonkeyB
CodeMonkeyB

Reputation: 3060

Redirect bots using heroku to a different server

I have a Django website running on Heroku where most of the data is served via AJAX. In order to allow search engines to index those pages I want to serve requests with the _escaped_fragment url parameter using phantomjs running on a separate nodejs server. similar to this on apache:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule (.*) http://webserver:3000/%1? [P]
(source: http://backbonetutorials.com/seo-for-single-page-apps/)

` How can I do this in Heroku?

Upvotes: 0

Views: 173

Answers (1)

Thomas
Thomas

Reputation: 11888

Heroku does not give you control over the frontend loadbalancer (which is where you'd ideally want this embedded). You would need to embed this logic into your application (See: Middleware), or run your own "mini loadbalancer" in front of your application (i.e. you would run nginx on each web worker, purely to implement this match and redirect scheme).

To use nginx, the fastest way is to use https://github.com/ryandotsmith/nginx-buildpack

Step 1: Use heroku-buildpack-multi

$ heroku config:set BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git

Step 2: Add the required extra buildpacks to .buildpacks

$ echo 'https://github.com/ryandotsmith/nginx-buildpack.git' >> .buildpacks
$ echo 'https://github.com/heroku/heroku-buildpack-python.git' >> .buildpacks
$ git commit .buildpacks -m 'Add multi-buildpack'

Step 3: Add uwsgi to your requirements.py. UWSGI is the fastest way to server python projects out of heroku, and importantly it can listen on a domain socket.

Step 4 : Add a uwsgi.conf to your project

[uwsgi]
http-socket = /tmp/nginx.socket
master = true
processes = 4
die-on-term = true
memory-report = true
module = yourapp.wsgi:application
env = DJANGO_SETTINGS_MODULE=yourapp.settings

Step 5: Create config/nginx.conf.erb in your project. Copy from here. Edit this to add your custom redirect logic.

Step 6 : Modify your app to touch /tmp/app-initialized when it starts up

Now obviously this is pretty heavy modification, and will take a while to get it set up right. If you don't really need this high-performance option, take the simpler route and set up a custom middleware layer instead.

Upvotes: 2

Related Questions