【利用Nginx实现白名单功能】

1.只允许白名单中IP访问系统

1.1 nginx配置

http {
      include       mime.types;
      default_type  application/octet-stream;
      sendfile        on;
      #tcp_nopush     on;
      keepalive_timeout  65;
      client_body_buffer_size  720K;
      client_max_body_size 20m;
      geo $remote_addr $geo80 {
         default 0;         #0表示禁止访问
	     include /etc/nginx/white_80ip.conf;
      }
     
     server {
        listen       80;
		 server_name  localhost;
        underscores_in_headers on;	
        location / {
	        if ( $geo80 = 0 ) {
                return 403;
            }
            root   /usr/share/nginx/html/hpcoder_web;
            index  index.html index.htm;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
    }
}


1.2 白名单

white_80ip.conf 内容

allow 192.168.2.101
allow 192.168.2.102

2. 只允许白名单中IP访问指定端口

2.1 nginx 配置

stream {
        log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

        access_log /var/log/nginx/tcp-access.log proxy ;
        open_log_file_cache off;
        include /etc/nginx/conf.d/*.stream;

        upstream p1{

            server 192.168.18.23:17000;
        }
        
        upstream p2{

            server 192.168.18.20:22;
        }

        server{
            listen 17000;
            include /etc/nginx/p1.conf;
            deny all;
            proxy_pass p1;
            proxy_connect_timeout 1h;
            proxy_timeout 1h;

        }
        server{
            listen 17222;
            #include /etc/nginx/p1.conf;
            #deny all;
            proxy_pass p2;
            proxy_connect_timeout 1h;
            proxy_timeout 1h;

        }

}

2.2 白名单

p1.conf

allow 192.168.18.2
allow 192.168.18.5

2.3 应用场景

代理UDP端口、许可管理…

3. 执行脚本

3.1 脚本内容

#!/bin/bash

function usage()
{
printf "Usage $0 options:
        options:
            add:    增加指定ip到指定端口 
            del:    删除指定ip到指定端口
        Eg:
            增加: $0 add    software_name add_ip
            删除:$0 del    software_name del_ip
"
}

file=/etc/nginx/${2}.conf

if [ ! -f ${file} ];then
        touch ${file}
fi

if [ $# -eq 0 ];then
        usage
        exit 1
fi

case $1 in
add)
        ip=$3
        if [ $# -ne 3 ];then
                usage
                exit 1
        else
                sed -i "/^allow $ip/d" $file
                echo "allow ${ip};" >>$file
        fi
;;
del)
        ip=$3
        if [ $# -ne 3 ];then
                usage
                exit 1
        else
                sed -i "/^allow $ip/d" $file
        fi
;;
*)
        echo "Usage: $0 add || del"
        exit 1
esac

service  nginx reload >>/dev/null

3.2 调用方式

添加

./write.sh add p1 192.168.6.2

删除

./write.sh del p1 192.168.6.3

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wang_xiaoxin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值