前言-刚刚发布一个demo一直被一个叼毛攻击,不停的扫描端口,弄得接口一访问就是503错误

99元买了一个2核2g的服务器,1元钱买了一个服务器,有个人一直扫描我的端口

安装一个 nginx

dnf install nginx

安装完成后启动nginx服务

# 启动
systemctl start nginx 
 
# 设置开机自启动
systemctl enable nginx
 
# 重启
systemctl restart nginx
 
# 查看状态
systemctl status nginx
 
# 停止服务
systemctl stop nginx

打开etc\nginx\nginx.conf文件

 server {
        listen       80;                    #监听端口
        listen       [::]:80;
        server_name  192.168.100.10;        #服务器地址 域名或者ip
       # root         /usr/share/nginx/html;
        
    location / {
        # 检查请求的 URI 是否包含 "home"(不区分大小写)
        if ($request_uri ~* (?i:home)) {
            # 将原始 URI 反向代理到内部网站
            set $target_uri $request_uri;
            proxy_pass http://127.0.0.1:5000;
            break; # 跳出 if 语句
        }
    }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        error_page 404 /404.html;
        location = /404.html {
        }
 
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

 

webapi程序program里面改一下

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
//    app.UseSwagger();
//    app.UseSwaggerUI();
//}
 
app.UseAuthorization();

app.MapControllers();

app.Use(async (context, next) =>
{
    var requestedUrl = context.Request.Path.Value;

    if (!IsValidUrl(requestedUrl))
    { 
        if (requestedUrl.Contains(".html"))
        {
            logger.Debug($"{GetClientIp(context)}--{context.Request.Path}");
        }
        context.Response.StatusCode = StatusCodes.Status400BadRequest;
        await context.Response.WriteAsync(" ");
        return;
    }

    await next();
});
 
app.Run();

bool IsValidUrl(string url)
{
    // 这里可以实现你的URL验证逻辑
    // 例如,只允许某些路径
    var allowedUrls = new[] {"/home" };
    return allowedUrls.Contains(url.ToLower());
}


 string GetClientIp(HttpContext context)
{
    // 尝试从 X-Forwarded-For 头获取 IP 地址
    string ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();

    if (string.IsNullOrEmpty(ip))
    {
        // 如果 X-Forwarded-For 头为空,则获取直接连接的 IP 地址
        ip = context.Connection.RemoteIpAddress.ToString();
    }

    return ip;
}
如果前面前面的nginx被功能0进入到这里,直接记录ip
然后在管理后台限制ip访问

把允许改成拒绝,把掉毛的ip加进去

如果还是大量攻击那就上高防服务器,那种服务器打不死

我的服务器99元买的为什么非要打我,日了狗

 
posted on 2024-07-06 20:02  啊修  阅读(1)  评论(0编辑  收藏  举报