在.htaccess auth中使用HTTP_X_REAL_IP

时间:2012-02-14 12:36:37

标签: apache http-headers

我的web服务器前面有一个nginx loadbalancer,因此REMOTE_ADDR标头被loadbalancer的ip覆盖。

原始IP地址位于HTTP_X_REAL_IP标头中。我知道我可以获得带有重写规则的标题,但是还有一种方法可以在允许.htaccess Auth方法中使用http标头吗?

<Directory "/path/*">
  AuthType Basic
  AuthName "Authentication Required"
  AuthUserFile "/path/authfiles/www-app"
  Require valid-user
  ...
  Allow from HTTP_X_REAL_IP
</Directory>

我尝试使用谷歌搜索,但只是遇到了setifenv(尝试过)并重写了规则。

2 个答案:

答案 0 :(得分:1)

Allow不允许您向其传递HTTP标头,但它确实接受环境变量。你应该能够获得环境。变量RewriteCond / RewriteRule。我没有测试过,但这样的事情可以起作用:

RewriteCond %{HTTP:HTTP_X_REAL_IP} ^.+$
RewriteRule .* - [env=real_ip:%0]
Allow from env=real_ip

答案 1 :(得分:1)

使用此:

SetEnvIf X-Forwarded-For ^(%IP1|%IP2|%IP3)$ let_me_in
Allow from env=let_me_in

其中%IP1,%IP2,%IP3 - 您的IP。

当然,您可以根据需要更改正则表达式,或者只用一个IP替换它,或者在此处阅读更多内容: http://httpd.apache.org/docs/trunk/mod/mod_access_compat.html