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

apache get environment variable value

· 52CE 我爱测

I would like to get the value of a environment variable and assign it to another variable in Apache. For example, get the value of $_SERVER['REMOTE_ADDR'] assign it to $_SERVER['USER-IP'] and override $_SERVER['REMOTE_ADDR'] to 1…

问题原文

原帖链接 · MontrealDevOne · CC BY-SA 4.0

I would like to get the value of a environment variable and assign it to another variable in Apache.

For example, get the value of $_SERVER['REMOTE_ADDR'] assign it to $_SERVER['USER-IP'] and override $_SERVER['REMOTE_ADDR'] to 127.0.0.1

I am stuck on getting the value of REMOTE_ADDR

<IfDefine !USER-IP>
  SetEnv USER-IP %{REMOTE_ADDR}
  SetEnv REMOTE_ADDR '127.0.0.1'
</IfDefine>

已采纳解答

原帖链接 · Tom · CC BY-SA 3.0

Looking at the docs, the $_SERVER['REMOTE_ADDR'] var is not strictly an environment variable, but a cgi request meta-variable provided by the web server to the cgi context; http://www.faqs.org/rfcs/rfc3875.html

Meta-variables contain data about the request passed from the server to the script

  meta-variable-name = "AUTH_TYPE" | "CONTENT_LENGTH" |
                       "CONTENT_TYPE" | "GATEWAY_INTERFACE" |
                       "PATH_INFO" | "PATH_TRANSLATED" |
                       "QUERY_STRING" | "REMOTE_ADDR" |
                       "REMOTE_HOST" | "REMOTE_IDENT" |
                       "REMOTE_USER" | "REQUEST_METHOD" |
                       "SCRIPT_NAME" | "SERVER_NAME" |
                       "SERVER_PORT" | "SERVER_PROTOCOL" |
                       "SERVER_SOFTWARE" | scheme |

The apache docs indicate that these variables cannot be overriden using the standard SetEnvstyle directives

Some Caveats

It is not possible to override or change the standard CGI variables using the environment manipulation directives.
https://httpd.apache.org/docs/2.4/env.html#setting

So I think it's unlikely you can set those values easily from apache conf

Setting environment variables

(from existing server variables)

<Directory /var/www/server111>
    Order allow,deny
    Allow from all

    # This syntax works, as you can see from the image below...
    RewriteEngine On
    RewriteRule .* - [E=USER-IP:%{REMOTE_ADDR}]

    # none of these syntax seem to work
    SetEnv USERIP %{REMOTE_ADDR}
    SetEnv USERIP2 blah
    SetEnv USERIP3 ${REMOTE_ADDR}
    SetEnv USERIP6 %{ENV:REMOTE_ADDR}
</Directory> 

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

资料来源与适用范围

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

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

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