配置 Apache 服务器禁止所有非法域名 访问自己的服务器

发布时间 - 2026-01-11 02:04:20    点击率:

1、http2.4.1以前:

  第一种 直接拒绝访问

打开 httpd.conf  文件,将一下配置追加到文件最后。

<pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">#直接拒绝所有非法域名
<VirtualHost *:80>
  ServerName *
  ServerAlias *
  <Location />
    Order Allow,Deny
    Deny from all
  </Location>
  ErrorLog "/alidata/log/httpd/error.log"
  CustomLog "/alidata/log/httpd/info.log" common
</VirtualHost>
</pre><pre name="code" class="html"><pre name="code" class="html">#允许的域名
<VirtualHost *:80>
  DocumentRoot /alidata/www
  ServerName www.你的域名
  ServerAlias www.你的域名
  <Directory "/alidata/www">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)-htm-(.*)$ .php?
    RewriteRule ^(.*)/simple/([a-z0-9\_]+\.html)$ /simple/index.php?
  </IfModule>
  ErrorLog "/alidata/log/httpd/error.log"
  CustomLog "/alidata/log/httpd/info.log" common
</VirtualHost>

重启apache服务:service httpd restart

第二种 跳转到指定目录或文件

打开 httpd.conf  文件,将一下配置追加到文件最后。

#所有非法域名跳转到指定目录或文件
<pre name="code" class="html"><pre name="code" class="html"><VirtualHost *:80>
#指定目录或文件
  DocumentRoot "/yun/www"
  ServerName *
  ServerAlias *
</VirtualHost>
</pre><pre name="code" class="html"><pre name="code" class="html">#允许的域名
<VirtualHost *:80>
  DocumentRoot /alidata/www/fdt
  ServerName www.fdt-art.com
  ServerAlias www.fdt-art.com
  <Directory "/alidata/www/fdt">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)-htm-(.*)$ .php?
    RewriteRule ^(.*)/simple/([a-z0-9\_]+\.html)$ /simple/index.php?
  </IfModule>
#错误日志
  ErrorLog "/alidata/log/httpd/error.log"
  CustomLog "/alidata/log/httpd/info.log" common
</VirtualHost>

重启apache服务:service httpd restart

2、http2.4.1 以后:

http2.4.1 以后不再需要NameVirtualHost以及不再支持ServerName * 这种写法。

使用ServerName * 会报Invalid ServerName “*” use ServerAlias to set multiple server names.

第一种:直接拒绝

打开 httpd.conf  在文件末尾加上一下代码:

<pre name="code" class="html"><pre name="code" class="html">#禁止所有非法域名
<VirtualHost *:80>
  ServerName 服务器ip
  ServerAlias *
  <Location />
    Order Allow,Deny
    Deny from all
  </Location>
</VirtualHost>
<pre name="code" class="html">#允许访问的域名
<VirtualHost *:80>
  DocumentRoot /alidata/www
  ServerName www.你的域名
  ServerAlias www.你的域名
  <Directory "/alidata/www">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)-htm-(.*)$ .php?
    RewriteRule ^(.*)/simple/([a-z0-9\_]+\.html)$ /simple/index.php?
  </IfModule>
#错误日志保存位置
  ErrorLog "/alidata/log/httpd/error.log"
  CustomLog "/alidata/log/httpd/info.log" common
</VirtualHost>

重启apache服务:service httpd restart

第二种:跳转到指定目录或文件

打开 httpd.conf  在文件末尾加上一下代码:

<pre name="code" class="html"><pre name="code" class="html">#禁止所有非法域名
<VirtualHost *:80>
  DocumentRoot "/alidata/www"
  ServerName 服务器ip
  ServerAlias *
  <Location /alidata/www>
    Order Allow,Deny
    Allow from all
  </Location>
</VirtualHost>
</pre>
<pre name="code" class="html">#允许访问的域名
<VirtualHost *:80>
  DocumentRoot /alidata/www/fdt
  ServerName www.fdt-art.com
  ServerAlias www.fdt-art.com
  <Directory "/alidata/www/fdt">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)-htm-(.*)$ .php?
    RewriteRule ^(.*)/simple/([a-z0-9\_]+\.html)$ /simple/index.php?
  </IfModule>
#错误日志保存位置
  ErrorLog "/alidata/log/httpd/error.log"
  CustomLog "/alidata/log/httpd/info.log" common
</VirtualHost>

重启apache服务:service httpd restart

以上所述是小编给大家介绍的配置 Apache 服务器禁止所有非法域名 访问自己的服务器,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# apache  # 服务器禁止非法域名访问服务器  # IP策略实现服务器禁止Ping  # 详解如何在阿里云服务器部署程序并用域名直接访问  # 服务器配置禁止IP直接访问只允许域名访问的实现步骤  # 重启  # 跳转到  # 会报  # 第二种  # 小编  # 第一种  # 自己的  # 在此  # 给大家  # 所述  # 给我留言  # 感谢大家  # 疑问请  # 有任何  # lt  # code  # gt  # html  # VirtualHost  # alidata 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】  如何在新浪SAE免费搭建个人博客?  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  手机网站制作与建设方案,手机网站如何建设?  香港服务器网站推广:SEO优化与外贸独立站搭建策略  七夕网站制作视频,七夕大促活动怎么报名?  Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】  如何自定义建站之星模板颜色并下载新样式?  Laravel怎么返回JSON格式数据_Laravel API资源Response响应格式化【技巧】  简单实现Android文件上传  网站优化排名时,需要考虑哪些问题呢?  音乐网站服务器如何优化API响应速度?  深圳网站制作平台,深圳市做网站好的公司有哪些?  如何在Ubuntu系统下快速搭建WordPress个人网站?  Laravel API资源类怎么用_Laravel API Resource数据转换  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  南京网站制作费用,南京远驱官方网站?  Laravel怎么使用Markdown渲染文档_Laravel将Markdown内容转HTML页面展示【实战】  如何登录建站主机?访问步骤全解析  Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法  手机软键盘弹出时影响布局的解决方法  Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives  制作公司内部网站有哪些,内网如何建网站?  javascript中对象的定义、使用以及对象和原型链操作小结  JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)  C语言设计一个闪闪的圣诞树  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  如何用美橙互联一键搭建多站合一网站?  如何快速搭建高效服务器建站系统?  Laravel中的Facade(门面)到底是什么原理  如何在宝塔面板创建新站点?  Laravel怎么集成Vue.js_Laravel Mix配置Vue开发环境  中国移动官方网站首页入口 中国移动官网网页登录  原生JS实现图片轮播切换效果  Laravel怎么使用Blade模板引擎_Laravel模板继承与Component组件复用【手册】  如何在宝塔面板中创建新站点?  Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】  如何在 Pandas 中基于一列条件计算另一列的分组均值  Laravel如何与Pusher实现实时通信?(WebSocket示例)  JS中对数组元素进行增删改移的方法总结  制作旅游网站html,怎样注册旅游网站?  Linux系统命令中screen命令详解  Laravel怎么创建自己的包(Package)_Laravel扩展包开发入门到发布  微博html5版本怎么弄发语音微博_语音录制入口及时长限制操作【教程】  如何在景安服务器上快速搭建个人网站?  如何基于PHP生成高效IDC网络公司建站源码?  如何在阿里云虚拟服务器快速搭建网站?  Mybatis 中的insertOrUpdate操作