當(dāng)前位置:首頁 >  站長(zhǎng) >  建站經(jīng)驗(yàn) >  正文

WordPress站點(diǎn)怎么加速 Nginx緩存可以嗎

 2020-10-14 17:15  來源: 黎青松SEO博客   我來投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過

博客開通以來,主要記錄學(xué)習(xí)和使用過程中遇到的問題及解決方案。文章風(fēng)格偏向自娛自樂,因此訪問量較少,一臺(tái)1核1G的vps足以支撐網(wǎng)站的正常運(yùn)行。

后來本站引入三個(gè)頁面,這三個(gè)頁面應(yīng)該對(duì)有上外網(wǎng)需求的網(wǎng)友很有幫助,也給本站帶來了很大的流量。本站用的WordPress程序,嘗試過安裝各種緩存插件(super cache, w3 total cache等)加速運(yùn)行,但是低配的vps依然難以支持這么大的訪問量。通過日志可以看到隨著訪問量的增加,php-fpm進(jìn)程增多,Mysql的連接和線程增多,接著出現(xiàn)OOM,然后系統(tǒng)kill掉占用內(nèi)存最大的Mysql進(jìn)程,于是網(wǎng)站進(jìn)入503宕機(jī)模式。

買更好的vps能解決訪問量大的問題,但是要花更多的錢。做為一個(gè)技術(shù)宅,首先想到的當(dāng)然是如何榨干現(xiàn)有機(jī)器來支撐大流量。做過的嘗試包括切換到比WordPress性能更好的Ghost,參考:嘗試Ghost 。但是相對(duì)于WordPress,Ghost的生態(tài)遠(yuǎn)沒有那么成熟,最終放棄了。

左思右想下,終極解決辦法是用Nginx緩存,最初的文章可參考:Nginx配置fastcgi cache。fastcgi_cache的好處是大部分用戶的請(qǐng)求不用后端php-fpm打交道,直接發(fā)送緩存的靜態(tài)頁面,速度上甩各種WordPress插件好幾條街!相比之下wordpress的各種插件還要執(zhí)行php,也避免不了訪問數(shù)據(jù)庫,弱爆了!

自從使用了nginx緩存,網(wǎng)站平穩(wěn)運(yùn)行,再也沒有出現(xiàn)過宕機(jī)的現(xiàn)象。同時(shí)vps的cpu和內(nèi)存占用率直線下降,再也無需擔(dān)心vps的配置問題,感覺再來10倍流量博客也撐得?。?/p>

因?yàn)閚ginx穩(wěn)如狗的體驗(yàn),所以現(xiàn)在對(duì)于博客類讀多寫少的產(chǎn)品都是強(qiáng)推nginx緩存(fastcgi緩存或者proxy緩存)。鑒于可能幫到一些網(wǎng)友,現(xiàn)貼出 /etc/nginx/nginx.conf 配置文件供網(wǎng)友參考(包含ssl設(shè)置和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";

# 請(qǐng)按照自己的需求更改

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站點(diǎn)的網(wǎng)站配置文件(/etc/nginx/conf.d/tlanyan.conf):

server {

listen 80;

listen [::]:80;

server_name www.tlanyan.me tlanyan.me; # 請(qǐng)換成自己的域名

rewrite ^(.*) https://$server_name$1 permanent;

}

server {

listen 443 ssl http2;

listen [::]:443 ssl http2;

server_name www.tlanyan.me tlanyan.me; # 請(qǐng)換成自己的域名

charset utf-8;

ssl_certificate /etc/nginx/conf.d/tlanyan.pem; # 請(qǐng)換成自己的證書和密鑰

ssl_certificate_key /etc/nginx/conf.d/tlanyan.key;

set $host_path "/var/www/tlanyan"; # 請(qǐng)改成自己的路徑

access_log /var/log/nginx/tlanyan.access.log main buffer=32k flush=30s;

error_log /var/log/nginx/tlanyan.error.log;

root $host_path;

# 緩存標(biāo)記

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;

}

# 登錄用戶或發(fā)表評(píng)論者

if ($http_cookie ~* "comment_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;

}

}

上述配置對(duì)最新版的Nginx測(cè)試有效,詳細(xì)配置指令請(qǐng)參考Nginx官方文檔。

轉(zhuǎn)載:非常歡迎各位朋友分享到個(gè)人站長(zhǎng)或者朋友圈,但轉(zhuǎn)載請(qǐng)說明文章出處“黎青松SEO博客”。

原文地址:http://www.alitaohuo.com/CMSjiaocheng/wordpressjiaocheng/2018.html

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
wordpress站點(diǎn)

相關(guān)文章

  • 使用softaculous創(chuàng)建WordPress臨時(shí)站點(diǎn)

    WordPress是一個(gè)以PHP和MySQL為平臺(tái)的自由開源的博客軟件和內(nèi)容管理系統(tǒng),它非常流行,最適合中小企業(yè)自己建網(wǎng)站選用的建站程序,它操作方便,上手快的特點(diǎn),使得它擁有眾多的使用者。

    標(biāo)簽:
    wordpress站點(diǎn)
  • 如何利用centos7搭建wordpress站點(diǎn)

    隨著計(jì)算機(jī)技術(shù)的普及,搞IT的越來越多,但是隨著深入你會(huì)發(fā)現(xiàn)自己上了賊船,真的是一如IT深似海,從此妹紙是路人啊,當(dāng)你越來越接近自己想想中的目標(biāo)是,就會(huì)有種渺小感,這個(gè)行業(yè)知識(shí)太過深?yuàn)W龐大啦。

  • WordPress能夠用來做小說網(wǎng)站嗎

    wordpress在不斷發(fā)展中,逐漸從博客程序想整站程序發(fā)展,你除了可以用它建博客外還可以建立其他類型的網(wǎng)站,但是有局限性的,它最主要的用途還是博客,也就是他的互動(dòng)性。因此你要看到它的優(yōu)缺點(diǎn),綜合考慮,要不要用它來建站。

    標(biāo)簽:
    wordpress站點(diǎn)
  • wordpress站點(diǎn)更新需要注意什么

    數(shù)據(jù)備份,數(shù)據(jù)備份包括數(shù)據(jù)庫文件備份、wordpress主題文件備份,建議備份后直接下載到本地保存,以免發(fā)生不可預(yù)測(cè)的問題,導(dǎo)致數(shù)據(jù)丟失。

    標(biāo)簽:
    wordpress站點(diǎn)

熱門排行

信息推薦