Legacy Archive (2015): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

Underscores in Headers When Using Nginx as a Reverse Proxy for IIS

Original Chinese title: iis使用nginx做反向代理,下划线问题

Solution

  1. If you’re in the same boat as me—a bunch of internal IIS servers on the intranet, but only one public IP facing the outside—this setup is worth considering.
  2. For the specific configuration, I followed this guide: IIS Server Clustering (Nginx Approach).

The Nginx Underscore Issue

  1. Static assets like HTML, JS, and CSS all worked without a hitch, yet a few RESTful API requests on the site simply refused to go through. Time to check Nginx’s error_log:

    client sent invalid request while reading client request line
  2. After looking it up online, I found that Nginx restricts characters in HTTP header names—if a header name contains an underscore, it gets ignored.

  3. Sure enough, the failing APIs all had underscores in their headers. There are two ways around this: the brute-force approach is to redesign the APIs and remove the underscores.

  4. Or, as some suggest, you can check the Nginx source behavior and toggle the config directive:

    In the ngx_http_parse_header_line() function:
    f  (ch == ‘_’) {
            if (
                    allow_underscores  // where the issue lies
                )
    Enable underscores_in_headers on
    This can be added inside the http or server block.
    
    Syntax: underscores_in_headers on|off
    Default: off
    Context: http, server
    Determines whether underscores are allowed in header field names

That’s Pretty Much It