Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Verifying Apache Configuration for apache_request_headers

3 min read
01.05.2025

The PHP function apache_request_headers() relies on Apache properly passing HTTP headers to the PHP environment. If it doesn't work, it's often due to Apache configuration issues. Here's how to verify and fix your Apache setup.

Apache Headers Configuration Verification
Verifying Apache configuration for apache_request_headers().

For the Authorization-specific case that triggers most of these verifications, see Fixing apache_request_headers Authorization Not Working — A System Administrator's Guide. For mod_headers reference, How to Install and Enable mod_headers in Apache and Understanding mod_headers in Apache.

This guide covers Apache-specific configuration. For NGINX or other web servers, refer to our server-specific guides.

Ensure the Apache Headers Module is Enabled

The mod_headers module must be enabled in Apache to pass and manipulate HTTP headers.

Check if mod_headers is Enabled

  1. Run the following command:
    apachectl -M | grep headers
    • If you see headers_module (shared) in the output, the module is enabled.
    • If not, enable it.

Enable mod_headers

  1. Enable the module:
    sudo a2enmod headers
  2. Restart Apache:
    sudo systemctl restart apache2

Ensure Headers Are Passed to PHP

Some Apache configurations strip certain headers (like Authorization) before passing the request to PHP.

Modify Apache Configuration

  1. Open the main Apache configuration file:
    sudo nano /etc/apache2/apache2.conf

    Or, if using a virtual host:

    sudo nano /etc/apache2/sites-available/your-site.conf
  2. Add the following directive inside the <VirtualHost> block or globally:
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
  3. Save the file and restart Apache:
    sudo systemctl restart apache2

Check .htaccess Rules

If your application uses an .htaccess file, it might be stripping headers.

Add Header Rules to .htaccess

  1. Open your .htaccess file in the root directory of your application.
  2. Add the following:
    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
  3. Save the file.

Check for Proxy or Load Balancer Issues

If your Apache server is behind a reverse proxy or load balancer (e.g., NGINX, Cloudflare), the proxy might strip headers before passing them to Apache.

Fix for NGINX:

  1. Open the NGINX configuration file (e.g., /etc/nginx/nginx.conf or site-specific config).
  2. Add this directive to preserve the Authorization header:
    proxy_set_header Authorization $http_authorization;
  3. Reload NGINX:
    sudo systemctl reload nginx
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Debug Apache Header Handling

To verify that Apache is correctly passing headers to PHP:

Steps:

  1. Enable Apache logging at a higher level. Open the Apache config file:
    sudo nano /etc/apache2/apache2.conf
  2. Set the LogLevel to debug:
    LogLevel debug
  3. Restart Apache:
    sudo systemctl restart apache2
  4. Tail the logs and look for header-related messages:
    sudo tail -f /var/log/apache2/error.log

Test Header Passing

Use a tool like curl or Postman to send a request to your server and check the headers.

cURL Example:

curl -H "Authorization: Bearer YOUR_TOKEN" http://your-server.com/your-script.php

Debug in PHP:

Create a simple test script (headers.php) to see all the headers:

<?php
print_r(apache_request_headers());
?>

Checklist to Ensure Proper Apache Configuration

  1. Enable mod_headers.
  2. Ensure Authorization headers are preserved using SetEnvIf.
  3. Check .htaccess rules for any conflicting directives.
  4. Fix proxy settings to pass headers if a reverse proxy is used.
  5. Enable logging to debug header behavior in Apache.

By verifying and fixing these aspects of Apache configuration, the apache_request_headers() function should work as expected.

Frequently asked questions
Bottom up: (1) confirm mod_headers loaded with apachectl -M; (2) verify CGIPassAuth on or SetEnvIf in vhost/.htaccess; (3) check there's no proxy/load balancer in front stripping the header; (4) trigger a request with curl -H "Authorization: Bearer test" and dump $_SERVER inside PHP to see what arrived.
Apache + mod_php passes Authorization automatically. Apache + PHP-FPM (the modern combination) doesn't — the FastCGI bridge strips it as a CGI security feature. Most modern Linux distros ship PHP-FPM by default, so most fresh installs hit this issue.
Cloudflare, AWS ALB, and stock Nginx (without explicit proxy_pass_header Authorization;) all forward Authorization by default. Trouble starts with proxies that explicitly drop "hop-by-hop" or "sensitive" headers — check the proxy's config and ensure Authorization is on its forward list.
curl -v -H 'Authorization: Bearer test-token-123' https://yourdomain.com/auth-test.php where auth-test.php is just
Related articles
Solving apache_request_headers Issues on NGINX: A Professional Server Administrator's Guide
How a Programmer Can Solve Issues with apache_request_headers in PHP
How to Enable and Configure mod_headers in Apache