A solution to build a self-built CDN and a tutorial on setting up a reverse proxy cache system

888u

Last update at :2024-04-08,Edit by888u

A solution to build your own CDN, tutorial on how to build a reverse proxy cache system

Preparation
First, there must be at least two servers or VPS and other resources that can operate independently, such as A and B. A is in the United States, using WordPress, IP address: 1.2.3.4, domain name binding: www.a.com; B is in Japan, Blank website, IP address: 2.3.4.5, domain name binding: static.b.com.

The main website of the website is set up on server A (that is, all website content, including dynamic files, databases, etc.), and we will set up CDN services on station B.

Program Installation
Since the service environment has been set up on site A and the website is running normally, there is no need to perform extra operations on site A. Therefore, all operations that are not explained will be performed on site B.

First of all, the self-built CDN chooses the ngx_cache_purge module. To run this module, Nginx needs to be installed on the server. If it has been installed before, reinstall it and add the parameters.

Download related software

wget http://nginx.org/download/nginx-1.10.3.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxf nginx-1.10.3
tar zxf ngx_cache_purge-2.3.tar.gz

Install Nginx

cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=../ngx_cache_purge-2.3 # Because everyone's needs are different, Kangkang just wrote ngx_cache_purge here. Module, if you have other needs, please refer to nginx compilation parameters.
make && make install

Verify ngx_cache_purge installation

/usr/local/nginx/sbin/nginx -V

If the words "--add-module=../ngx_cache_purge-2.3" appear, it means that the installation has been completed.

Nginx configuration
Open the nginx.conf file and add the following content in the HTTP area

proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_cache_path /data/wwwroot/static.a.com levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=5g;
proxy_temp_path /data/wwwroot/static.a.com/tmp;

Pay attention to "/data/wwwroot/static.a.com" above, which is the website path of station B

Open the website configuration file, add or modify the following content, and cache static files

location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico|js|css)$ {
proxy_pass http://www.a.com;
proxy_redirect off;
proxy_set_header Host www.a.com;
proxy_cache cache_one;
proxy_cache_valid 200 302 304 365d;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
add_header Images-Cache "$upstream_cache_status from $host";
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
log_not_found off;
expires max;
}

Finally reload Nginx. When visiting static.a.com in the future, Station B will first check whether there is a local cache. If the cache exists, it will be displayed directly. If it does not exist, it will visit www.a.com. Resources are saved for display.

Website program configuration
Now the CDN is set up, but how to use it on the website?

The domain name of all static resources on the main site must be static.a.com
The static.a.com domain name must correctly point to station B
WordPress replacement method

If the website uses the WordPress program, it can be very convenient to operate the resources. In fact, no operation is required. Just change the domain name of the static resources. Anyway, the CDN will automatically pull the resources to the local!

Open the website template function file "function.php" and add the following code in it

if ( !is_admin() ) {
add_action('wp_loaded','lovekk_ob_start');
function lovekk_ob_start() {
ob_start('lovekk_cdn_replace');
}
function lovekk_cdn_replace($html) {
return str_replace('http://www.a.com/wp-content/uploads/', 'http://static.a.com/wp-content/uploads/', $html);
}
}

If this site has a cache plug-in installed, clear the cache first, and then try to open the website. If all the static resources in the uploads directory point to the static.a.com domain name, and the access is normal, it means that the CDN is running normally!

Recommended site searches: website icp registration, Hong Kong's best virtual host, domain name registration number query, US free hosting, webmaster IP, cheap cloud server 5 yuan a month, US virtual host, free domain name, domain name registration network, news ip server,

A solution to build a self-built CDN and a tutorial on setting up a reverse proxy cache system

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码