WordPress使用Nginx+memcached全站页面缓存加速

Memcached是个好东西 它可以 由于页面是缓存在内存里,所以减少了系统的I/O操作。可以直接利用Memcached的分布式特性。可以直接利用缓存的过期时间,方便对页面的过期时间进行处理。

网上找了很多教程 都不适用于wordpress 于是我经网上的脚本 自己改了一个

注意事项!! memcached是多核处理的 如果你是单核的服务器 在配上这个 会相当的卡 甚至会宕机 我之前出过了很多问题 所以我现在仅能把教程写出来 而我用了redis /哭;w;

首先 你要是Nginx不是Apache 装好memcached 方法前几篇文章写过了 https://51shenyun.cn/wordpress_memcached/

原理

Nginx内置了Memcached模块memcached_module
它可以从memcached中读取内容后直接输出,后续的请求不再经过如php-fpm、django应用程序处理,大大的提升动态页面的速度。nginx只负责从memcached中读取数据,要往memcached写入数据还得需要后台的应用程序来完成,主动的将要缓存的页面缓存到memcached中,然后通过404重定向到后端去处理的。

memcached的key可以通过memcached_key变量来设置,如以$uri。如果命中,那么直接输出内容,没有命中就意味着nginx需要从应用程序请求页面。同时,我们还希望该应用程序将键值对写入到memcached,以便下一个请求可以直接从memcached获取。
如果键值不存在,nginx将报告not found错误。最好的方法是使用error_page指定和location请求处理。同时包含"Bad
Gateway"错误和"Gateway Timeout"错误,如:error_page 404 502 504 = @app;。

Wordpress应用实例

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;  events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

	    keepalive_timeout  75s;
	    keepalive_requests 10000;
        
        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 512k;
	fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 3;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css text/xml application/xml image/jpeg image/gif image/png application/x-httpd-php;
        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;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;
        
#memcache servers load balanced
	upstream memcached {
        server     127.0.0.1:11211 weight=5 max_fails=3  fail_timeout=30s;
		keepalive 1024;
		}
#fastcgi - little load balancer
	upstream phpbackend{
		server     127.0.0.1:9000 weight=5 max_fails=5  fail_timeout=30s;
		}
server {
    listen 80;
    root  /www/server/wwwroot;
    server_name www.bt.cn;
    index index.html index.htm index.php;
    include enable-php.conf;
    client_body_timeout  1460;
    client_header_timeout 1460;
    send_timeout 1460;
    client_max_body_size 10m;
    keepalive_timeout 1300;

    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/            { deny all; }
    location /report/config.xml   { deny all; }
    location /var/                { deny all; }

	location ~* \.(jpg|png|gif|css|js|swf|flv|ico)$ {
	    expires max;
	    tcp_nodelay off;
	    tcp_nopush on;
    }
    location / {
		
        try_files $uri $uri/ @handler;
        expires 30d;
    }
   location @handler {
	rewrite / /index.php;
    }

    location ~ \.php$ {
        if (!-e $request_filename) { 
            rewrite / /index.php last; 
        }  
        expires        off; ## Do not cache dynamic content
        default_type       text/html; charset utf-8;
        if ($request_method = GET) {
            set $memcached_key $request_uri;
            memcached_pass     memcached;
            error_page         404 502 = @cache_miss;
            add_header x-header-memcached true;
		}
		if ($request_method != GET) {
			fastcgi_pass phpbackend;
		}
    }
    location @cache_miss {
        # are we using a reverse proxy?
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        
        #configure fastcgi
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_send_timeout  5m;
        fastcgi_read_timeout 5m;
        fastcgi_connect_timeout 5m;
        fastcgi_buffer_size	256k;
        fastcgi_buffers	4	512k;
        fastcgi_busy_buffers_size	768k; 
        fastcgi_param  PHP_VALUE "memory_limit = 32M";
        fastcgi_param  PHP_VALUE "max_execution_time = 18000";
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\. {
		deny all;
	}
}

}

宝塔面板

首先在nginx总配置中连接memcached 在http{ }字段中添加

#memcache servers load balanced
	upstream memcached {
        server     127.0.0.1:11211 weight=5 max_fails=3  fail_timeout=30s;
		keepalive 1024;
		}
#fastcgi - little load balancer
	upstream phpbackend{
		server     127.0.0.1:9000 weight=5 max_fails=5  fail_timeout=30s;
		}

然后在网站中添加配置

网站-你的网站-设置-配置文件中 的 最后一个“}”前添加

  client_body_timeout  1460;
    client_header_timeout 1460;
    send_timeout 1460;
    client_max_body_size 10m;
    keepalive_timeout 1300;

    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/            { deny all; }
    location /report/config.xml   { deny all; }
    location /var/                { deny all; }

    location ~* \.(jpg|png|gif|css|js|swf|flv|ico)$ {
	    expires max;
	    tcp_nodelay off;
	    tcp_nopush on;
    }
    location / {
		
        try_files $uri $uri/ @handler;
        expires 30d;
    }
   location @handler {
	rewrite / /index.php;
    }

    location ~ \.php$ {
        if (!-e $request_filename) { 
            rewrite / /index.php last; 
        }  
        expires        off; 
        default_type       text/html; charset utf-8;
        if ($request_method = GET) {
            set $memcached_key $request_uri;
            memcached_pass     memcached;
            error_page         404 502 = @cache_miss;
            add_header x-header-memcached true;
		}
		if ($request_method != GET) {
			fastcgi_pass phpbackend;
		}
    }
    location @cache_miss {

        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_send_timeout  5m;
        fastcgi_read_timeout 5m;
        fastcgi_connect_timeout 5m;
        fastcgi_buffer_size	128k;
        fastcgi_buffers	4	256k;
        fastcgi_busy_buffers_size	512k; 

        fastcgi_param  PHP_VALUE "memory_limit = 32M";
        fastcgi_param  PHP_VALUE "max_execution_time = 18000";
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\. {
		deny all;
	}

说明

这个是我改过的版本 理应宝塔面板下的Wordpress都可以用

如果提示你 什么什么错误 那你应该检查一下 你的Nginx刚开始时是否是 编译安装方法 如果你是极速安装的方法 请重装成编译安装

文中部分内容转自nginx+memcached构建页面缓存应用

评论 ( 1)
  1. 沙发
    escher 2019-06-29 02:14

    说的很细,所见即所得的文章