Trong hệ thống của mình đang dùng Reverse Proxy, và có nhiều truy cập không mong muốn từ Trung Quốc. Đây là các máy họ crawl dữ liệu, chắc cho AI, chỉ tốn tài nguyên. Do đó mình cần chặn.
1. Các file cấu hình cụ thể trong conf.d

Nội dung các file:
/etc/nginx/conf.d/10-geoip2.conf
# Load GeoIP2 database (Country)
# Requires: libnginx-mod-http-geoip2
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
$geoip2_country_name country names en;
}
/etc/nginx/conf.d/20-access-geoip.conf
# 1) Trusted IPs/Subnets (always allow)
geo $trusted_ip {
default 0;
# Internal networks
10.10.0.0/16 1;
192.168.0.0/16 1;
# Specific trusted public IPs (ví dụ)
#14.224.x.x 1; # qms
#124.158.x.x 1; # cmc
# 203.113.130.10 1; # admin public ip (nếu có)
}
# 2) Blocked countries (example: China)
map $geoip2_country_code $geoip_blocked_country {
default 0;
CN 1;
}
# 3) Final deny decision: deny only when NOT trusted AND blocked country
map "$trusted_ip:$geoip_blocked_country" $geoip_deny {
default 0;
"0:1" 1;
}
/etc/nginx/conf.d/30-geoip2-log.conf
log_format geo_audit
'$remote_addr [$time_local] '
'"$request" $status '
'cc=$geoip2_country_code '
'trusted=$trusted_ip '
'blocked=$geoip_blocked_country';
2. File để include trong snippets

/etc/nginx/snippets/security/geoip2-apply.conf
# Apply deny decision (requires $geoip_deny defined in http context)
if ($geoip_deny) {
return 403;
}
3. File cấu hình website chỉ cần include vào là được
/etc/nginx/sites-available/WebGov/example.vn.conf
#ALL HTTP --> HTTPS
server {
listen 80;
listen [::]:80;
server_name example.vn
include /etc/nginx/snippets/letsencrypt-acme.conf;
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS reverse proxy
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
ssl_certificate /etc/letsencrypt/live/example.vn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.vn/privkey.pem; # managed by Certbot
# GeoIP (block CN) + whitelist internal/admin
include /etc/nginx/snippets/security/geoip2-apply.conf;
# access_log /var/log/nginx/access-geo.log geo_audit;
#ALL SITES
include /etc/nginx/snippets/ssl/ssl-common.conf;
include /etc/nginx/snippets/ssl/ssl-params.conf;
include /etc/nginx/snippets/proxy/proxy-common.conf;
include /etc/nginx/snippets/headers/security-headers.conf;
# #WEBAPP THÊM
# include /etc/nginx/snippets/proxy/proxy-webapp.conf;
# #CLOUDFLARE THÊM
# include /etc/nginx/snippets/ssl/ssl-cloudflare.conf;
location / {
include /etc/nginx/snippets/backend/backend-active.conf;
}
nginx -t
systemctl reload nginx
Kết quả sau khi đã chặn Trung Quốc
GA sẽ thấy giảm dần nhé, chứ không mất ngay trên GA đâu vì GA ghi nhận các “hit”/attempt theo cách riêng (hoặc do cache/trình duyệt, hoặc bot).
Sau 5-10 phút áp dụng cấu hình

Xem log đã thấy có chặn rồi:
grep 'cc=CN' /var/log/nginx/access-geo.log | tail -n 20
Trong kết quả có ... 403 cc=CN trusted=0 blocked=1

Sau khoảng 30 phút thì đã "sạch" các IP từ Trung Quốc.
Có truy cập từ Singapore, có thể các bot crawl chuyển qua proxy hoặc chuyển qua máy chủ ở Singapore. Chúng ta cứ theo dõi và chặn thêm khu vực nếu cần.
Hoặc kết hợp chặn bằng các điều kiên khác, ngoài IP, location.
