• 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: Mark Twain

    “It is curious that physical courage should be so common in the world and moral courage so rare.” — Mark Twain

  • Instructions should match reality

    I had to visit the doctor yesterday for a very minor surgical procedure. The doc packed the wound with gauze when he was finished and gave me some instructions on how to remove it later that night.

    As seems to be typical with doctors these days, the gauze he used was that industrial-grade stuff. The kind they probably use to mop up nuclear waste. The point is that it persists in sticking to a wound until the bitter end. The irony is that the instruction sheet he gave me said it would “come off easily” with some hydrogen peroxide. An hour later and it was still hanging on for dear life.

    The moral of the story? Whether in medicine or software, instructions should match reality!

  • Quote of the Week: Napoleon Hill

    “The majority of men meet with failure because of their lack of persistence in creating new plans to take the place of those that fail.” — Napoleon Hill

  • Restarting Windows services in C#

    It’s dead simple to restart a Windows service in C#. I’m a little shocked at how easy it is actually, given some of the other things I’ve found to be so ugly about the language.

    public void RestartService(string name)
    {
        ServiceController service = new ServiceController(name);
        service.Stop();
        Thread.Sleep(2500);
        service.Start();
        Thread.Sleep(2500);
    }
    

    You guessed it: “name” is the name of the service to restart. It must exactly match the actual service name or bad things will happen. I added the sleeps for good measure. They may or may not be needed depending on how responsive you want the service to be immediately after restarting.

    Now if only C# made it this easy to restart a UNIX process…