Set REMOTE_ADDR to X-Forwarded-For in apache

前端 未结 9 1208
感情败类
感情败类 2020-12-29 10:06

In a situation where Apache is sitting behind a reverse proxy (such as Squid), the cgi environment variable REMOTE_ADDR gets the address of the proxy rather tha

9条回答
  •  北海茫月
    2020-12-29 10:12

    Note that the X-Forwarded-For header may contain a list of IP addresses if the request has traversed more than one proxy. In this case, you usually want the leftmost IP. You can extract this with a SetEnvIf:

    SetEnvIf X-Forwarded-For "^(\d{1,3}+\.\d{1,3}+\.\d{1,3}+\.\d{1,3}+).*" XFFCLIENTIP=$1
    

    Note the use of $1 to set the XFFCLIENTIP environment variable to hold the contents of the first group in the regex (in the parentheses).

    Then you can use the value of the environment variable to set headers (or use it in Apache log formats so that the logs contain the actual client IP).

提交回复
热议问题