VPS 搭建代理服务器指南
utils
本文字数:4.7k 字 | 阅读时长 ≈ 21 min

VPS 搭建代理服务器指南

utils
本文字数:4.7k 字 | 阅读时长 ≈ 21 min

搭建 V2Ray 服务器 (ws+tls+web)

本文是搭建🪜的完整教程,此外附带一个网上的一个做法:v2ray-ws-tls-web。相比传统的 SS、SSR 等方案,V2Ray + ws + tls + web 更稳定且更安全,用 TLS 加密保护且流量特征和普通网站无异

注意本文不适用于纯小白,最好是有一些 vps 以及域名购买和使用经验

1. 工作原理

1.1 传统代理方案的问题

传统的 V2Ray/SS 代理方案存在一个明显的缺陷:服务器直接暴露在公网上,容易被主动探测识别。如图所示,当防火墙对可疑 IP 进行主动探测时,可以直接发现代理服务的存在。

// 传统架构
Client --> GFW --> V2Ray/SS --> Internet
            |         ^
            |         |
            +-- Probe-+

1.2 改进后的架构

通过添加 HTTPS 服务器作为前置代理,我们可以:

这种架构极大地提高了服务的隐蔽性和安全性,是目前最推荐的部署方案。在配置了真实网站之后,只有你自己知道这是个代理服务器,在外界看来就是一个普通的网站。这里的"HTTPS 流量"实际上是"TLS 流量",就是普通的加密网页浏览流量。

// 改进架构
Client --> GFW --> HTTPS --> V2Ray --> Internet
            |       ^
            |       |
            +-Probe-+

如果服务器之前运行过 SS、SSR、V2Ray(非 TLS)等代理,请务必先停止并清理这些旧服务,或者直接在控制面板里重装系统。因为翻墙的隐蔽性取决于最薄弱的一环,如果同一台服务器上还跑着其他容易被探测的代理软件,就可能连带暴露 V2Ray+ws+tls+web 的流量特征。

2. 域名设置

先在域名服务商购买一个域名,并用 Cloudflare 做域名解析,方便之后一键切换到 v2ray+ws+tls+web+cdn 模式,应对连接异常或 IP 被封等情况。虽然走 CDN 会稍慢一些,但稳定性更高。Cloudflare 是全球最大的 CDN 提供商,约半数网站都在使用,其注册和使用在隐私与安全方面总体是可靠的。

2.1 域名托管(域名购买商处修改)

Cloudflare 域名托管步骤:先注册并登录 Cloudflare,添加你的域名后,Cloudflare 会分配两个新的域名服务器(nameserver)地址。然后登录你的域名注册商后台,把原来的 nameserver 改成 Cloudflare 提供的这两个地址,保存后等待大约 24–48 小时,域名托管就会完成。

2.2 域名地址解析(Cloudflare 处修改)

在 Cloudflare 控制面板中配置域名解析:

  1. 进入 DNS 设置页面,点击【Add record】添加解析记录

  2. 需要添加两条 A 记录:

    • 第一条: Name=www, Address=你的服务器IP, TTL=1小时
    • 第二条: Name=@, Address=你的服务器IP, TTL=1小时
  3. 重要: 确保记录旁边的云朵图标为灰色(DNS only),不要启用 CDN

  4. 验证配置: ping your-domain.com,如果能 ping 通并且 ip 正确,说明解析配置成功

名词解释:

  • A 记录: 将域名指向 IPv4 地址
  • www: 解析带 www 的域名 (www.domain.com)
  • @: 解析裸域名 (domain.com)
  • TTL(Time To Live): DNS 缓存时间,建议设置 1 小时或 Auto

图 1: DNS设置界面

3. VPS 服务器购买

购买一个海外的 VPS 服务器,这里不赘述

4. v2ray 使用

4.1 安装脚本

# 安装 V2Ray
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

# 常用管理命令
systemctl start v2ray     # 启动服务
systemctl stop v2ray      # 停止服务
systemctl restart v2ray   # 重启服务
systemctl status v2ray    # 查看状态

4.2 配置流程

V2Ray 的主配置文件位于 /usr/local/etc/v2ray/config.json。id 是你的 v2ray 的 uuid,可以使用 v2ray uuid 生成,或者 linux 命令 uuidgen or cat /proc/sys/kernel/random/uuid

{
    "inbound": {
        "protocol": "vmess",
        "listen": "127.0.0.1",
        "port": =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=,
        "settings": {
            "clients": [
                {
                    "id": "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=",
                    "alterID":0
                }
            ]
        },
        "streamSettings": {
            "network": "ws",
            "security": "none",
            "wsSettings": {
                "path": "/=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
            }
        }
    },
    "outbound": {
        "protocol": "freedom",
        "settings": {}
    }
}

结束后重启 v2ray 即可,systemctl restart v2ray

4.3 证书申请

# 1. 安装必要工具
curl https://get.acme.sh | sh
apt install socat

# 2. 配置 acme.sh
ln -s /root/.acme.sh/acme.sh /usr/local/bin/acme.sh
acme.sh --set-default-ca --server letsencrypt

# 3. 申请证书
# 如果有 www 子域名,使用:
acme.sh --issue -d your_domain -d www.your_domain --standalone -k ec-256
# 如果只有主域名,使用:
acme.sh --issue -d your_domain --standalone -k ec-256

# 4. 安装证书到 V2Ray 目录
acme.sh --installcert -d your_domain --ecc --key-file /usr/local/etc/v2ray/server.key --fullchain-file /usr/local/etc/v2ray/server.crt

5. Nginx 配置

安装 apt install nginx。在 /etc/nginx/sites-available 文件夹下新建一个 your_domain 文件,然后将下面内容复制进去,其中 server_name 中填写 your_domain www.your_domainlocation /xxx的路径改为你上面生成的路径即可,他会自动定位到 v2ray 的代理中,只要这个路径不暴露,墙就无法发现你

server {
    server_name =-=-=-=-=-=-=-=-=-=your_domain www.your_domain-=-=-=-=-=-=-=-=-=-=-=;
    listen 80;  # http 80 端口
    # 将所有 HTTP 请求重定向到 HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    server_name =-=-=-=-=-=-=-=-=-=your_domain www.your_domain-=-=-=-=-=-=-=-=-=-=-=;
    listen 443 ssl http2;  # https 443 端口
    charset utf-8;

    # SSL 证书配置
    ssl_certificate /usr/local/etc/v2ray/server.crt;
    ssl_certificate_key /usr/local/etc/v2ray/server.key;

    # 优化的 SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    
    # SSL 会话配置
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 10s;

    # 安全性配置
    add_header X-Frame-Options "DENY" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Content-Security-Policy "default-src * data: blob: 'unsafe-inline' 'unsafe-eval';" always;

    # 限制请求方法
    if ($request_method !~ ^(GET|POST)$) {
        return 405;
    }

    # v2ray 路径优先匹配
    location /=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= {
        proxy_redirect off;
        proxy_pass http://127.0.0.1:=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # Bing 反向代理作为默认路由
    location / {
        proxy_pass https://www.bing.com;
        proxy_set_header Host "www.bing.com";
        
        # 处理动态请求
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 处理 Cookie
        proxy_cookie_domain bing.com $server_name;
        proxy_cookie_domain www.bing.com $server_name;
        proxy_cookie_path / "/";
        
        # 更全面的子域名替换
        sub_filter_once off;
        sub_filter_types *;  # 处理所有内容类型
        sub_filter "https://www.bing.com" "https://$server_name";
        sub_filter "https://bing.com" "https://$server_name";
        sub_filter "//www.bing.com" "//$server_name";
        sub_filter "//bing.com" "//$server_name";
        
        # 处理重定向
        proxy_redirect https://www.bing.com/ https://$server_name/;
        proxy_redirect https://bing.com/ https://$server_name/;
        
        # 确保正确处理 WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        # 其他设置保持不变...
        proxy_ssl_server_name on;
        proxy_ssl_protocols TLSv1.2 TLSv1.3;
        proxy_ssl_verify off;
        proxy_buffer_size 16k;
        proxy_buffers 8 16k;
        proxy_busy_buffers_size 32k;
    }

    # 静态文件配置
    root /usr/share/nginx/html;
    index index.html index.htm index.php;
    
    # 基础安全配置
    autoindex off;
    server_tokens off;
}

然后将这个文件链接到 sites-enabled 中,sudo ln -s /etc/nginx/sites-available/your_domain_file /etc/nginx/sites-enabled/your_domain_file

6. 配置客户端

v2ray 客户端配置

v2ray配置

surge 客户端配置

v2ray配置

7. 安全性与性能优化

7.1 配置防火墙

如果 VPS 上没有其它服务,建议开启防火墙以提高安全性。服务器只需对外暴露必要的端口(80、443、SSH),可以有效降低被探测的风险。

# 安装 ufw
apt install ufw   # Ubuntu/Debian

# 配置防火墙规则
ufw disable  # 先禁用防火墙
ufw allow ssh  # 允许 SSH(默认端口 22)
ufw allow http  # 允许 HTTP
ufw allow https  # 允许 HTTPS
ufw enable  # 启用防火墙

# 如果使用自定义 SSH 端口(如 14320)
ufw allow port  # 替换为你的实际 SSH 端口

注:ufw 和 firewalld 都基于 Linux iptables,功能本质上相同。选择其一即可。

7.2 BBR 加速配置

BBR 是 Google 开发的 TCP 拥塞控制算法,可以显著提升网络性能。要使用 BBR,需要 Linux 内核版本 ≥ 4.10。

  1. 检查内核版本:
uname -a
  1. 如果内核版本满足要求,执行以下命令开启 BBR:
# 添加 BBR 配置
bash -c 'echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf'
bash -c 'echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf'
sysctl -p

# 验证 BBR 是否启用
sysctl net.ipv4.tcp_congestion_control

如果输出显示 net.ipv4.tcp_congestion_control = bbr,则表示 BBR 已成功启用。

通过以上配置,我们实现了:

  1. 最小化暴露端口,提高服务器安全性
  2. 启用 BBR 加速,优化网络性能

8. 故障排查

如果设置了不能访问外网,多半是防火墙的问题,如果用的是 ufw 或 firewalld 防火墙,确保开放了必要的端口: - 80 (HTTP) - 443 (HTTPS) - 你的 V2Ray 端口

ufw status # 检查 UFW 防火墙状态
firewall-cmd --list-ports # 检查 firewalld 防火墙状态

开启端口示例:

sudo ufw allow 8080/tcp  # 开启 TCP 端口
sudo ufw allow 8080 # 如果你需要同时开启 TCP 和 UDP
sudo ufw delete allow 8080/tcp # 关闭端口

firewall-cmd --zone=public --add-port=8080/tcp --permanent # 打开某端口
firewall-cmd --reload # 重新加载防火墙才能生效

注意:服务器可能使用 UFW 或 firewalld 其中之一作为防火墙,使用对应的命令即可。

如果发现服务器 IP 状态异常或无法访问,应先确认是否存在封锁或网络限制等问题。遇到此类情况时,可以考虑更换新的服务器 IP、启用 Cloudflare CDN 来缓解访问不稳定的问题,或调整服务端口以排查是否为端口级别的阻断。

如果之前能科学上网,过了一段时间又上不去了,可以先看看服务器能不能登录,如果可以的话,再看看登录一下域名网站,看看如果提示网站不安全,应该是证书到期了,只需要重新更新一下证书即可

➜ v2ray acme.sh --renew -d mychat.cc -d www.mychat.cc --ecc                                                                                 
[Thu Oct  2 21:42:05 EDT 2025] Renewing: 'mychat.cc'
[Thu Oct  2 21:42:05 EDT 2025] Renewing using Le_API=https://acme-v02.api.letsencrypt.org/directory
[Thu Oct  2 21:42:05 EDT 2025] Using CA: https://acme-v02.api.letsencrypt.org/directory
[Thu Oct  2 21:42:05 EDT 2025] Standalone mode.
[Thu Oct  2 21:42:05 EDT 2025] LISTEN   0         128                 0.0.0.0:80               0.0.0.0:*        users:(("nginx",pid=520,fd=8),("nginx",pid=519,fd=8))                          
LISTEN   0         128                    [::]:80                  [::]:*        users:(("nginx",pid=520,fd=9),("nginx",pid=519,fd=9))                          
[Thu Oct  2 21:42:05 EDT 2025] tcp port 80 is already used by (("nginx",pid=520,fd=8),("nginx",pid=519,fd=8))                          
80                  [
[Thu Oct  2 21:42:05 EDT 2025] Please stop it first
[Thu Oct  2 21:42:05 EDT 2025] _on_before_issue.

这里 nginx 占用了 80 端口,所以需要先停止 nginx

➜ v2ray nginx -s stop
nginx: [warn] "ssl_stapling" ignored, host not found in OCSP responder "e5.o.lencr.org" in the certificate "/usr/local/etc/v2ray/server.crt"
➜ v2ray acme.sh --renew -d mychat.cc -d www.mychat.cc --ecc
[Thu Oct  2 21:47:41 EDT 2025] Renewing: 'mychat.cc'
-----BEGIN CERTIFICATE-----
Sw==
-----END CERTIFICATE-----
[Thu Oct  2 21:47:57 EDT 2025] Your cert is in: /root/.acme.sh/mychat.cc_ecc/mychat.cc.cer
[Thu Oct  2 21:47:57 EDT 2025] Your cert key is in: /root/.acme.sh/mychat.cc_ecc/mychat.cc.key
[Thu Oct  2 21:47:57 EDT 2025] The intermediate CA cert is in: /root/.acme.sh/mychat.cc_ecc/ca.cer
[Thu Oct  2 21:47:57 EDT 2025] And the full-chain cert is in: /root/.acme.sh/mychat.cc_ecc/fullchain.cer
[Thu Oct  2 21:47:57 EDT 2025] Installing key to: /usr/local/etc/v2ray/server.key
[Thu Oct  2 21:47:57 EDT 2025] Installing full chain to: /usr/local/etc/v2ray/server.crt

这里安装成功了,然后重启一下 nginx 和 v2ray 即可

➜ v2ray systemctl restart nginx  # 重启
➜ v2ray systemctl restart v2ray   # 重启服务

这时候登录一下网站就不会提示不安全了,如果还是不行,可以清除一下浏览器缓存,然后就可以了

9. 流量异常与爬虫拦截

博主在翻墙之后,经常服务商提醒我流量不够用了,如果只是浏览网页的话,流量一个月 1T 是肯定用不完的,检查发现在未连接代理的情况下,服务器依然有 200KB/s - 1MB/s 的持续上传流量,有两个原因:一个是 V2Ray 被盗用(可能是较低),第二是因为**伪装网站被搜索引擎爬虫盯上了* (V2Ray 的 ws+tls+web 模式通常会伪装成 Bing 搜索或新闻网站,这对于 OpenAI (ChatGPT)、Amazon、Google 等公司的爬虫机器人极具吸引力,它们会频繁抓取你的站点数据,消耗大量带宽)

9.1 现象确认

使用 nethogs 查看实时流量,发现是 nginx 进程在大量占用带宽:

sudo nethogs
# 结果显示 nginx: worker process 占用大量 SENT 流量

worker 有异常流量

正常来讲,在 V2Ray (WS + TLS + Web) 这种架构下,你在看视频、下载文件时,V2Ray 从外网拉多少数据,Nginx 基本就要往你的设备送多少数据,所以在 NetHogs 里,V2Ray 的 Received 和 Nginx 的 Sent 理论上应该比较接近。如果只有你自己在用这个服务,两者流量大致相当是比较正常的。截图里 Nginx 的流量远远高于 V2Ray,说明有相当一部分流量并不是通过 V2Ray 出来的,而是 Nginx 直接把本地网页内容返回给别的访问者。这通常就是一些爬虫或扫描程序在直接访问你的伪装站点,导致 Nginx 的数据暴涨,而 V2Ray 只占其中一小部分——所以这种“不对称”本身就侧面证明了爬虫流量的存在。

此外也可以查看 Nginx 实时访问日志,寻找流量来源:

➜ ~ tail -f /var/log/nginx/access.log

74.7.241.15 - - [02/Dec/2025:17:25:56 +0800] "GET /videos/riverview/relatedvideo?FORM=VRDGAR&mid=73664D74EE97AB18684373664D74EE97AB186843&mmscn=stvo&q=%2bWww.www.www.www.youtubec+Com.org.net.edu&qft=+filterui%3avideoage-lt10080+filterui%3aresolution-480p+filterui%3asite-vimeo.com+filterui%3aduration-short HTTP/2.0" 200 51042 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"
74.7.242.63 - - [02/Dec/2025:17:25:56 +0800] "GET /videos/search?FORM=RESTAB&q=Https+Www.Bing.com+Maps&qft=%2bfilterui%3aduration-medium%2bfilterui%3avideoage-lt10080%2bfilterui%3asite-mtv.com%2bfilterui%3aprice-paid%2bfilterui%3aresolution-480p HTTP/2.0" 200 53307 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"
74.7.242.63 - - [02/Dec/2025:17:25:57 +0800] "GET /videos/search?FORM=RESTAB&q=Www.www.www.www.youtubec+Com.org.net.edu&qft=%2bfilterui%3aduration-short%2bfilterui%3avideoage-lt10080%2bfilterui%3asite-vevo.com%2bfilterui%3aprice-paid%2bfilterui%3aresolution-480p HTTP/2.0" 200 46608 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"
74.7.241.15 - - [02/Dec/2025:17:25:57 +0800] "GET /videos/riverview/relatedvideo?FORM=VRDGAR&mid=1A3BE4443BF9914FA3931A3BE4443BF9914FA393&mmscn=stvo&q=%2bWww.www.www.www.youtubec+Com.org.net.edu&qft=+filterui%3avideoage-lt43200+filterui%3aresolution-360p+filterui%3asite-cbsnews.com+filterui%3aprice-free HTTP/2.0" 200 50990 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"

如果看到大量状态码为 200 的请求,且 User-Agent 中包含 GPTBot 等字眼,说明是爬虫在抓取

**解决方案:**我们可以通过修改 Nginx 配置文件,直接拒绝这些爬虫的访问请求(返回 403)。

  1. 编辑 Nginx 配置文件
    找到你的站点配置文件(通常是 /etc/nginx/sites-available/你的域名):

  2. 添加拦截规则
    在监听 443 端口的 server 块中(listen 443 ssl 下方),添加以下拦截代码:

    server {
        server_name your_domain.com;
        listen 443 ssl http2;
        
        # ... 其他 SSL 配置 ...
        # =========== 添加以下拦截代码 ===========
        # 增强版:拦截 AI 爬虫、SEO 工具、云服务商扫描器
        if ($http_user_agent ~* (GPTBot|Amazonbot|bingbot|Googlebot|YandexBot|Bytespider|DotBot|SemrushBot|AhrefsBot|ClaudeBot|CCBot|Applebot|PetalBot|MJ12bot|CensysInspect|InternetMeasurement|Barkrowler)) {
            return 403;
        }
        # ======================================
        # ... 原有的 location 配置 ...
    }
    
  3. 重启 Nginx:sudo nginx -t sudo systemctl restart nginx

9.2 验证效果

再次查看日志:

➜ ~ tail -f /var/log/nginx/access.log
74.7.241.15 - - [02/Dec/2025:17:33:14 +0800] "GET /videos/riverview/relatedvideo?FORM=VRDGAR&mid=28245542D5FF09EBC8C628245542D5FF09EBC8C6&mmscn=stvo&q=%2bWww.www.www.www.youtubec+Com.org.net.edu&qft=+filterui%3avideoage-lt525600+filterui%3aresolution-480p+filterui%3asite-hulu.com+filterui%3aduration-long HTTP/2.0" 403 123 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"
74.7.242.63 - - [02/Dec/2025:17:33:19 +0800] "GET /videos/search?FORM=VRFLTR&q=Www.www.www.www.op+Com.org.net.edu&qft=+filterui:duration-long+filterui:resolution-720p+filterui:site-myspace.com+filterui:price-paid+filterui:videoage-lt10080 HTTP/2.0" 403 123 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"
74.7.241.15 - - [02/Dec/2025:17:33:19 +0800] "GET /videos/riverview/relatedvideo?FORM=VRDGAR&mid=1A3BE4443BF9914FA3931A3BE4443BF9914FA393&mmscn=stvo&q=%2bWww.www.www.www.youtubec+Com.org.net.edu&qft=+filterui%3aduration-medium+filterui%3avideoage-lt525600+filterui%3aresolution-720p+filterui%3aprice-free+filterui%3asite-vevo.com HTTP/2.0" 403 123 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.3; +https://openai.com/gptbot)"

此时应该看到爬虫的请求状态码由 200 变为 403,且流量消耗会瞬间下降。然后我们看看流量使用情况,这下流量正常了,然后我们打开一个视频,发现 v2ray 的接收流量多,nginx 的发送流量多,这也合理了

worker 有异常流量

worker 有异常流量

9.3 封禁特定恶意 IP

如果你通过日志分析(tail -f /var/log/nginx/access.log)发现某个特定的 IP 地址(非爬虫)正在疯狂请求你的 V2Ray 路径,或者试图盗用你的流量,有两种方法,一是修改了 UUID,但是可能对方还会发送请求(日志显示状态码 101 或 400,至于为什么会这样,我也不知道,之前我试过确实有这个情况),二是直接禁掉此 ip,简单省事。通过以下两种方式将其拉黑。

方法一:Nginx 层面封禁

适合记录日志,方便查看对方是否还在尝试: 直接在 Nginx 配置文件中拒绝该 IP,对方访问时将直接收到 403 Forbidden 错误。sudo nano /etc/nginx/sites-available/your_domain,和之前屏蔽爬虫一样,加一个 deny 202.38.78.112; 替换为你要禁用的 ip 即可。重启 nginx sudo nginx -t sudo systemctl restart nginx

方法二:系统防火墙封禁(更彻底)

适合对付极其顽固的高频攻击,直接丢弃数据包,效率最高: 在系统最外层直接拦截,让对方连 Nginx 都连不上(节省 CPU 处理握手的资源)

sudo ufw insert 1 deny from 202.38.78.112 to any # 1. 禁止特定 IP 访问本机所有端口 (insert 1 表示插入到规则首位,优先级最高)
sudo ufw reload # 2. 重新加载防火墙使规则生效
sudo ufw status # 3. 查看拦截状态
8月 26, 2025