nginx限制访问配置

  • 现在有个需求,有个接口只允许某个ip调用,但nginx是暴露在公网的
  • 这时候就需要配置nginx的限制访问了

1、知识点

  • nginx限制访问有两种方法,一种是限制ip,一种是基于用户信任登录,不过第二种用的并不是很多,今天我们只说第一种,限制ip
location ~ / {
		proxy_pass http://tomcat;
        access_log  /var/log/nginx/access.log  json;
		allow 127.0.0.1;
		deny all;
	}
  • 单站点统一限制访问项目路径,需要将以上内容直接写在 某一域名配置的server{}内
  • 多站点统一限ip,需要将以上内容直接写在server{}的外围
	location ~^/tomcat.png {
		proxy_pass http://tomcat;
        access_log  /var/log/nginx/access.log  json;
		allow 127.0.0.1;
		deny all;
	}	
  • ^~ /tomcat.png/ 以/tomcat.png/ 开头的项目路径

  • allow ip; 除了 这个ip意外

  • deny all; 限制所有的ip

  • 基于用户信任登录的方式呢,需要配置两个参数auth_basic 、auth_basic_user_file ,需要添加一个身份文件,auth_conf 文件,在此就不多阐述了,用的比较少。

2、测试

  • 接下来来改配置,我们以访问tomcat.png来测试,https://47.101.201.179/tomcat.png 在这里插入图片描述
  • 来修改https.www.flighting.top.conf
vi /etc/nginx/conf.d/https.www.flighting.top.conf
location ~^/tomcat.png {
		proxy_pass http://tomcat;
        access_log  /var/log/nginx/access.log  json;
		allow 127.0.0.1;
		deny all;
	}	
  • 只允许127.0.0.1访问tomcat.png,重启nginx后,访问应该提示403
    在这里插入图片描述
  • 访问https://47.101.201.179/tomcat.png
    在这里插入图片描述
  • 关注公众号"双城人",搬砖过程遇到的问题,大家一起探讨,资源共享在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值