简体   繁体   English

如何在htaccess中指定从何处获取IP

[英]How to specify where to get IP from in htaccess

Okay so we use some simple IP authentication with htaccess for a folder on our site: 好的,所以我们对站点上的文件夹使用一些简单的htaccess IP身份验证:

order deny,allow
deny from all
allow from 1.1.1.1

But with the new CDN we are on, the actual users IP address comes through in a different server variable, lets say: 但是随着新CDN的推出,实际用户的IP地址将通过不同的服务器变量输入,可以这样说:

$_SERVER['absolutely_true_ip'];

whereas

$_SERVER['remote_addr'];

is not the users true IP 不是用户的真实IP

Is there anyway to tell htaccess where to look for the IP we want to authenticate with? 无论如何,有没有告诉htaccess在哪里寻找我们想要进行身份验证的IP?

Yes this is possible. 是的,这是可能的。

X-FORWARDED-FOR is used by most servers (CDNs, Proxys) to send the originating ip (the users ip). 大多数服务器(CDN,代理)使用X-FORWARDED-FOR发送始发ip(用户ip)。 So this should to the job: 所以这应该工作:

SetEnvIf X-FORWARDED-FOR 1.1.1.1 allow
order deny,allow
deny from all
allow from env=allow

If your CDN has a different variable name, just edit the first line. 如果您的CDN具有不同的变量名,只需编辑第一行。


EDIT: 编辑:

If you want to allow multiple ip's, just duplicate the first line: 如果要允许多个IP,只需复制第一行:

SetEnvIf X-FORWARDED-FOR 1.1.1.1 allow
SetEnvIf X-FORWARDED-FOR 2.2.2.2 allow
SetEnvIf X-FORWARDED-FOR 3.3.3.3 allow
order deny,allow
deny from all
allow from env=allow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM