Nginx开启fastcgi_cache缓存加速

VPS

Nginx 开启fastcgi_cache缓存需要额外编译ngx_cache_purge模块。
看下旧版本 nginx 的 configure 选项:

[root@vultr ~]nginx -V

如果编译参数中找不到ngx_cache_purge,就需要重新编译 Nginx。

下载模块

cd /usr/local/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

下载 Nginx

cd /usr/local/src
wget http://nginx.org/download/nginx-1.13.3.tar.gz
tar zxvf nginx-1.13.3.tar.gz
cd nginx-1.13.3

编译模块

因为 Nginx 编译参数中找不到ngx_cache_purge,需要重新编译 Nginx ,新增编译参数:
#注意解压的文件夹和 nginx 源码在同一个目录

--add-module=../ngx_cache_purge-2.3

在 LNMP1.4 中,即:

./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-ipv6 --with-http_sub_module --with-openssl=/usr/local/src/lnmp1.4-full/src/openssl-1.0.2l --add-module=../ngx_cache_purge-2.3

①、接下来,常规编译新版本 Nginx,不过只要执行到make就打住,不要make install

make

②、重命名 Nginx 旧版本二进制文件,即 sbin 目录下的 nginx(期间 Nginx 并不会停止服务):

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

③、然后拷贝一份新编译的二进制文件:

cp objs/nginx /usr/local/nginx/sbin/

④、在源码目录执行 make upgrade 开始升级:

make upgrade

以上步骤完成后,再nginx -V查看一下模块是否编译成功。

重启 Nginx

nginx -s reload
service nginx restart && service php-fpm restart

建立缓存目录

/tmp/wpcache/temp两个目录需要提前创建好,后面 Nginx 配置会用到。

Nginx 配置

要用这个缓存功能,建议重新弄一个server模块(替换之前的),如下代码是目前正在使用的规则如果是 LNMP1.4 的可以直接替换。
以下内容注意域名及相关参数的替换。
①、/usr/local/nginx/conf/nginx.conf

user  www www;
worker_processes auto;
error_log  /home/wwwlogs/nginx_error.log  crit;
pid        /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http
    {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile   on;
        tcp_nopush on;
        keepalive_timeout 60;
        tcp_nodelay on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";
        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
        server_tokens off;
        access_log off;
server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #修改 IP 访问直接跳转 501
        return 501;
        #error_page   404   /404.html;
        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access.log;
    }
#以下的 server 为增加 //IP 跳转 501
server
    {
        listen 443 ssl http2 default_server;
        #listen [::]:443 default_server;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        return 501;
        ssl on;
        ssl_certificate /usr/local/nginx/conf/ssl/www.crt;
        ssl_certificate_key /usr/local/nginx/conf/ssl/www.key;
    }
include vhost/*.conf;
}

②、/usr/local/nginx/conf/vhost/www.youfencun.conf(根据自己的 LNMP 的配置修改)

#下面各个参数的含义请自行百度,我就不赘述了
#下面 2 行的中的 wpcache 及 temp 路径请自行提前创建,否则可能会路径不存在而无法启动 nginx,max_size 请根据分区大小自行设置
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

server
    {
        listen 443 ssl http2;
        server_name www.youfencun.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /var/www/html;
        ssl on;
        ssl_certificate /usr/local/nginx/conf/ssl/www.crt;
        ssl_certificate_key /usr/local/nginx/conf/ssl/www.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
        #配置缓存
        set $skip_cache 0;
        #post 访问不缓存
        if ($request_method = POST) {
            set $skip_cache 1;
        }
        #动态查询不缓存
        if ($query_string != "") {
            set $skip_cache 1;
        }
        #后台等特定页面不缓存(其他需求请自行添加即可)
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }
        #对登录用户、评论过的用户不展示缓存
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
        include wordpress.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        #robots.txt 文件非蜘蛛无法访问
        location = /robots.txt {
                if ($http_user_agent !~* "spider|bot|Python-urllib|pycurl") {
        return 403;
        }
                log_not_found off;
                access_log off;
        }
        #配置 wp-admin 的 Nginx 的认证
        location ^~ /wp-admin/
        {
        location ~ .*\.(php|php5)?$ {
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        }
        auth_basic "Authorized users only";
        auth_basic_user_file /usr/local/nginx/conf/pass.db;
        }

        access_log  /home/wwwlogs/www.youfencun.com.log;
    }

#无须收录顶级域名及顶级域名的跳转
server
    {
        listen 80;
        listen 443 ssl http2;
        server_name youfencun.com;
        location /
        {
        root /var/www/html/html/;
        }
        #配置顶级域名的 robots.txt 文件改写
        location ~ (robots.txt)
        {
        rewrite /robots.txt /wrobots.txt last;
        }
        #顶级域名跳转
        error_page 404 //www.youfencun.com;
    }

③、/usr/local/nginx/conf/wordpress.conf

location / {
	try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
#配置外链转内链跳转的伪静态
rewrite ^/go/(.*)$ /go.html?url=$1 last;

④、/usr/local/nginx/conf/enable-php.conf

location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            #以下为新增函数
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;
            add_header X-Cache "$upstream_cache_status From $host";
            fastcgi_cache WORDPRESS;
            fastcgi_cache_valid 200 301 302 1d;
            #HSTS 跳转
            add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        }

安装插件

直接进入 WordPress 后台插件安装界面搜索 Nginx Helper 关键词在线安装即可,配置如下:
Nginx开启fastcgi_cache缓存加速

Nginx开启fastcgi_cache缓存加速

Nginx开启fastcgi_cache缓存加速

清理模式

在 WordPress 根目录下的 wp-config.php 中新增如下代码即可:

//根据实际情况定义缓存的存放路径
define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

效果预览

Nginx开启fastcgi_cache缓存加速

© 声明:本站原创文章采用BY-NC-SA创作共用协议,转载时请标明本文地址;非原创(转载)文章版权归原作者所有。

RECENT POSTS

Stay Hungry. Stay Foolish.

数据库查询次数:11次,页面生成时间:0.032秒。