Chuyển tới nội dung chính

Proxy — Forward, Reverse và SOCKS

Proxy là trung gian đứng giữa client và server. Tùy vị trí và mục đích, proxy có vai trò rất khác nhau.


Phân loại Proxy​


Forward Proxy​

Forward proxy đứng phía client — tất cả request từ client đi qua proxy trước khi ra Internet.

Use cases​

  • Corporate: Kiểm soát nhân viên chỉ truy cập site được phép
  • Cache: Lưu web content giảm bandwidth (trường học, ISP)
  • Anonymity: Ẩn IP thật của client
  • Bypass geo-restriction: Dùng proxy ở country khác

Squid — Forward Proxy phổ biến​

# Cài Squid
sudo apt install squid

# Cấu hình cơ bản
sudo nano /etc/squid/squid.conf
# /etc/squid/squid.conf

http_port 3128

# ACL — định nghĩa nhóm
acl localnet src 192.168.0.0/16
acl allowed_sites dstdomain .company.com .google.com .github.com
acl blocked_sites dstdomain .facebook.com .youtube.com

# Rules
http_access allow localnet allowed_sites
http_access deny localnet blocked_sites
http_access allow localnet
http_access deny all

# Cache
cache_mem 256 MB
maximum_object_size 100 MB
cache_dir ufs /var/spool/squid 10000 16 256 # 10GB cache
sudo systemctl restart squid

# Cấu hình client dùng proxy
export http_proxy=http://proxy-server:3128
export https_proxy=http://proxy-server:3128
export no_proxy=localhost,127.0.0.1,192.168.0.0/16

# Curl qua proxy
curl -x http://proxy:3128 https://google.com

Reverse Proxy​

Reverse proxy đứng phía server — client nghĩ họ đang nói chuyện trực tiếp với server, nhưng thực ra qua proxy.

Chức năng của Reverse Proxy​

Chức năngMô tả
Load BalancingPhân phối traffic đến nhiều backend servers
SSL TerminationXử lý TLS, backend dùng HTTP plain text
CachingCache static content, giảm tải backend
CompressionGzip/Brotli response trước khi gửi client
Rate LimitingGiới hạn request từ một IP
WAFLọc request độc hại (SQL injection, XSS...)
AuthCentralized authentication trước khi vào backend
A/B TestingRoute % traffic đến version khác nhau

nginx — Reverse Proxy phổ biến nhất​

# /etc/nginx/sites-available/myapp.conf

upstream backend {
least_conn; # Load balance: least connections
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080 backup; # chỉ dùng khi 2 server kia down
keepalive 32; # connection pool
}

server {
listen 80;
server_name app.company.com;
return 301 https://$host$request_uri; # redirect HTTP → HTTPS
}

server {
listen 443 ssl http2;
server_name app.company.com;

# SSL Termination
ssl_certificate /etc/ssl/certs/app.crt;
ssl_certificate_key /etc/ssl/private/app.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

# Headers bảo mật
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header Strict-Transport-Security "max-age=31536000";

# Proxy đến backend
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
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;

proxy_connect_timeout 10s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}

# Cache static files
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
proxy_pass http://backend;
proxy_cache_valid 200 1d;
expires 7d;
add_header Cache-Control "public, immutable";
}

# Rate limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://backend;
}
}

HAProxy — Layer 4/7 Load Balancer​

# /etc/haproxy/haproxy.cfg

frontend http_front
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/
redirect scheme https if !{ ssl_fc }
default_backend http_back

backend http_back
balance roundrobin
option httpchk GET /health
server app1 192.168.1.10:8080 check
server app2 192.168.1.11:8080 check
server app3 192.168.1.12:8080 check backup

SOCKS Proxy​

SOCKS (Socket Secure) hoạt động ở Layer 4 — tunnel bất kỳ TCP/UDP traffic, không phụ thuộc application protocol.

SOCKS4 vs SOCKS5​

SOCKS4SOCKS5
TCP✅✅
UDP❌✅
Authentication❌✅ (username/password)
DNSClient-sideServer-side (tránh DNS leak)
IPv6❌✅

SOCKS5 qua SSH (Dynamic Port Forwarding)​

# Tạo SOCKS5 proxy qua SSH tunnel
ssh -D 1080 -N -q user@remote-server
# -D 1080 : Dynamic forwarding (SOCKS5) trên port 1080
# -N : Không mở shell
# -q : Quiet

# Dùng trong nền
ssh -D 1080 -N -f user@remote-server

# Cấu hình ứng dụng dùng SOCKS5 proxy
curl --socks5 127.0.0.1:1080 https://internal-site.com
curl --socks5-hostname 127.0.0.1:1080 https://internal-site.com # DNS qua proxy

# Git qua SOCKS5
git config --global http.proxy socks5://127.0.0.1:1080

Transparent Proxy​

Client không biết có proxy — traffic bị intercept ở network level (thường dùng iptables/NAT).

# iptables redirect HTTP đến Squid transparent proxy
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128
# Squid transparent mode
http_port 3128 intercept # HTTP transparent
https_port 3129 ssl-bump intercept cert=/etc/squid/ca.crt key=/etc/squid/ca.key
ssl_bump server-first all # SSL inspection (HTTPS)
HTTPS Inspection (SSL Bump)

Intercept HTTPS yêu cầu cài CA certificate của proxy vào browser/OS của client. Phải có policy rõ ràng và thông báo cho người dùng — yêu cầu pháp lý ở nhiều quốc gia.


So sánh Proxy vs VPN​

Tiêu chíProxyVPN
ScopePer-applicationSystem-wide
Mã hoáTuỳ loạiLuôn mã hoá
Layer4–73–4
PerformanceNhanh hơnOverhead cao hơn
SetupĐơn giảnPhức tạp hơn
Use caseFiltering, LB, cacheRemote access, security