• 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.


  • Deploying PHP apps with Capistrano

    Capistrano is a wonderful tool. I have this really old PHP-based web site, TolkienMovies.com, that I needed to make a change to earlier today. (The spam bots had finally found my news submission form.) I decided this was as good a time as any to automate deployment of the app. This article was very helpful. Staring at a task like this can be daunting, but once I actually got in there and started hacking it wasn’t half as bad as I thought it would be. And now I have a warm, fuzzy feeling knowing that deployment of this stinkin’ PHP app is only one “cap deploy” command away. Hallelujah.

  • Quote of the Week: Handel H. Brown

    “We cannot remind ourselves too often that it is not our believing in things that makes them true. We believe them because they are true.” — Handel H. Brown

  • Jason Rudolph talks Grails at the JUG next week

    Jason Rudolph, local Groovy/Grails aficianado and Raleigh.rb regular, will be presenting at the RTP Java Users Group next Monday evening.

    Scott Davis “got Groovy” with the JUG in April. Now Jason will show us how Groovy’s deliciousness has been brought to bear on the world of web application frameworks. Grails is another mutation of Rails for the Java platform, promising rapid deployment, joyful coding, and as little XML as possible.

    Pizza arrives at 6:30 PM and the talk starts at 7. Post a comment here if you’re planning on attending and your first soda is on me. 😉

  • Automating file uploads with SSH and Ruby

    Are you fairly new to Ruby programming? Do you want to learn more about how Ruby can be used as the “glue” to script various libraries into harmonious cooperation? Look no further…

    InfoQ recently published an article I wrote last year about how to automate file uploads to a web server with Ruby’s Net::SSH and Net::SFTP libraries. It’s a fairly lengthy tutorial with plenty of code samples.

    Looking back, the script I created could pretty easily be reproduced in Capistrano today. But isn’t it sometimes more fun (not to mention educational) to write something from scratch? I sure think so.