WordPress使用Nginx+memcached全站頁面緩存加速
WordPress使用Nginx+memcached全站頁面緩存加速Memcached是個好東西 它可以?由于頁面是緩存在內(nèi)存里,所以減少了系統(tǒng)的I/O操作??梢灾苯永肕emcached的分布式特性??梢灾苯永镁彺娴倪^期時間,方便對頁面的過期時間進(jìn)行處理。 網(wǎng)上找了很多教程
WordPress使用Nginx+memcached全站頁面緩存加速Memcached是個好東西 它可以?由于頁面是緩存在內(nèi)存里,所以減少了系統(tǒng)的I/O操作??梢灾苯永肕emcached的分布式特性??梢灾苯永镁彺娴倪^期時間,方便對頁面的過期時間進(jìn)行處理。 網(wǎng)上找了很多教程
Memcached是個好東西 它可以?由于頁面是緩存在內(nèi)存里,所以減少了系統(tǒng)的I/O操作??梢灾苯永肕emcached的分布式特性??梢灾苯永镁彺娴倪^期時間,方便對頁面的過期時間進(jìn)行處理。
網(wǎng)上找了很多教程 都不適用于wordpress 于是我經(jīng)網(wǎng)上的腳本 自己改了一個
注意事項!!?memcached是多核處理的 如果你是單核的服務(wù)器 在配上這個 會相當(dāng)?shù)目?甚至?xí)礄C(jī) 我之前出過了很多問題 所以我現(xiàn)在僅能把教程寫出來 而我用了redis /哭;w;
Nginx內(nèi)置了Memcached模塊memcached_module
它可以從memcached中讀取內(nèi)容后直接輸出,后續(xù)的請求不再經(jīng)過如php-fpm、django應(yīng)用程序處理,大大的提升動態(tài)頁面的速度。nginx只負(fù)責(zé)從memcached中讀取數(shù)據(jù),要往memcached寫入數(shù)據(jù)還得需要后臺的應(yīng)用程序來完成,主動的將要緩存的頁面緩存到memcached中,然后通過404重定向到后端去處理的。
memcached的key可以通過memcached_key變量來設(shè)置,如以$uri。如果命中,那么直接輸出內(nèi)容,沒有命中就意味著nginx需要從應(yīng)用程序請求頁面。同時,我們還希望該應(yīng)用程序?qū)㈡I值對寫入到memcached,以便下一個請求可以直接從memcached獲取。
如果鍵值不存在,nginx將報告not found錯誤。最好的方法是使用error_page指定和location請求處理。同時包含"Bad
Gateway"錯誤和"Gateway Timeout"錯誤,如:error_page 404 502 504 = @app;。
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; }
然后在網(wǎng)站中添加配置
網(wǎng)站-你的網(wǎng)站-設(shè)置-配置文件中 的 最后一個“}”前添加
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; }
這個是我改過的版本 理應(yīng)寶塔面板下的Wordpress都可以用
如果提示你 什么什么錯誤 那你應(yīng)該檢查一下 你的Nginx剛開始時是否是 編譯安裝方法 如果你是極速安裝的方法 請重裝成編譯安裝
版權(quán)所有:深圳市網(wǎng)商在線科技有限公司