【每日阅读】2020年10月6日-wordpress+nginx调优

索引

gzip

参考资料:http://nginx.org/en/docs/http/ngx_http_gzip_module.html

gzip  on;
gzip_proxied any;
# 不需要压缩图片,因为各种图片格式已经是为了网络传输压缩过了,再压缩效果不好,还耗费cpu时间。text/html不用写,nginx默认会压缩这个类型的内容
gzip_types text/plain text/xml text/css application/javascript;
gzip_vary on;

缓存

其实下面这个配置在已经有了If-Modified-Since和If-None-Match时,下面这个配置是没用的,因为有了这两个response,后续的请求中浏览器还是会去服务器验证资源是否过期。除非把must-revalidate替换成immutable,这个意味着资源永远永远不会过期,请浏览器不需要验证这种资源是否过期。

但是很多测试平台都看来这个东西来给网站打分,所以我还是配置上了。

location ~* .*\.(css|js|png|jpg)$ {
	add_header Cache-Control max-age=604800,must-revalidate;
}

服务器response的ETag、Last-Modified都是用于缓存控制的,在后续请求时会带上这两个值,分别放在If-Modified-Since和If-None-Match中。从下图可以看到,服务器在资源没有变更时返回了304 Not Modified的响应码,代表浏览器可以使用本地缓存。

【每日阅读】2020年10月6日-wordpress+nginx调优

webp

imagify这个wordpress插件把图片转换成了webp,并且提供了rewrite配置,这里记录一下这个配置

# BEGIN Imagify: rewrite rules for webp
location ~* ^(/.+)\.(jpg|jpeg|jpe|png|gif)$ {
	add_header Vary Accept;

    # 浏览器支持如果支持web
	if ($http_accept ~* "webp"){
		set $imwebp A;
	}

    # 如果web文件存在
	if (-f $request_filename.webp) {
		set $imwebp  "${imwebp}B";
	}

    # 验证满足上述两个条件,就请求webp
	if ($imwebp = AB) {
		rewrite ^(.*) $1.webp;
	}
}
# END Imagify: rewrite rules for webp

http2

按道理说只需要在这里加上http2的配置即可。

listen 443 ssl http2 default_server;

但是nginx -s reload时可能会报错说nginx: [emerg] the "http2" parameter requires ngx_http_v2_module。这个说明编译时缺了一个参数,导致nginx不支持http2,所以要重新编译来更新nginx。

首先使用nginx -V来看之前的编译命令。这一点真的是太棒了,比apache好太多了,因为apache如果不是自己编译安装的,根本看不到当初编译的参数,这昨天就让我很头疼,所以改换了nginx。

【每日阅读】2020年10月6日-wordpress+nginx调优

看到编译参数之后,加上--with-http_v2_module编译就行。注意这里面有一个目录--prefix,指的是nginx的安装目录。这个经过实验,指向原有nginx目录即可(如果你原来的nginx所有文件都在一个目录下),配置文件不会丢失。

我这里贴上我服务器的重新编译的3行命令作记录

./configure --prefix=/usr/local/lighthouse/softwares/nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --with-http_v2_module

make

make install

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2542

(0)
geekgaogeekgao博主
上一篇 2020年10月6日
下一篇 2020年10月8日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-1617.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900