利用 Nginx 的反向代理與快取功能,可在單一伺服器上建立簡易 CDN 節點,加速靜態資源存取、分擔來源站壓力。本文提供在 CentOS 上從零開始編譯安裝 Nginx、配置 proxy_cache、ngx_cache_purge 清除快取、虛擬主機管理與優化參數的完整步驟。

| 角色 | IP/域名 | 說明 |
|---|---|---|
| Web 來源站 | www.win8.net / 61.188.87.100 | 原始內容伺服器 |
| Nginx CDN 節點 | 192.168.190.128 | CentOS,反向代理+快取 |
| 客戶端 | 192.168.190.1 (Win7) | 修改 hosts 指向 CDN 節點 |
vim /etc/selinux/config
# SELINUX=enforcing → 註解
# SELINUXTYPE=targeted → 註解
SELINUX=disabled # 新增
:wq
shutdown -r now
vim /etc/sysconfig/iptables
# 加入:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables restart
yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl
/usr/local/src/usr/local/軟體名稱cd /usr/local/src
# Nginx 穩定版
wget https://nginx.org/download/nginx-1.8.0.tar.gz
# PCRE(支援 rewrite)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
# ngx_cache_purge(快取清除模組)
wget https://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
mkdir /usr/local/pcre
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre
make && make install
groupadd www
useradd -g www www -s /bin/false
cd /usr/local/src
tar zxvf ngx_cache_purge-2.3.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure \
--prefix=/usr/local/nginx \
--user=www --group=www \
--with-http_stub_status_module \
--with-openssl=/usr/ \
--with-pcre=/usr/local/src/pcre-8.35 \
--add-module=../ngx_cache_purge-2.3
make && make install
關鍵: --with-pcre 指向原始碼解壓路徑,非安裝路徑。
建立 /etc/rc.d/init.d/nginx(內容見原文),賦予執行權限並加入開機啟動:
chmod 775 /etc/rc.d/init.d/nginx
chkconfig nginx on
service nginx restart
/usr/local/nginx/conf/nginx.confuser www www;server { listen 80 default; server_name _; return 404; }include vhost/*.conf;include proxy.conf;include mysvrhost.conf;worker_processes 2;
events {
use epoll;
worker_connections 65535;
}
http {
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
}
/usr/local/nginx/conf/proxy.confmkdir -p /home/proxy_temp_dir /home/proxy_cache_dir
chown www.www -R /home/proxy_cache_dir /home/proxy_temp_dir
chmod -R 777 /home/proxy_cache_dir /home/proxy_temp_dir
# proxy.conf 內容
proxy_temp_path /home/proxy_temp_dir;
proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=10g;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_pass_header Set-Cookie;
proxy_pass_header P3P;
proxy_hide_header X-Powered-By;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/usr/local/nginx/conf/vhost/www.win8.net.confserver {
listen 80;
server_name www.win8.net;
location / {
proxy_pass https://backend_servers;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
add_header X-Cache $upstream_cache_status;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.190.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}
/usr/local/nginx/conf/mysvrhost.confupstream backend_servers {
server 61.188.87.100:80 weight=1 max_fails=2 fail_timeout=30s;
# 可加入多台來源站
}
https://www.win8.net,檢查回應標頭 X-Cache: HIT/MISS/BYPASS。curl "https://192.168.190.128/purge/path/to/resource"。ngx_http_stub_status_module 或分析存取日誌。此架構可作為簡易 CDN 邊緣節點,生產環境建議搭配多節點、健康檢查、SSL 終結、WAF 等進階功能。