Nginx或Apache禁止某些IP段访问的两种方法

宝塔面板Nginx与Apache禁止IP直接访问的配置指南 Web服务器安全防护是运维工作的核心环节,其中IP访问控制能有效防止恶意扫描和未授权访问。通过Nginx或Apache的server_name匹配机制,结合HTTP协议Host头验证,可实现精准的流量过滤。在宝塔面板环境中,这种技术既保障了多站点托管时的隔离性,又能规避爬虫攻击等安全风险。典型应用场景包括:防止品牌形象受损、绕过WAF防护的渗透攻击防御等。本文以Nginx的403返回和Apache的Require指令为例,详解如何通过修改配置文件实现IP限制,同时介绍防火墙层方案与fail2ban动态防护的 阅读详情


修改Nginx配置文件nginx.conf
Nginx配置访问IP可以修改nginx.conf文件,只需要在server中添加allow和deny的IP即可,如下:

server {
    listen       80;
    server_name  localhost;
    allow all;
    deny 123.123.123.123;
    error_page   500 502 503 504  /50x.html;
}
 
注意!上面的配置中allow必须在deny的前面配置,不然allow不生效。

Nginx中允许或者拒绝某个IP访问是这么写的:

allow 192.168.0.1;
deny  192.168.0.2;
 
如果你需要对整个IP段设置不允许访问,可以这么写:

deny 123.0.0.0/8;    // 封 123.0.0.1~123.255.255.254 这个段的ip
deny 123.123.0.0/16;   // 封 123.123.0.1~123.123.255.254 这个段的ip
deny 123.123.123.0/24;   // 封 123.123.123.1~123.123.123.254 这个段的ip
deny all;  // 封所有ip
 
说明,allow跟deny配置相同,如果需要开放某个IP段,只需要把上面的deny改成allow。

另外需要注意,该配置修改需要重启Nginx服务器才能生效。

修改网站配置文件.htaccess
除了修改Nginx配置文件之外,还可以通过修改网站配置文件.htaccess来限制某IP(段)访问,方法如下。

将以下内容保存为.htaccess文件,上传至网站根目录。如果原来已经存在.htaccess文件的,将以下内容加到该文件开头。

Order allow,deny
allow from all
deny from 123.123.123.123
 
如果要限制一个IP段,比如要限制123.123.123.*这个C段,则输入:

Order allow,deny
allow from all
deny from 123.123.123
 
以上内容表示拒绝123.123.123开头的所有IP,其他IP允许。

如果要限制多个IP段,中间用空格隔开,如:

Order allow,deny
allow from all
deny from 111.111.111 111.222.222 111.333.333
 
说明,该配置不用重启web服务器,修改立即生效。

此外,该方法适用任何web网站,例如IIS、Apache网站。 


 

禁止使用服务器 IP 访问 Apache 环境下的 Web 站点 通过在 Apache 的配置文件中添加适当的指令,我们可以轻松地禁止使用服务器 IP 地址访问我们的 Web 站点。在 Apache 环境下,有时我们希望禁止用户使用服务器的 IP 地址直接访问我们的 Web 站点,而只允许使用域名进行访问。现在,当用户尝试使用服务器的 IP 地址来访问你的网站时,他们将会被拒绝访问,只有通过域名才能正常访问。为了实现这个目标,我们可以通过 Apache 的配置文件进行相应的设置。表示禁止对根目录的访问权限,即禁止使用服务器的 IP 地址进行访问。 阅读详情

相关推荐

apache rewrite规则实现白名单

今天弄了半天apache 的rewrite 规则,还是没有配置出符合的规则。后一同事,研究了半个小时弄出来,很是惭愧。

【教程】apache设置禁止通过IP访问,只能域名访问

真的还是很必要的!

xfxuezhang.cn 5737

记录Nvidia AGX Orin的配置日常(二)设置上电自动开机和远程桌面

因为要拿AGX作为机器人的主控板,所以需要给他设置一下通电后无需按电源键就自动开机,然后再配置一下远程桌面。

AutomaticForever的博客 1349

apache限制ip去进行访问80和443端口

1.因为如果用ip去进行访问的话,会造成恶意攻击,所以为了避免出现这种情况,我们对apache进行限制。 在虚拟主机下面添加这一段,就可以实现禁止ip访问网站 <VirtualHost *:80> ServerName ip <Location /> Order Allow,Deny Deny from all </Lo...

AZXHNLS81的博客 1906

硬件基础(4):(7)ADC电压采集:软件校准(标定、拟合)

在实际项目中,**软件端校准**可以有效弥补硬件方面(分压电阻误差、放大器失调、参考源误差等)带来的系统性偏差,从而提高 ADC 测量精度。下面是一个常见的**多点实测校准**流程与思路,个人感觉传感器标定的原理也只这样。

FCZlll的博客 6226

Qcom camera hal简介

Qcom camera hal简介

weixin_42136255的博客 7757

Apache 配置禁止IP地址访问,只允许使用域名的方式访问

配置过程 提前安装好httpd服务器

qq_45631844的博客 6948

基于STM32设计自动输液监测系统(241)

该系统集成了液位传感模块、光电传感器、电机模块、按键模块和OLED显示屏等多个功能模块,以实现全面的输液过程监控和控制。液位传感模块能够实时监测输液瓶中的液位,当液位到达限定值时,系统会自动控制步进电机夹紧输液管,并发出报警提醒,确保输液过程不会因液位过低而中断。

1334

单个html网页禁止某ip段访问,Nginx禁止某IP(段)访问的两种方法

当你的Nginx服务器想禁止某个IP或IP段访问时,可以通过配置文件来达到目的,本文介绍两种常用的方法。Nginx禁止某IP(段)访问方法一:修改Nginx配置文件nginx.confNginx配置访问IP可以修改nginx.conf文件,只需要在server中添加allow和deny的IP即可,如下:server{listen80;server_namelocalhost;a...

weixin_29970399的博客 863

Python 爬虫实战:爬取 GitHub 热门项目(星数 / 语言 / 作者)

本文系统介绍了使用Python爬虫技术获取GitHub热门项目数据的完整方案。通过分析GitHub Trending页面和官方API两种数据源,详细讲解了HTML解析、JSON数据处理、反爬应对策略等关键技术。采用requests、BeautifulSoup、pandas等工具链,实现了项目信息(名称、星数、语言等)的结构化爬取,并配套数据清洗、统计分析及可视化功能。特别强调遵守GitHub API使用规范,包括Token认证、请求频率控制等合规要求。该方案为开发者提供了一种高效、合法的数据采集方法,适用于

2503_91057718的博客 2333

YOLOv8中的可变形卷积改进 | 从DCNv1到DCNv3的多位置替换可变形卷积保姆级实操

可变形卷积(Deformable Convolution)最早由Dai等人在2017年提出,其核心思想是在标准卷积操作的基础上,引入可学习的偏移量(offsets),使卷积核能够自适应调整其采样位置,从而增强模型对目标变形的感知能力。return x本文通过深入探讨可变形卷积在YOLOv8中的应用,详细介绍了DCNv1、DCNv2和DCNv3的原理和实现,并通过实验验证了其在目标检测任务中的有效性。未来,我们将继续探索更多优化策略和技术,提升YOLOv8模型的性能,以应对更复杂的目标检测任务。

一键难忘的博客 4040

禁止通过ip直接访问网站来防止恶意解析-(apache,nginx,iis)

一、IIS 禁止IP直接访问 首先就是打开IIS管理器之后,在IIS管理器左侧找到网站下面的相应网站,右键选择打开该网站属性,进入相关IIS网站属性设置。  (IIS打开网站属性方法)在网站属性窗口中的默认“网站”选项卡中找到并单击“高级”,如下图所示。  (IIS打开网站高级网站标识)在高级网站标识中,一般默认主机头值是空的。如果想让别人直接输入IP地址访问网站

友声网博客 1万+

路由三大分类:直连路由、静态路由、动态路由

直连路由、静态路由和动态路由各有其独特的特点和适用场景。直连路由适用于简单的直接连接网络,静态路由提供了精确的路径控制,而动态路由则在大型复杂网络中展现出强大的适应性和自动化能力。了解和掌握这三种路由类型的配置和管理,对于网络管理员来说,是确保网络高效、安全运行的重要技能。

网络技术联盟站 4690

php禁止代理ip访问_php禁止某ip或ip地址段访问的方法

本文实例讲述了php禁止某ip或ip地址段访问的方法,分享给大家供大家参考。具体分析如下:因为还没有深入学习Apache和nginx 所以用PHP写了一个可以禁用地址的小程序使用时只需:即可禁用单个ip如下://禁用ip地址$ip=$_SERVER["REMOTE_ADDR"];$ban=file_get_contents("ban.dat");if(stripos($ban,$ip)){die(...

weixin_39849153的博客 169

Ubuntu 系统下安装 OpenClaw(原 Clawdbot)的详细指南

Ubuntu 系统下安装 OpenClaw(原 Clawdbot)的详细指南

Science prince的博客 3531

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

几种不同的方式禁止IP访问网站(PHP、Nginx、Apache设置方法)

几种不同的方式禁止IP访问网站(PHP、Nginx、Apache设置方法)几种不同的方式禁止IP访问网站(PHP、Nginx、Apache设置方法)

阿酷tony 1939

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

web服务禁止 ip 直接访问: apache/httpd/nginx

随着这一段时间政府加大扫黄打非的力度,互联网上似乎正在被重新洗涤;而越来越严格的管控,姑且不论是否一刀切,至少我们看到了政府对黄这个问题的严重性的关注。创建一个文明的时代,正在成为越来越多网民以及网民相关人员的期待和迫切要求。 (原文链接 http://ddbiz.com/?p=193) 跑题了 :-)   昨日IDC机房通知,所有web服务器不可以直接通过ip地址进行访问web服务,必须

钱可通神 2765

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

linux禁止IP访问PHP代码,禁止IP访问网站的各种方法(linux,php,nginx,apache)

PHP禁止某个IP或IP段访问废话不多说,客官请看:代码如下//禁止某个IP$banned_ip = array ("127.0.0.1",//"119.6.20.66","192.168.1.4");if ( in_array( getenv("REMOTE_ADDR"), $banned_ip ) ){die ("您的IP禁止访问!");}//禁止某个IP段$ban_range_low=ip2...

weixin_35339134的博客 379

301跳转-Apache和Nginx输入ip跳转到域名--输入ip禁止访问

一 域名解析 在你的域名管理里面对域名进行解析 xxx.com指向你的主机ip地址 记录类型为@ www.xxx.com 指向你的主机ip地址 记录类型为A 二 Apache 配置 1 修改httpd.conf文件 去掉#打开重定向模块 LoadModule rewrite_module modules/mod_rewrite.so 打开 # Virtual ...

tangbin0505的博客 5276

Apache和Nginx下禁止访问特定的目录或文件

例如:http://www.domain.com/test/a.txt,此时a.txt里的内容会全部暴露在外面了(有时这个txt是很机密的文件),这样以来就不安全了。 同样:我在Nginx配置后后也存在这样的问题,Apache下此问题的解决多谢NetSeek帮助。 #不解析PHP文件,但是提示下载文件 #php_flag engine off #A...

224

常见几个方式禁止IP访问网站(PHP、Nginx、Apache不同设置方法)

常见几个方式禁止IP访问网站(PHP、Nginx、Apache不同设置方法)

shoujoai的博客 2335

php禁止某个IP,PHP_php禁止某ip或ip地址段访问的方法,本文实例讲述了php禁止某ip或ip - phpStudy...

php禁止某ip或ip地址段访问的方法本文实例讲述了php禁止某ip或ip地址段访问的方法,分享给大家供大家参考。具体分析如下:因为还没有深入学习Apache和nginx 所以用PHP写了一个可以禁用地址的小程序使用时只需:即可禁用单个ip如下://禁用ip地址$ip=$_SERVER["REMOTE_ADDR"];$ban=file_get_contents("ban.dat");if(stri...

weixin_29285605的博客 510

apache禁止访问目录或文件

大家是否测试Apache做了目录禁止浏览后,目录下面的txt文件还是可以显示里面的内容的。 例如:http://www.domain.com/test/此访问会报403错误,但是如果test下有很多txt,你访问该txt时; 例如:http://www.domain.com/test/a.txt,此时a.txt里的内容会全部暴露在外面了(有时这个txt是很机密的文件),这样以来就不安全了。

yunliupan的专栏 1809

nginx禁止直接访问php,禁止IP访问网站的多种方法分享(linux,php,nginx,apache)

PHP禁止某个IP或IP段访问废话不多说,客官请看:?apache禁止使用IP访问方法一:在httpd.conf文件最后面,加入以下代码?说明:蓝色部分是实现拒绝直接通过221.*.*.*这个IP的任何访问请求,这时如果你用221.*.*.*访问,会提示拒绝访问。红色部分就是允许通过www.zzvips.com这个域名访问,主目录指向c:/web(这里假设你的网站的根目录是c:/web)Linux...

weixin_42186579的博客 1130

php访问禁止,php如何禁止某ip或ip地址段访问

这篇文章主要介绍了php禁止某ip或ip地址段访问的方法,涉及服务器变量$_SERVER、文件及字符串的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下本文实例讲述了php禁止某ip或ip地址段访问的方法,具体分析如下:因为还没有深入学习Apache和nginx 所以用PHP写了一个可以禁用地址的小程序使用时只需:即可禁用单个ip如下:...

weixin_36383576的博客 424
上一篇: 正则表达式详解
下一篇: centos6.10环境下安装php7.4(基于WLNMP包,php-redis扩展手动安装)
abiao1981
博客等级 码龄19年 176粉丝 138原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abiao1981

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值