title: Nginx conf for prerender + reverse proxy to django + serving angular in html5 mode
body: <p>The mouthful of a title says it all: </p>
  
  <p>We've got an Angular frontend with a Django backend providing a REST API that is exposed independently at endpoints <code>example.com/api/v1/*</code></p>
  
  <p>The Angular app runs in HTML5 mode, and we want hard-links to <code>example.com/foo/bar</code> to bring users into the app at the <code>foo.bar</code> state as if it were a static page rather than an app state (where <code>foo</code> is anything but <code>api</code>). </p>
  
  <p>We're running behind nginx,and our basic strategy in the conf was to  define locations at <code>^~ /scripts</code>, <code>/images</code> etc. for serving static content directly, as well as a <code>^~ /api/*</code> location that gets routed to django. Below that, we have a <code>location ~ ^/.+$</code> that matches any path not matched by any of the above and "sends it to Angular" - i.e. serves our index page to it and appends the path to the base url, allowing our angular router to handle it from there. </p>
  
  <p>This is our conf in full: </p>
  
  <pre><code>upstream django {
      server 127.0.0.1:8000 fail_timeout=0;
  }
  
  server {
      listen                  80;
      server_name example.com;
      return 301 https://example.com$request_uri;
  }
  
  server {
      listen          443;
      server_name example.com;
      client_max_body_size 10M;
      ssl                     on;
      ssl_certificate         /etc/ssl/thawte/example_com.crt;
      ssl_certificate_key     /etc/ssl/thawte/example_com.key;
      ssl_verify_depth 2;
      gzip            on;
      gzip_types      text/plain text/html application/javascript application/json;
      gzip_proxied        any; 
  
      index index.html;
  
      location ^~ /index.html {
          gzip_static on;
          root /www/dist;
      }
      location ^~ /images/ {
          expires max;
          root /www/dist;
      }
      location ^~ /scripts/ {
          expires max;
          gzip_static on;
          root /www/dist;
      }
  
      location ^~ /favicon.ico {
          expires max;
          root /www/dist;
      }
  
      location ^~ /api {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-Forwarded-Proto https;
          proxy_redirect off;
          proxy_pass http://django;
      }
  
      //Send anything else to angular 
      location ~ ^/.+$ {
          rewrite .* /index.html last;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>This has worked perfectly for us, <strong>but we now need to set it up to work with prerender.io</strong>. We've tried doing this several ways, making modifications on <a href="https://gist.github.com/thoop/8165802" rel="nofollow">the official prerender nginx example</a>, but none have worked - crawlers are getting the same code users are rather than cached pages. </p>
  
  <p><strong>How can we get this working?</strong></p>
  
  <p>(note: this is new territory for everyone involved here, so if the best way to handle this involves making different choices a few steps back, please suggest them)</p>