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

Make Apache server only accept requests to domain rather than IP

· 52CE 我爱测

I have a CentOS server running Apache 2.2.15. If the IP address of the server is 192.0.2.231 and I write in browser http://192.0.2.231/ it goes on my website. I want to prevent this. I want my website to be accessible only on the…

问题原文

原帖链接 · antiks · CC BY-SA 4.0

I have a CentOS server running Apache 2.2.15. If the IP address of the server is 192.0.2.231 and I write in browser http://192.0.2.231/ it goes on my website.

I want to prevent this. I want my website to be accessible only on the FQDN i.e. http://example.com/.

How can I configure my server so the website is not accessible when I visit the IP address?

已采纳解答

原帖链接 · Froggiz · CC BY-SA 4.0

You can use Alias * to catch any other trafic than thoose allowed in your virtual host, for this you have to use in the last position a virtual host with * as alias.

Like that only defined domain will be served.

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

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

# /!\ THIS HAS TO BE ON THE LAST POSITION /!\
<VirtualHost *:80 *:443>
# [ Server Domain ]
ServerName localhost
ServerAlias *
# [ Cancel trafic ]
RewriteRule .* - [END,R=406]
# [ Custom Log ]
CustomLog ${APACHE_LOG_DIR}/other.log combined
</VirtualHost>

In my example only example.com and another.example.com will be allowed, all other domains or IP will have trafic cancelled.

To cancel the trafic you can use a redirect to - and then add an error code, for example i used a RewriteRule to redirect to 406 Not Acceptable (R=406).

Here you can find the list of redirect codes: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

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

资料来源与适用范围

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

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

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