Search code examples
phpnginxhttp-headersfpm

PHP - Get Custom Headers when Using Nginx with php-fpm


I'm trying to convert my PHP web application (a RESTful API) from Apache to Nginx.

It's working fine in Apache, but the codebase currently uses the apache_request_headers() method in order to retrieve the custom headers that we use for authentication. An example header is auth_system_signature.

I had hoped it would be a simple case of replacing the call to that function with getallheaders(). However, it appears that this method is undefined when using php-fpm.

I see various "workarounds" on the net that will define the function if it doesn't exist, but they all appear to loop over $_SERVER which does not have my custom headers. I'm pretty sure if it did, then we wouldn't have been using apache_request_headers() in the first place.

Is there a way to retrieve the custom headers in the request with php-fpm, or do I just have to move over to using php-cgi?

Context

  • OS: Ubuntu 16.04
  • PHP version: 7.0.

Solution

  • It turns out that Nginx won't pass on headers that have underscores in them by default. You could replace your custom headers' underscores with hyphens as others have done, or you could update your nginx.conf file and set:

    underscores_in_headers on
    

    Now your headers will appear in the $_SERVER superglobal with the HTTP_ prefix. They will also be in all caps.