美国服务器搭建CDN节点教程是利用美国机房服务器部署Nginx反向代理缓存节点,将源站内容缓存到美国节点并为用户就近提供加速服务的完整操作流程。

环境准备
1 服务器要求
操作系统:Ubuntu 22.04 LTS 或 Debian 12
配置建议:2核CPU、4GB内存、50GB固态硬盘起步
带宽:100Mbps以上独享带宽
源站:已有可访问的源站IP或域名
2 更新系统
登录美国服务器后执行以下命令:
aptupdate&&aptupgrade-y
安装常用工具:
aptinstall-ycurlwgetgnupg2ca-certificateslsb-releaseufw
安装Nginx
使用Nginx官方源安装最新稳定版:
curl-fsSLhttps://nginx.org/keys/nginx_signing.key|gpg--dearmor-o/usr/share/keyrings/nginx-archive-keyring.gpg echo"deb[signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg]http://nginx.org/packages/ubuntu$(lsb_release-cs)nginx">/etc/apt/sources.list.d/nginx.list aptupdate aptinstall-ynginx
启动并设置开机自启:
systemctlenable--nownginx systemctlstatusnginx
配置缓存目录与全局参数
创建缓存目录:
mkdir-p/var/cache/nginx/cdn chown-Rwww-data:www-data/var/cache/nginx/cdn
编辑Nginx主配置文件 /etc/nginx/nginx.conf,在 http 块中加入以下参数:
proxy_cache_path/var/cache/nginx/cdnlevels=1:2keys_zone=cdn_cache:256mmax_size=20ginactive=7duse_temp_path=off; proxy_cache_key$scheme$host$request_uri; proxy_cache_valid20030130224h; proxy_cache_valid4041h; proxy_cache_use_staleerrortimeoutupdatinghttp_500http_502http_503http_504; proxy_cache_background_updateon; proxy_cache_lockon; client_max_body_size100m; server_tokensoff;
参数说明:
keys_zone定义共享内存缓存区,名称为cdn_cache,大小256MBmax_size设置缓存文件最大占用磁盘20GBinactive设置7天未访问的缓存自动删除use_temp_path关闭临时目录以减少磁盘IO
创建反向代理CDN节点配置
新建站点配置文件:
vim/etc/nginx/conf.d/cdn.conf
写入以下配置:
server{
listen80;
server_namecdn.example.com;
access_log/var/log/nginx/cdn_access.log;
error_log/var/log/nginx/cdn_error.log;
location/{
proxy_passhttp://origin.example.com;
proxy_cachecdn_cache;
proxy_cache_valid20030130212h;
proxy_cache_valid40430m;
proxy_cache_use_staleerrortimeoutupdatinghttp_500http_502http_503http_504;
proxy_cache_background_updateon;
proxy_cache_lockon;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
add_headerX-Cache-Status$upstream_cache_status;
proxy_ignore_headersCache-Control;
}
location~*\.(css|js|jpg|jpeg|png|gif|ico|webp|svg|woff|woff2|ttf|eot|mp4|webm|pdf|zip|gz|tar)${
proxy_passhttp://origin.example.com;
proxy_cachecdn_cache;
proxy_cache_valid20030d;
proxy_cache_use_staleerrortimeoutupdatinghttp_500http_502http_503http_504;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
add_headerX-Cache-Status$upstream_cache_status;
expires30d;
access_logoff;
}
}将 cdn.example.com 替换为节点域名,将 origin.example.com 替换为源站域名或IP,如果需要源站使用IP,可以写成 proxy_pass http://192.0.2.10;,同时在 proxy_set_header Host 中指定源站域名。
配置HTTPS访问
安装Certbot:
aptinstall-ycertbotpython3-certbot-nginx
申请SSL证书:
certbot--nginx-dcdn.example.com
按提示完成证书申请,Certbot会自动修改Nginx配置并启用HTTPS,手动配置可参考以下片段:
server{
listen443sslhttp2;
server_namecdn.example.com;
ssl_certificate/etc/letsencrypt/live/cdn.example.com/fullchain.pem;
ssl_certificate_key/etc/letsencrypt/live/cdn.example.com/privkey.pem;
ssl_protocolsTLSv1.2TLSv1.3;
ssl_ciphersHIGH:!aNULL:!MD5;
ssl_session_cacheshared:SSL:10m;
ssl_session_timeout1d;
location/{
proxy_passhttp://origin.example.com;
proxy_cachecdn_cache;
proxy_cache_valid20030130212h;
proxy_cache_valid40430m;
proxy_cache_use_staleerrortimeoutupdatinghttp_500http_502http_503http_504;
proxy_cache_background_updateon;
proxy_cache_lockon;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
add_headerX-Cache-Status$upstream_cache_status;
proxy_ignore_headersCache-Control;
}
location~*\.(css|js|jpg|jpeg|png|gif|ico|webp|svg|woff|woff2|ttf|eot|mp4|webm|pdf|zip|gz|tar)${
proxy_passhttp://origin.example.com;
proxy_cachecdn_cache;
proxy_cache_valid20030d;
proxy_cache_use_staleerrortimeoutupdatinghttp_500http_502http_503http_504;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
add_headerX-Cache-Status$upstream_cache_status;
expires30d;
access_logoff;
}
}
server{
listen80;
server_namecdn.example.com;
return301https://$host$request_uri;
}启用压缩与性能优化
在 http 块或 server 块中添加Gzip压缩:
gzipon; gzip_varyon; gzip_proxiedany; gzip_comp_level6; gzip_min_length1k; gzip_typestext/plaintext/cssapplication/jsonapplication/javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascriptimage/svg+xml;
调整连接参数:
keepalive_timeout60; keepalive_requests1000; sendfileon; tcp_nopushon; tcp_nodelayon;
安全与访问限制
1 限制请求频率
在 http 块中定义限速规则:
limit_req_zone$binary_remote_addrzone=cdn_limit:10mrate=50r/s; limit_conn_zone$binary_remote_addrzone=conn_limit:10m;
在 server 或 location 中启用:
limit_reqzone=cdn_limitburst=100nodelay; limit_connconn_limit20;
2 防火墙配置
使用UFW放行必要端口:
ufwallow22/tcp ufwallow80/tcp ufwallow443/tcp ufwenable
3 隐藏Nginx版本
在主配置文件中确认:
server_tokensoff;
检查配置并测试节点
检查Nginx配置语法:
nginx-t
重载配置:
systemctlreloadnginx
使用curl测试缓存状态:
curl-Ihttps://cdn.example.com/test.jpg
查看响应头中的 X-Cache-Status:
MISS表示首次请求未命中缓存,已回源并存储HIT表示命中缓存,直接从节点返回EXPIRED表示缓存过期,已重新回源UPDATING表示后台更新缓存
连续请求同一资源两次,第二次应显示 HIT,检查磁盘缓存目录是否生成缓存文件:
ls-lh/var/cache/nginx/cdn du-sh/var/cache/nginx/cdn
查看访问日志确认请求记录:
tail-f/var/log/nginx/cdn_access.log
将域名解析到美国服务器IP,使正式流量进入节点。