Apache2 反向代理剥离了我的自定义 header

Apache2 reverse proxy strips my custom header

对于我的 REST API,我试图通过 apache2 反向代理将自定义 header X-APP-ID 传递给托管 API 的应用程序,但是,它似乎 apache2 正在剥离 header。它没有到达应用程序。这是为什么?

这是我的 apache2 配置

<VirtualHost *:443>
    ServerName $SERVER_NAME
    ServerAlias $SERVER_ALIASES

    # Make sure requests are rewritten to use https://
    RewriteEngine on
    RewriteCond %{HTTP_HOST}   !^$SERVER_ALIASES [NC]
    RewriteCond %{HTTP_HOST}   !^$SERVER_NAME
    RewriteRule ^/?(.*)         https://$SERVER_NAME/ [L,R,NE]

    SSLEngine on
    SSLOptions +StrictRequire
    <Directory />
        Require all granted
        SSLRequireSSL
    </Directory>

    SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256

    # Enable SSL (disabling weak/vulnerable protocols)
    SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLHonorCipherOrder On
    SSLCertificateFile /etc/letsencrypt/live/$SERVER_NAME/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/$SERVER_NAME/privkey.pem

   # Logging
   LogLevel warn
   CustomLog /var/log/apache2/access.log combined

   # Static files
   Alias /static/ [redacted]
   Alias favicon.ico [redacted]

   # If the URL mentions favicon, but is not acutally pointing to a file
   # location, rewrite the url to point to the favicon file
   RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
   RewriteRule  .*favicon\.ico$        [redacted] [L]

   ProxyPass /static/ !
   ProxyPass /media/ !
   ProxyPass / http://localhost:8000/
   ProxyPassReverse / http://localhost:8000/
</VirtualHost>

<VirtualHost *:80>
    # Rewrite request to use SSL
    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteCond %{REQUEST_URI} !/.well-known
    RewriteRule ^/(.*) https://$SERVER_NAME/ [NC,R,L]

    ServerName $SERVER_NAME
    ServerAlias $SERVER_ALIASES

    # Logging
    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined

    # Location for Let's Encrypt to read and write files
    Alias /.well-known /var/www/html/.well-known

</VirtualHost>

当 Django 将 HTTP header 转换为 request.META 中的键时,它会将所有字符转换为大写,将连字符替换为下划线,并添加一个 HTTP_ 前缀。

因此您应该使用 request.META['HTTP_X_APP_ID'].

访问 X-APP-ID HTTP header