Nginx搭建并配置属于自己的CDN的步骤:WordPress如何获取真实IP并自动刷新缓存

搬瓦工机场JMS

1.安装Nginx

这里可以手动编译,或者使用 OneinStack LNMP等脚本一键编译,注意编译上ngx_cache_purge插件

Oneinstack

编译完成后使用一些命令编译|ngx_cache_purge`,注意替换

cd /root/oneinstack/src #进入安装包目录
nginx -V
tar xzf nginx-*.tar.gz #根据上面查看到的nginx版本选择解压包

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz
cd /root/oneinstack/src/nginx-1.10.3

下面的./configure 后加的参数,你可以直接复制刚刚用nginx -V得到的参数,然后在最后加上–add-module=../ngx_cache_purge-2.3即可,参考:

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2k --with-pcre=../pcre-8.39 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3
make

mv /usr/local/nginx/sbin/nginx{,$(date +%m%d)}
cp objs/nginx /usr/local/nginx/sbin #oneinstack,其它的可以不用这个操作

nginx -t
service nginx restart

LNMP

首先下载ngx_cache_purge插件

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

后再lnmp.conf文件中的Nginx_Modules_OptiONs=''''之间添加–add-odule=/root/ngx_cache_purge-2.3
后升级Nginx版本即可

方法2:
LNMP安装ngx_cache_purge缓存清除组件步骤如下:

一、检查是否已安装 ngx_cache_purge

nginx -V 2>&1 | grep -o ngx_cache_purg
如果显示 ngx_cache_purge 则已安装。

二、编译安装 ngx_cache_purge 步骤

1、进入LNMP的源码目录
cd /root/lnmp1.6/src
2、下载最新版 ngx_cache_purge

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

3、解压 NGINX 和 ngx_cache_purge

tar xzf nginx-*.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz

4、进入 nginx 目录

cd nginx-

5、查看现有 nginx 配置参数

nginx -V
6、在现有的编译参数后面加上–add-module=/root/lnmp1.6/src/ngx_cache_purge-2.3

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.5/src/openssl-1.0.2o --add-module=/root/lnmp1.5/src/ngx_cache_purge-2.3

如您的编译参数不同,请自行对照修改。

7、开始编译
make
8、备份原来的 Nginx 编译文件

mv /usr/local/nginx/sbin/nginx{,_`date +%F`}

9、拷贝新的编译文件过去

cp objs/nginx /usr/local/nginx/sbin/nginx
10、检查配置

/usr/local/nginx/sbin/nginx -t

11、完成升级

make upgrade
12、检查是否安装成功

nginx -V 2>&1 | grep -o ngx_cache_purge
出现 ngx_cache_purge ,表示已经成功在 LNMP1.6 环境下添加了 ngx_cache_purge 组件。

2.配置网站配置文件

#创建缓存目录
mkdir -p /usr/local/nginx/caches/moec.top
#设置缓存目录权限
chown -R www:www /usr/local/nginx/caches/moec.top

网站配置文件:

proxy_cache_path /usr/local/nginx/caches/moec.top levels=1:2 keys_zone=laoxong:50m inactive=30m max_size=50m;
server { 
listen 80; 
listen 443 ssl http2; 
ssl_certificate /usr/local/nginx/conf/ssl/moec.crt; 
ssl_certificate_key /usr/local/nginx/conf/ssl/moec.key; 
ssl_session_timeout 1d; 
ssl_session_cache builtin:1000 shared:SSL:10m; 
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; 
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; 
ssl_ciphers "TLS-CHACHA20-POLY1305-SHA256:TLS-AES-256-GCM-SHA384:TLS-AES-128-GCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5"; 

ssl_prefer_server_ciphers on; 

ssl_stapling on; 
ssl_stapling_verify on; 

server_name 你的域名; 
access_log /usr/local/nginx/caches/moec.top_nginx.log combined; 

charset utf-8,gbk; 
location / { 
proxy_set_header Accept-Encoding ""; 
proxy_pass https://你的网站; 
proxy_redirect off; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_cache laoxong; 
proxy_cache_key $uri$is_args$args; 
proxy_cache_valid 200 304 30m; 
proxy_cache_valid 301 24h; 
proxy_cache_valid 500 502 503 504 0s; 
proxy_cache_valid any 1s; 
proxy_cache_min_uses 1; 
expires 12h;
 } 
location ~ /purge(/.*) {                
               allow              all;                
               allow               127.0.0.1;            
                #    deny               all;
                proxy_cache_purge laoxong $1$is_args$args;
 }
}

/usr/local/nginx/caches/moec.top:为缓存目录。
levels:指定该缓存空间有两层 hash 目录,第一层目录为 1 个字母,第二层为 2 个字母。
keys_zone=laoxong:50m:为缓存空间起个名字,这里取名为“laoxong”,后面的 50m 指内存缓存空间。
inactive=30m:如果 30 分钟内该资源没有被访问则删除。
max_size=50m:指硬盘缓存大小为 50MB.
proxy_cache_valid:指定状态码缓存时间,前面写状态码,后面写缓存时间。

访问域名/purge即可清理缓存

WordPress 获取真实IP

将下面一段代码加入到wp-config.php文件中即可:

if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}
PHP

WordPress自动刷新缓存

对于Wordpress和Typecho博客,如果启用CDN后页面被缓存,用户提交评论后无法马上显示出来,可以使用Ajax异步请求ngx_cache_purge接口,当用户提交评论的时候则清除该页面缓存。只需要下面的这段js添加到footer.php即可。

<script>
        $(document).ready(function(){
            $("#submit").click(function(){
                var uri = "https://域名/purge" + window.location.pathname;
                $.get(uri,function(data,status){
                    return true;
                });
            });
        });
</script>

还可以参考www.vpsqk.com/post/2997.html。

未经允许不得转载:搬瓦工VPS_美国VPS » Nginx搭建并配置属于自己的CDN的步骤:WordPress如何获取真实IP并自动刷新缓存

赞 (3) 打赏

相关推荐

    暂无内容!

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏