• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • dnrTV features Venkat on .NET Generics

    dnrTV’s March 9th episode featured Venkat Subramaniam giving an interactive demonstration of .NET Generics. The entire video tutorial is about an hour long. I thought it was quite useful. It’s fantastic being able to watch an expert in this area as he writes code and explains what he’s doing while he’s writing it. It’s almost as good as pair programming with a master developer.

    I may have to check out some of the other shows on dnrTV this week. .NET Gotchas and Databinding in .NET 2.0 in particular look like they would be interesting.

  • Ham radio equipment restrictions

    Amateur radio is a hobby of mine and it’s always sad to read about the restrictions being placed on hams these days. This WRAL article is especially interesting, considering the amount of help hams have provided during the aftermath of the Katrina disaster.

    73s,
    Matt / AG4TS

  • Quote of the Week: George Washington

    “We may find, by our own unhappy experience, that there is a natural and necessary progression from the extreme of anarchy to the extreme of tyranny, and that arbitrary power is most easily established upon the ruins of liberty abused to licentiousness.” — George Washington

  • Ruby crashes the next Java User’s Group

    So what in the world is Ruby on Rails? Where does it make sense to use it? How do you get started developing with it? And why would I forsake the established Java community for the wild ravages of a “new” technology, and love every minute of it?

    The next Java User’s Group meeting will feature a talk by my friend and former co-worker, Nathaniel Talbott, on why he prefers Ruby on Rails over Java. It’s sure to be an educational (and controversial!) presentation. For more information, visit the JUG web site.