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

Disable direct IP access in apache

· 52CE 我爱测

This is my httpd.config file How to disable direct IP access ? If anyone types in the ip address of the site then it should not redirect to the default domain name or the site which is xxx.com in this example <VirtualHost ipaddre…

问题原文

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

This is my httpd.config file How to disable direct IP access ? If anyone types in the ip address of the site then it should not redirect to the default domain name or the site which is xxx.com in this example

    <VirtualHost ipaddress:8080>

            ServerName xxx.com
            ServerAlias www.xxx.com
            ServerAdmin [email omitted]
            DocumentRoot /home/admin/web/xxx.com/public_html
            ScriptAlias /cgi-bin/ /home/admin/web/xxx.com/cgi-bin/
            Alias /vstats/ /home/admin/web/xxxcom/stats/
            Alias /error/ /home/admin/web/xxx.com/document_errors/
            .
            .
            .
    </VirtualHost>

社区高分解答

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

I'm expanding my comment into an answer in order to provide the detailed configuration in response to the OP's further inquiry. My recommendation is moving your config into VirtualHost blocks to allow for increased flexibility. Only the essential bits are shown here:

<VirtualHost *:80>
    ServerName default
    DocumentRoot /home/admin/web/xxx.com/public_html
    <Location />
        Require all denied
    </Location>
</VirtualHost>

<VirtualHost *:80>
    ServerName domain.com
    DocumentRoot /home/admin/web/xxx.com/public_html
</VirtualHost>

default can be any name or the server's IP address.

domain.com is the domain you want your server to respond to.

Explanation: When called with the IP address, apache elaborates the request with the default VirtualHost. The first VirtualHost defined is the default, regardless of its ServerName. Of course, setting the ServerName to the IP address also works, and will leave the default server free to be used otherwise. The location block denies every request to the whole VirtualHost.

Edit: I later saw that the question is tagged apache-2.2, while my answer uses apache-2.4 syntax. On apache-2.2 the line Require all denied must be substituted with two lines: Order deny,allow and Deny from all. Additionally, a NameVirtualHost *:80 is required before the VirtualHost definitions.

Update: The question has been updated so I think it's best to provide the configuration for apache-2.2, including all necessary lines. The same explanation as above applies.

Listen 8080
NameVirtualHost ipaddress:8080
<Directory "/home/admin/web/xxx.com/public_html">
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost ipaddress:8080>
    ServerName ipaddress
    DocumentRoot /home/admin/web/xxx.com/public_html
    <Location />
        Order deny,allow
        Deny from all
    </Location>
</VirtualHost>

<VirtualHost ipaddress:8080>
    ServerName xxx.com
    DocumentRoot /home/admin/web/xxx.com/public_html
</VirtualHost>

Comment: I agree that there are other valid solutions; however, this is how I like to organize my config files, with security restraints inside Directory or Location blocks so I know where to look for them.

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

资料来源与适用范围

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

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

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