欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

代理服务器 htaccess 拒绝真正的ip (不是代理的ip)的访问 代理禁止ip 有大用 有大大用 有大大大用

见下面的红色的字有大用

我有限制访问特定IP(10.0.0.5)所需的URL。当我以下一种方式进行直接访问时,它可以完美地完成:


<Location /incoming>    Order Allow,Deny    Deny from 10.0.0.5    Allow from all </Location>
       

但是,当此IP来自代理(代理IP:192.168.1.43)时,此解决方案不起作用。这就是我在日志中看到的:        

10.0.0.5,192.168.1.43 - - [24 / May / 2017:16:03:54 +0300]“POST / incoming HTTP / 1.0”200 698 0/6899“ - ”“ - ”            

我试着做下一个 - 添加代理部分:        

<Proxy /incoming >
    Order Allow,Deny
    Deny from 10.0.0.5
    Allow from all
</Proxy>
       

它也没有帮助。        

我需要你的帮助,朋友们!      

如果代理设置了X-Forwarded-For标头,您应该能够使用 (下面的例子阻止了多个 many ip)         

下面红色字是自己亲自做的 ,肯定有用 (并且 <Location /incoming> 和 </Location> 这两行都不需要,ip地址要使用双引号引起来 )


       正确答案         

<Location /incoming>
    Order Deny,Allow
    SetEnvIf X-Forwarded-For "10.0.0.5" DenyAccess
   SetEnvIf X-Forwarded-For "10.0.0.6" DenyAccess    Deny from env=DenyAccess
</Location>
       

       

Order Deny,Allow是一个默认允许的指令,除非deny-rule匹配,否则它将提供访问权限。SetEnvIf根据X-Forwarded-For的值有条件地设置环境标志。此处的一个拒绝规则仅在设置了该标志时触发。如果未触发拒绝规则,则允许访问。        

您也可以参考env的标志Require块,如图所示这里        

来自  https://serverfault.com/questions/852000/how-to-block-ip-address-on-apache-when-it-comes-from-proxy


.htaccess阻止真正的客户端而不是代理ip 

如何让.htaccess阻止客户端真正的IP地址它似乎只是阻止了代理ip。

例如Joe正在使用他的ISP的代理服务器80.80.80.80。他的真实IP地址(如X_FORWARDED_FOR所示)为80.123.123.123。我想要做的是阻止80.123.123.123 

但是.htaccess不会这样做。它不阻止80.123.123.123,因为这个地址是X_FORWARDED_FOR IP而不是host_ip 

我不能阻止80.80.80.80因为这会阻止成千上万的用户

这个.htaccess应该可以工作

SetEnvIf X-Forwarded-For ^ bad.ip.add.ress $ badclient 

Order Allow,Deny 

Allow from all 

Deny from all env = badclient 

这要求你有mod_access和mod_setenvif加载到你的服务器   


来自  https://www.tek-tips.com/viewthread.cfm?qid=326713


将Apache的HTTP身份验证与X-Forwarded-For Varnish中的IP白名单相结合

Mattias Geniar,2014年12月14日,星期日 - 最后修改日期:2016年7月27日星期三

这么长的帖子标题。如果要使用HTTP身份验证保护页面或整个网站,但又想将一些固定IP(例如:办公室或VPN IP)列入白名单,则可以通过.htaccess文件将两种身份验证机制组合在一起

完整的例子是这样的。

AuthName“User + Pass required”
AuthUserFile / path / to / your / htpasswd
AuthType Basic
需要有效用户
拒绝订单,允许
拒绝所有人

#Normal白名单只会添加Allow指令
从12.34.12.34开始允许
从12.34.12.35开始

#但如果您的网站位于Varnish后面,则会显示所有连接
#来自Varnish IP,很可能是127.0.0.1
#或来自主机本身的IP。
#所以我们在Varnish中设置了X-Forwarded-For标头,并进行过滤 
#在Apache的htaccess /目录访问控制中的那个标题上。
#允许来自X-Forwarded-For标头中的IP(Varnish?)
SetEnvIf X-Forwarded-For ^ 12 \ .34 \ .12 \ .34 env_allow_1
允许来自env = env_allow_1

#允许来自X-Forwarded-For标头中的另一个IP
SetEnvIf X-Forwarded-For ^ 12 \ .34 \ .12 \ .35 env_allow_2
允许来自env = env_allow_2

#HTTP身份验证需要正确,或自定义
允许X-Forwarded-For标头的#environment
满足任何

当我们更多地打破这个时间时,它会显示出2种明确的方法。

.htaccess中的HTTP身份验证

第一部分是Apache的HTTP身份验证,用户名和密码。您可以在Apache vhost配置中或.htaccess要保护的目录中的文件中进行设置。

AuthName“User + Pass required”
AuthUserFile / path / to / your / htpasswd
AuthType Basic
需要有效用户
拒绝订单,允许
拒绝所有人

这将设置AuthUserFile为存储用户名/密码的路径。如果还没有这样的文件,您可以htpasswd在CLI中使用该工具创建一个文件

$ htpasswd -c / path / to / your / htpasswd $ username

替换$username为您想要的用户名。系统将提示您输入该用户的密码。如果要将用户名/密码附加到现有文件,或者要修改现有文件中用户的密码,请使用不带-c参数的相同CLI命令

$ htpasswd / path / to / your / htpasswd $ username

并且您已设置:您的HTTP身份验证密码文件存在。

使用X-Forwarded-For进行IP白名单

此方案的正常IP白名单将像这样完成。

允许来自10.0.1.1
允许10.0.1.2
...

但是这使用连接到Apache的客户端的IP,在反向代理配置(如Nginx或Varnish)的情况下,几乎总是127.0.0.1或连接代理的IP。不是客户的IP(除非您将运行透明代理)。我们可以通过X-Forwarded-For检索它,如果它在Varnish配置中设置的话。

要检查您的Varnish配置是否已启用此功能,请搜索显示类似内容的行。

...
set req.http.X-Forwarded-For = client.ip;
...

它可以存在多种变体,但set req.http.X-Forwarded-For总是相同的。如果缺少,请将其添加到vcl_recv例程的顶部如果这不起作用,请查看我的Varnish配置中的一些想法。

现在,Apache位。您可以对HTTP标头执行检查,并根据该标头允许或拒绝身份验证。

SetEnvIf X-Forwarded-For ^ 12 \ .34 \ .12 \ .35 env_allow_1
允许来自env = env_allow_1
满足任何

env_allow_1如果X-Forwarded-ForHTTP标头与正则表达式匹配上面的示例将设置一个名为的环境变量^12\.34\.12\.35在人类的话中,这意味着“ X-Forwarded-For标头必须以12.23.12.35开头 ”。如果是这种情况,env_allow_1将设置环境变量

Allow from代码允许您检查环境变量,允许或拒绝访问。

来自 

https://ma.ttias.be/apache-http-authentication-with-x-forwarded-for-ip-whitelisting-in-varnish/

Combine Apache’s HTTP authentication with X-Forwarded-For IP whitelisting in Varnish

Mattias Geniar, Sunday, December 14, 2014 - last modified: Wednesday, July 27, 2016

Such a long title for a post. If you want to protect a page or an entire website with HTTP authentication, but also want to whitelist a few fixed IPs (for instance: office or VPN IPs), you can combine both authentication mechanismes in Apache via .htaccess files.

The full example goes like this.

AuthName "User + Pass required"
AuthUserFile /path/to/your/htpasswd
AuthType Basic
Require valid-user
Order Deny,Allow
Deny from all

# Normal whitelist would just add Allow directives
Allow from 12.34.12.34
Allow from 12.34.12.35

# But if your site is behind Varnish, all connections will appear
# to come from the Varnish IP, most likely 127.0.0.1
# or the IP from the host itself.
# 
# So we set the X-Forwarded-For header in Varnish, and filter 
# on that header in Apache's htaccess / Directory access control.
#
# Allow from an IP in the X-Forwarded-For header (Varnish?)
SetEnvIf X-Forwarded-For ^12\.34\.12\.34 env_allow_1
Allow from env=env_allow_1

# Allow from another IP in the X-Forwarded-For header
SetEnvIf X-Forwarded-For ^12\.34\.12\.35 env_allow_2
Allow from env=env_allow_2

# Either the HTTP authentication needs to be correct, or the custom
# environment that allowed the X-Forwarded-For header
Satisfy Any

And when we break this time even more, it'll show 2 clear methods.

HTTP authentication in .htaccess

The first part is the HTTP authentication, usernames and passwords, for Apache. You can set this in your Apache vhost config, or in an .htaccess file in the directory you want to secure.

AuthName "User + Pass required"
AuthUserFile /path/to/your/htpasswd
AuthType Basic
Require valid-user
Order Deny,Allow
Deny from all

This sets the AuthUserFile to the path where the usernames/passwords are stored. If there's no such file yet, you can create one with the htpasswd tool at the CLI.

$ htpasswd -c /path/to/your/htpasswd $username

Replace $username with what you want as a username. You'll be prompted for a password for that user. If you want to append a username/password to an existing file, or want to modify the password of a user in an existing file, use the same CLI command without the -c parameter.

$ htpasswd /path/to/your/htpasswd $username

And you're set: your HTTP authentication password file exists.

IP Whitelisting with X-Forwarded-For

Normal IP whitelisting for this scenario would be done like this.

Allow from 10.0.1.1
Allow from 10.0.1.2
...

But this uses the IP of the client connecting to Apache, which in the case of a reverse proxy config (like Nginx or Varnish), would nearly always be 127.0.0.1 or the IP of the connecting proxy. Not the client's IP (unless you would be running a transparant proxy). We can retrieve that via the X-Forwarded-For, if it's set in the Varnish configs.

To check if your Varnish config has this enabled, search for the line that says something like this.

...
set req.http.X-Forwarded-For = client.ip;
...

It can exist in many variants, but the set req.http.X-Forwarded-For is always the same. If it's missing, add it at the top of the vcl_recv routine. If that doesn't work, have a look at my Varnish configs for some ideas.

Now, the Apache bit. You can perform a check on HTTP headers and allow or deny authentication based on that.

SetEnvIf X-Forwarded-For ^12\.34\.12\.35 env_allow_1
Allow from env=env_allow_1
Satisfy Any

The above example will set an environment variable called env_allow_1, if the X-Forwarded-For HTTP header matches the regex ^12\.34\.12\.35, which in human words means as much as "the X-Forwarded-For header must start with 12.23.12.35". If that's the case, the environment variable env_allow_1 will be set.

The Allow from code allows you to check for environment variables, to allow or deny access.



Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek & public speaker. Currently working on DNS Spy & Oh Dear!. Follow me on Twitter as @mattiasgeniar.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!


来自  https://ma.ttias.be/apache-http-authentication-with-x-forwarded-for-ip-whitelisting-in-varnish/









你确定你所包含的ip地址是Apache看到的ip地址吗?如果服务器位于负载均衡器后面,它将看到负载均衡器地址。

要解决此问题,您需要检查X-Forwarded-For标头

        SetEnvIf X-Forwarded-For ^aaa\.bbb\.ccc\.ddd proxy_env
        Order allow,deny
        Satisfy Any
        deny from env=proxy_env



您可以通过查看日志来查看正在查看的IP地址

来自 https://stackoverflow.com/questions/22972533/blocking-of-ip-in-htaccess-not-working

https://ma.ttias.be/apache-http-authentication-with-x-forwarded-for-ip-whitelisting-in-varnish/


普通分类: