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


  • Quote of the Week: John Quincy Adams

    “Duty is ours; results are God’s.” — John Quincy Adams

  • Off to RubyConf 2006

    I’m off to RubyConf 2006 tomorrow. I used to live in Colorado so I’m looking forward to returning to the area and exploring some old haunts. I’m also looking forward to hanging out with other Ruby geeks and hear Nathaniel’s talk again (he gave it at last night’s Ruby meetup). This will be my first RubyConf. I’m psyched.

  • What’s the best Rails web host?

    During the Ruby hack fest tonight the topic of Rails web hosts came up again. This seems to be a frequent topic of discussion lately. The usual questions are (1) which host is the best, and (2) which host is the cheapest? I thought I’d post my own recommendations here for those who are interested.

    I’ve hosted my small Rails apps at TextDrive for over a year now. Although I’ve heard from many people who seem to have no end of trouble with them, I’ve experienced nothing but stellar service from them so far. I average maybe an hour of down-time per month, which is perfectly acceptable to me. The control panel used to be slow as mud, but it’s pretty zippy now. With shell access and the flexibility to customize the environment to suit my needs, it’s a nice setup. It’s also $12.95/month for the basic package.

    Other hosts that support Rails include DreamHost and OCS Solutions. You can read more about OCS on Soapadoo. I’ve never used either of these hosts, but I’ve heard good things about them from people who have.

    So, what’s your favorite Rails web host?

  • Quote of the Week: George Washington

    “The name of American must always exalt the just pride of Patriotism. With slight shades of difference, you have the same Religion, Manners, Habits, and political Principles. …

    “Of all the dispositions and habits which lead to political prosperity, religion and morality are indispensable supports.

    “In vain would that man claim the tribute of patriotism, who should labor to subvert these great pillars of human happiness, these firmest props of the duties of men and citizens.”

    — George Washington