大网数据广告活动
服务器运维问答

How can I block direct IP access to my web server without response error code?

· 52CE 我爱测

I hope people can help me with this question. Example: My domain name is: example.com My IP Address is 103.103.103.103 When people try to connect to my IP on port 80. It will not respond any request. It should show: connecting(on…

问题原文

原帖链接 · Jane Thorne · CC BY-SA 3.0

I hope people can help me with this question.

Example:

My domain name is: example.com

My IP Address is 103.103.103.103

When people try to connect to my IP on port 80. It will not respond any request.

It should show: connecting(on browser)... with no response.(It will look like unable to connect)

I have tried to modify httpd.conf(i'm using apache 2.4) but not successful yet.

I tried googling but there is no result for my question.

Thank you

已采纳解答

原帖链接 · Tom · CC BY-SA 3.0

Assuming that you want requests to http://example.com to serve your website, and requests to http://103.103.103.103 to be explicitly dropped you have 2 main options that I am aware of.

1) is to use mod_security module and the drop target. A configuration like this;

    <VirtualHost *:80>
        ServerName default.only
        SecRuleEngine On
        SecRule REMOTE_ADDR "^\d" id:'1234',drop,phase:1
    </VirtualHost>

    <Directory /var/www/example.com >
      Options FollowSymLinks
      AllowOverride FileInfo Options
      Order allow,deny
      Allow from all
    </Directory>

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/example.com
    </VirtualHost>

returns the website, if you go to http://example.com

# wget -O-  http://example.com
--2018-03-09 15:35:35--  http://example.com/
Resolving example.com (example.com)... 127.0.0.1
Connecting to example.com (example.com)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24 [text/html]
Saving to: ‘STDOUT’

and if you try to go to the default site via ip http://127.0.0.1

# wget -O-  http://127.0.0.1
--2018-03-09 15:35:28--  http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... No data received.

The connection is dropped

2) The second option is to use the string module for iptables and drop connections with a header matching Host: 103.103.103.103

iptables -A INPUT -m string --string "Host: 103.103.103.103" -j DROP

(I didn't test that iptables rules...so you would have to check the docs for the correct incantation...) However I think the mod_security option is simpler

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。