大网数据广告活动
服务器运维问答

Apache Custom Header with an environment variable

· 52CE 我爱测

I try to set a custom HTTP Header with an environment variable** with Apache server 2.4.6 and headers_module. I work inside a HTTP VHost on port 80. Everything work as expected with basic example like: Header set MyHeader "%D %t"…

问题原文

原帖链接 · S. Di Cioccio · CC BY-SA 4.0

I try to set a custom HTTP Header with an environment variable** with Apache server 2.4.6 and headers_module.

I work inside a HTTP VHost on port 80.

Everything work as expected with basic example like:

Header set MyHeader "%D %t"

I have read the Apache Documentation of mod_headers : https://httpd.apache.org/docs/2.4/en/mod/mod_headers.html#header

It is said that one can access environment variable using %{ENVVARNAME}e

(with ENVVARNAME the name of the ENV variable)

I also read other post but no real working answer: Apache set custom header with an evironment variable

But my problem is to add a header than identify my Apache Server like the HOSTNAME environment value

  1. I tried this

Header Set X-Serv %{HOSTNAME}e

No success : always display X-Serv:{null} instead of the value

  1. I also tried :

SetEnv myvar ${HOSTNAME} Header set X-Serv %{myvar}e

No success : it displays in my HTTP Headers : X-Serv: %{myvar}e

I also tried some other various combinations:

Please note: My HOSTNAME environment variable exists and it display in 'env' command with the apache user owning the httpd process

Any idea?

社区高分解答

原帖链接 · S. Di Cioccio · CC BY-SA 3.0

Found a way to get it working:

  • Ensure module is activated (mod_headers) or activate if necessary

    a2enmod headers
    
  • in /etc/apache2/envvars add

    export HOSTNAME=`uname -n`
    
  • in <VirtualHost> clause

    Header set apachehost ${HOSTNAME}
    
  • Restart or reload httpd service

    service apache2 restart
    

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。