怎么使用Nginx缓存加速WordPress站点-WordPress

资源魔 64 0

博客守旧以来,次要记载学习以及应用进程中遇到的成绩及处理计划。文章格调倾向自娱自乐,因而拜访量较少,一台1核1G的vps足以撑持网站的失常运转。

起初本站引入三个页面,这三个页面应该对有上外网需要的网友颇有协助,也给本站带来了很年夜的流量。本站用的WordPress顺序,测验考试过装置各类缓存插件(super cache, w3 total cache等)减速运转,然而低配的vps仍然难以支持这么年夜的拜访量。经过日记能够看到跟着拜访量的添加,php-fpm过程增多,Mysql的衔接以及线程增多,接着呈现OOM,而后零碎kill掉占用内存最年夜的Mysql过程,于是网站进入503宕机模式。

买更好的vps能处理拜访量年夜的成绩,然而要花更多的钱。做为一个技巧宅,起首想到确当然是若何榨干现无机器来撑持年夜流量。做过的测验考试包罗切换到比WordPress功能更好的Ghost,参考:测验考试Ghost 。然而绝对于WordPress,Ghost的生态远不那末成熟,终极保持了。

冥思苦想下,最终处理方法是用Nginx缓存,最后的文章可参考:Nginx设置装备摆设fastcgi cache。fastcgi_cache的益处是年夜局部用户的申请不必后端php-fpm打交道,间接发送缓存的动态页面,速率上甩各类WordPress插件好几条街!相比之下wordpress的各类插件还要执行php,也防止没有了拜访数据库,弱爆了!

自从应用了nginx缓存,网站颠簸运转,再也不呈现过宕机的景象。同时vps的cpu以及内存占用率直线降落,再也无需担忧vps的设置装备摆设成绩,觉得再来10倍流量博客也撑患上住!

由于nginx稳如狗的体验,以是如今关于博客类读多写少的产物都是强推nginx缓存(fastcgi缓存或许proxy缓存)。鉴于可能帮到一些网友,现贴出 /etc/nginx/nginx.conf 设置装备摆设文件供网友参考(蕴含ssl设置以及gzip局部):

# 文件: /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
    access_log  /var/log/nginx/access.log  main buffer=32k flush=30s;
    server_tokens       off;
    client_max_body_size 100m;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # ssl设置装备摆设
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_ecdh_curve secp384r1;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets off;
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7
    add_header Strict-Transport-Security "max-age=63072000; preload";
    #add_header X-Frame-Options DENY;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    # 请依照本人的需要更改
    fastcgi_cache_path /var/cache/nginx/tlanyan levels=1:2 keys_zone=tlanyan:10m inactive=30m use_temp_path=off; 
    fastcgi_cache_key $request_method$scheme$host$request_uri;
    # note: can also use HTTP headers to form the cache key, e.g.
    #fastcgi_cache_key $scheme$request_method$host$request_uri$http_x_custom_header;
    #fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 200 301 302 10h;
    fastcgi_cache_valid 404 10m;
    fastcgi_ignore_headers Expires Set-Cookie Vary;
    # gzip 设置装备摆设
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_comp_level 7;
    gzip_types
        text/css
        text/plain
        text/javascript
        application/javascript
        application/json
        application/x-javascript
        application/xml
        application/xml+rss
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon
        application/rss+xml
        application/atom_xml
        image/jpeg
        image/gif
        image/png
        image/icon
        image/bmp
        image/jpg;
    gzip_vary on;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

和用于WordPress站点的网站设置装备摆设文件(/etc/nginx/conf.d/tlanyan.conf):

server {
    listen 80;
    listen [::]:80;
    server_name www.tlanyan.me tlanyan.me; # 请换成本人的域名
    rewrite ^(.*) https://$server_name$1 permanent;
}
server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name www.tlanyan.me tlanyan.me; # 请换成本人的域名
    charset utf-8;
    ssl_certificate /etc/nginx/conf.d/tlanyan.pem;  # 请换成本人的证书以及密钥
    ssl_certificate_key /etc/nginx/conf.d/tlanyan.key;
    set $host_path "/var/www/tlanyan";  # 请改为本人的门路
    access_log  /var/log/nginx/tlanyan.access.log  main buffer=32k flush=30s;
    error_log /var/log/nginx/tlanyan.error.log;
    root   $host_path;
    # 缓存标志
    set $skip_cache 0;
    if ($query_string != "") {
        set $skip_cache 1;
    }
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    # 登任命户或宣布评论者
    if ($http_cookie ~* "co妹妹ent_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }
    location = / {
        index  index.php index.html;
        try_files /index.php?$args /index.php?$args;
    }
    location / {
        index  index.php index.html;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ ^/\.user\.ini {
            deny all;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_cache tlanyan;
        fastcgi_cache_valid 200 301 302 30m;
        fastcgi_cache_valid 404 10m;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache_lock on;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|jpeg)$ {
        expires max;
        access_log off;
        try_files $uri =404;
    }
}

上述设置装备摆设对最新版的Nginx测试无效,具体设置装备摆设指令请参考Nginx民间文档。

更多WordPress技巧文章,请拜访WordPress栏目!

以上就是怎样应用Nginx缓存减速WordPress站点的具体内容,更多请存眷资源魔其它相干文章!

标签: WordPress wordpress教程 wordpress自学 wordpress技术

抱歉,评论功能暂时关闭!