Nginx+Memcached高速优化DedeCMS之服务器配置

接前日的Nginx+Memcached高速优化DedeCMS之软件安装软件安装.这些配置包括:文章内页,文章列表页,栏目主页,网站首页,图片,CSS等。这些资源都会被缓存到Memcache中,以达到最快的打开速度,最小的CPU使用。
下面贴出Nginx+Memcache高度优化的详细配置:

user  www;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

#平均分配CPU
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000;

events {
use epoll;
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#upload_progress proxied 1m;

fastcgi_buffers 8 128k;
send_timeout 3600;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

tcp_nopush     on;

gzip on;
gzip_min_length  10k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_types     gzip_types  text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/atom+xml;

gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

server_name_in_redirect off;

upstream backend{
server  127.0.0.1:9000;
server  127.0.0.1:9001;
ip_hash;
}

#Memcache 后端
upstream mem_backend {
server 127.0.0.1:11211;
ip_hash;
keepalive 1024 single;
}

server {
listen       80;
server_name  mdede.yourdomain.com;

location / {
root   /var/www/mdede.yourdomain.com;
index  index.html index.htm index.php;
}
#用于自定清理哪个页面
location ~ ^/pure/index.html {
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
memc_pass 127.0.0.1:11211;
memc_cmds_allowed get delete flush_all stats;
}

location ~* ^/index.html$ {
set $memc_cmd 'get';
set $memc_key http://$host$uri$is_args$args;
memc_flags_to_last_modified on;
memc_pass mem_backend;

error_page 404 = @index404;

}

#文章内页,设置为直接从Memcache内存读取
location ~* ^/a/(.*)/(\d+)(_(\d+))?\.html {
set $memc_cmd 'get';
set $memc_key http://$host$uri$is_args$args;
memc_flags_to_last_modified on;
memc_pass mem_backend;

error_page 404 = @view404;#当Memcache中无记录,跳到@view404

}

location ~* ^/a/(.*)/list_(\d+)(_(\d+))?\.html {
set $memc_cmd 'get';
set $memc_key http://$host$uri$is_args$args;
memc_flags_to_last_modified on;
memc_pass mem_backend;
error_page 404 = @list404;

}

location ~* ^/a/(.*)/index\.html?$ {
set $memc_cmd 'get';
set $memc_key http://$host$uri$is_args$args;
memc_flags_to_last_modified on;
memc_pass mem_backend;
error_page 404 = @typeindex404;

}

location ~ \.php$ {

if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}
root          /var/www/mdede.yourdomain.com;
fastcgi_pass backend;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /var/www/mdede.yourdomain.com/$fastcgi_script_name;
include        fastcgi_params;
##fastcgi_buffers 8 128k;
send_timeout 600;
error_page 502 = @view502;
}

location ~* \.(gif|jpg|jpeg|png|bmp|css)$ {
set $memc_cmd 'get';
set $memc_key $uri;
memc_flags_to_last_modified on;
memc_pass mem_backend;
error_page 404 = @static404;
}

location @a404 {
rewrite ^(.*)? /nginx_memcached.php?f=$1&e=3600&a=1 break;
error_page 404 = @a404;

}

#当Memcache中无记录时,跳到这里交给PHP处理
location @view404 {
root          /var/www/mdede.yourdomain.com;
rewrite ^/a/(.*)/(\d+)\.html /plus/view2.php?aid=$2&k=$memc_key last;#由htmlrewrite为PHP处理,view2.php是一个基于view.php修改的PHP程序,主要功能为捕获文章内容,保存到Memcache中,并将内容输出。由于篇幅,这个PHP程序将在下一编中与其它PHP程序一同贴出。
rewrite ^/a/(.*)/(\d+)(_(\d+))?\.html /plus/view2.php?aid=$2&pageno=$4&k=$memc_key last;
}
#处理文章列表
location @list404 {
root          /var/www/mdede.yourdomain.com;
rewrite  ^/a/(.*)/list_(\d+)(_(\d+))?\.html /plus/list2.php?tid=$2&PageNo=$4&k=$memc_key last;
rewrite  ^((/a/.*)/(index\.html)?)$ /plus/list2.php?tid=$2&PageNo=1&k=$memc_key last;
}

#处理栏目首页
location @typeindex404{
root          /var/www/mdede.yourdomain.com;
#rewrite  ^/a/(.*)/list_(\d+)(_(\d+))?\.html /plus/list2.php?tid=$2&PageNo=$4&k=$memc_key last;
rewrite  ^((/a/.*)/(index\.html)?)$ /plus/list2.php?tid=$2&k=$memc_key last;
}

location @index404 {
root          /var/www/mdede.yourdomain.com;
rewrite ^/index.html$ /index2.php?k=$memc_key last;
#rewrite ^/a/(.*)/(\d+)(_(\d+))?\.html /plus/view2.php?aid=$2&pageno=$4&k=$memc_key last;
}

location @view502 {
root          /var/www/mdede.yourdomain.com;
rewrite ^(.*)$ /index.html last;
}

location @static404{
rewrite ^(.*)? /nginx_memcached.php?f=$1&e=300 break;
proxy_pass http://localhost:8088;
}

}

#特别地,本次优化还将会把图片,CSS等缓存于Memcache中,加速!
server {
listen       8088;

location / {
root   /var/www/mdede.yourdomain.com;
index  index.html index.htm index.php;

}

location ~ \.php$ {
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}
root          /var/www/mdede.yourdomain.com;
fastcgi_pass backend;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /var/www/mdede.yourdomain.com/$fastcgi_script_name;
include        fastcgi_params;
##fastcgi_buffers 8 128k;
send_timeout 600;
}

}

}

相关阅读:
Nginx+Memcached高速优化DedeCMS之软件安装

发表评论?

2 条评论。

  1. 请问memcache可以加速静态页面吗?怎么配置呢

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>