mod_env, mod_setenvif(SetEnvIf)

最後更新: 2022-09-20

介紹

一共有兩個 module 及 environment variables 有關,

它們分別是 mod_env 及 mod_setenvif

目錄

 


mod_env

 

PassEnv

# Passes environment variables from the shell

Specifies one or more native system environment variables to make available as internal environment variables

PassEnv LD_LIBRARY_PATH

UnsetEnv

UnsetEnv LD_LIBRARY_PATH

SetEnv

SetEnv SPECIAL_PATH /foo/bin

 


mod_setenvif

 

Defines environment variables based on attributes of the request

Usage(SetEnvIf):

SetEnvIf attribute regex [!]env-variable[=value]

attribute 它可以用以下

1) An HTTP request header field (RFC2616)

  • Host
  • User-Agent
  • Referer
  • Accept-Language
  • ...

2) Server 自己的 Variable

  • Remote_Addr
  • Server_Addr
  • Request_Method (GET, POST)
  • Request_Protocol ("HTTP/1.1")
  • Request_URI (without the query string)

3) The name of an environment variable in the list of those associated with the request
     (This allows SetEnvIf directives to test against the result of prior matches.)

regex

If the regex matches against the attribute, then the remainder of the arguments are evaluated.

ENV_VAR

對 ENV VAR 的處理

  • varname                 # the value will be set to "1"
  • !varname                # remove the given variable
  • varname=value       # set the variable to the literal value given by value

e.g.

SetEnvIf X-Forwarded-Proto  "https"    HTTPS=on
SetEnvIf Request_URI        "\.gif$"   object_is_image=gif

# Set environment variable if the request contains any headers that begin with "TS"
# whose values begins with any character in the set [a-z]HAVE_TS

SetEnvIf    ^TS    ^[a-z].*    HAVE_TS

應用: CORS

SetEnvIf Origin "^http(?:s)?://(:?.+\.)?(:?mydomain\.net|mydomain\.com)$" origin_is=$0
SetEnvIf origin_is "^$" origin_is="https://mydomain.net"
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is
Header merge Vary "Origin"

SetEnvIfNoCase Directive

regular expression matching is performed in a case-insensitive manner

SetEnvIfNoCase Host Apache\.Org site=apache