标签 wordpress 下的文章

由于好奇心重而且比较贪玩,喜欢尝试不同的东西。也为便于记录生活,很久前我还部署了一个专门用于存放手机随手拍照片的博客。该博客是基于WordPress系统,地址:https://www.ccchen.com

因为访问量非常低,而且也不想再倒腾备案的事情。所以,我决定让它留在linode上,毕竟还有4个多月的免费使用时间呢:D。

今天抽空把digitalocean上的系统删掉,顺便把linode上的WordPress修整了一下。主要是作提高访问速度和风格简单的修正处理。

1、图片用了又拍云的免费CDN加速功能。为了节省系统资源没有用外接插件,直接在风格的模板函数文件functions.php头部加入了下面的代码。有需要的朋友可以参考一下,把CDN_HOST的值替换你的cdn地址即可。

    //将本地图片地址替换为CDN地址
    define('CDN_HOST','//ccchen.gdcn.net');
    add_filter('the_content','z_cdn_content');
    function z_cdn_content($content){
        return str_replace(home_url().'/wp-content/uploads', CDN_HOST.'/wp-content/uploads', $content);
        }
        add_filter('wp_get_attachment_url','z_get_attachment_url',10,2);
    function z_get_attachment_url($url, $post_id){
        return str_replace(home_url(), CDN_HOST, $url);
        }
    add_filter( 'post_thumbnail_html', 'cdn_post_image_html', 10, 3 ); 
    function cdn_post_image_html( $html, $post_id, $post_image_id ) {  
        return str_replace(home_url(), CDN_HOST, $html); 
    }  
    
    //强行插入特色图片
    
    function wpforce_featured() {
        global $post;
        $already_has_thumb = has_post_thumbnail($post->ID);
        if (!$already_has_thumb)  {
            $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
            if ($attached_image) {
                foreach ($attached_image as $attachment_id => $attachment) {
                    set_post_thumbnail($post->ID, $attachment_id);
                }
            } else {
                set_post_thumbnail($post->ID, '414');
            }
        }
    }  //end function
    add_action('the_post', 'wpforce_featured');
    add_action('save_post', 'wpforce_featured');
    add_action('draft_to_publish', 'wpforce_featured');
    add_action('new_to_publish', 'wpforce_featured');
    add_action('pending_to_publish', 'wpforce_featured');
    add_action('future_to_publish', 'wpforce_featured');

2、使用cloudflare加速普通文件访问速度,解决linode间歇断流问题。在页面规则(Page Rules)里面按照下图配置一个缓存规则即可。有需要的可以参考一下。
cloudflare

个人主观感受,还是有比较明显的提速效果。

以上。


wordpress-logo-stacked-rgb.png

貌似2017年1月1日起是http使用的最后期限,所有站点都纷纷开始使用https访问。我最近也使用了acme.sh进行相关站点的https证书处理。但是唯独某个使用wordpress的站点老死无法生成证书,通过acme的debug模式可以看到服务器返回的是403访问拒绝的错误。

通过更换虚拟目录和手工配置验证文件等方法纠错依旧无解。我的主机配置是nginx的PHP方案。后来想了一下是否是nginx配置文件有问题。最终在看wordpress官方的nginx rewrite时发现了问题的端倪。原来在Global restrictions file就是官方推荐的rewrite方案文件wordpress.conf其中有一段:

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
    deny all;
}

因为ssl证书验证时候创建过程是要通过web方式访问站点根目录中的.well-known目录中生成验证字符串文件。因此这行rewrite导致该目录无法被验证服务访问。但是奇怪的是通过手工创建的文件可以通过http访问(可能是我当时幻觉了╥﹏╥...)。

解决问题的方法很简单,就是将nginx配置文件目录中的wordpress.conf里面找到上面那几行注释掉即可。

具体的设置可以参考hrwhisper的配置方法进行配置Nginx服务器,本机使用的配置文件是这样的,因为多个站点共用了证书生成路径,因此统一写成了ssl-key.conf文件进行配置。ssl.conf文件请详细查看这里
nginx站点的vhost配置文件

server {
        listen 443 ssl;
        server_name www.ezo.biz ezo.biz;
        charset     utf-8;
        index index.html index.htm index.php;
        root  /home/ccchen/www/ezo.biz;
        ssl_certificate /etc/letsencrypt/live/ezo.biz/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/ezo.biz/privkey.pem;
        include global/ssl.conf;

        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
        }

        location ~ .*\.php(\/.*)*$ {
            #root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

}
server {
    listen  80;
    server_name ezo.biz www.ezo.biz;
    include global/ssl-key.conf;
}

ssl-key.conf文件

location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /home/ccchen/www/key;
}

location / {
    return 301 https://$server_name$request_uri;
}

BTW:如果日后使用acme.sh进行证书更新的时候切记使用下面的范例进行操作(注意路径):

acme.sh  --issue  -d mydomain.com -d www.mydomain.com  --webroot  /home/ccchen/www/key

其实/home/ccchen/www/key这个路径是所有站点都共用的,你可以自定义自己的路径,记得将改文件夹权限改为可读写。

如果你想独立管理每个站点的ssl证书的话,推荐查看这个文章,有介绍nginx 1.6+后版本的ssl配置。

-EOF-