亚洲av电影,av天堂,少女のトゲ在线观看 ,性欧美4khdxxxx,无码人妻丰满熟妇区免费

上一篇 下一篇 分享鏈接 返回 返回頂部

寶塔面板配置 Nginx Cache 緩存教程

發(fā)布人:會長君 發(fā)布時間:2024-05-17 17:14 閱讀量:6434
  • 系統(tǒng)環(huán)境:ubuntu 22.04
  • 寶塔面板:7.9.8
  • Nginx :1.18
  • PHP :7.4
  • Mysql :8.0

注:默認寶塔安裝的 nginx 已經(jīng)編譯了 nginx cache 和 nginx purge 相關模塊,無需額外編譯安裝模塊。

操作教程

添加下方配置到 nginx 配置文件的 http {…} 段落中

#http {...}
fastcgi_cache_path /www/wwwroot/www.yourname.com/wp-content/cache/nginx-cache levels=1:2 keys_zone=yourname:400m max_size=4g inactive=72h use_temp_path=off;#此處設置緩存名
fastcgi_cache_key "$scheme$request_method$host$request_uri";
  • fastcgi_cache_path /www/…/nginx-cache 緩存路徑,可以隨便設定。
  • levelslevels=1:2 緩存目錄層級,保持默認即可。
  • keys_zone=yourname:400m 緩存區(qū)域名 yourname,后面的 :400m 是緩存鍵值可用內(nèi)存空間。
  • max_size=4g 緩存最大占用磁盤空間。
  • inactive=72h 緩存過期時間。
  • use_temp_path=off 禁用緩存臨時存儲,直接寫入到指定位置,避免多磁盤分區(qū)跨區(qū)寫入造成IO消耗。

如果當前主機有其他站點需要 nginx 緩存,那么第二個站點只需添加上方配置到 http {…} 段落中,然后修改第一行 fastcgi_cache_path、keys_zone 這兩項的值即可,最后并把第二行 fastcgi_cache_key 這行注釋掉。

參考案例

#添加到網(wǎng)站配置文件頂部
#http {...}
fastcgi_cache_path /www/wwwroot/paheal.cn/wp-content/cache/nginx-cache levels=1:2 keys_zone=yourname:400m max_size=4g inactive=72h use_temp_path=off;#此處設置緩存名
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server
{
    listen 80 reuseport;
    listen 443 ssl http2 reuseport;
    listen [::]:443 ssl http2 reuseport;
    listen [::]:80 reuseport;
    server_name paheal.cn asiayun.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /wwww/wwwroot/paheal.cn;
    #SSL-START SSL相關配置,請勿刪除或修改下一行帶注釋的404規(guī)則
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/paheal.cn/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/paheal.cn/privkey.pem;
    ssl_early_data on;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;
    #SSL-END
    #引用重定向規(guī)則,注釋后配置的重定向代理將無效
    include /www/server/panel/vhost/nginx/redirect/paheal.cn/*.conf;
    #ERROR-PAGE-START  錯誤頁配置,可以注釋、刪除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    #PHP-INFO-START  PHP引用配置,可以注釋或修改
    #include enable-php-74.conf;#重點!注銷寶塔默認配置!
    #PHP-INFO-END
    #REWRITE-START URL重寫規(guī)則引用,修改后將導致面板設置的偽靜態(tài)規(guī)則失效
    include /www/server/panel/vhost/rewrite/paheal.cn.conf;
    #REWRITE-END
    ************* 下方省略 **************

將下面的內(nèi)容添加到寶塔網(wǎng)站設置的偽靜態(tài)規(guī)則中,需修改下方規(guī)則中的 asiayun 對應上面你設定的 keys_zone 值。

#server {...}
set $skip_cache 0;
#如果請求方式為 POST 則繞過緩存
if ($request_method = POST) {
    set $skip_cache 1;
}   
if ($query_string != "") {
    set $skip_cache 1;
}   
#如果請求鏈接含下方內(nèi)容則繞過緩存
if ($request_uri ~* "/wp-admin/|/denglu/|/checkout/|/my-account/|/cart/|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.(xml|xsl)|[a-z0-9_-]+-sitemap([0-9]+)?.(xml|xsl)") {
    set $skip_cache 1;
}  
#如果攜帶 cookie 包含下方內(nèi)容則跳過
if ($http_cookie ~* "comment_author|comment_|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpressuser_|wordpresspass_|wordpress_sec_|wordpress_logged_in_|woocommerce_items_in_cart|yith_wcwl_products") {
    set $skip_cache 1;
}
#添加可以繞過緩存的終端IP地址
#if ($remote_addr ~* "123.123.123..*|10.10.10..*") { 
#    set $skip_cache 1; 
#}
location ~ [^/]\.php(/|$) {
include enable-php-74.conf; #根據(jù)你的PHP版本修改
fastcgi_cache yourname; #此處設置緩存名
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache_valid 200 301 302 304 72h;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
fastcgi_hide_header Pragma;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Nginx-Cache "$upstream_cache_status";
add_header Last-Modified $date_gmt;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
etag on;
}
location ~ /purge(/.*) {
    allow 127.0.0.1; #允許本機請求清除緩存
    #allow 10.10.10.0/24;
    #allow fd00:10::/24;
    deny all; #除上面允許的IP地址之外,其他IP地址請求一律拒絕。
    fastcgi_cache_purge asiayun "$scheme$request_method$host$1";#此處設置緩存名
}  
#設置js、css等靜態(tài)文件緩存時間30天
location ~* \.(css|js|pdf)$ { 
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=2592000, stale-while-revalidate=86400, stale-if-error=604800"; 
    expires 30d;
}
#設置jpg、png等圖片靜態(tài)文件緩存時間365天
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|webp|avif|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=31536000, stale-while-revalidate=86400, stale-if-error=604800"; 
    expires 365d;
}
#禁止緩存 robots.txt 
location = /robots.txt {
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
    expires -1;
}
#禁止緩存 xml 網(wǎng)站地圖
location ~* \.(xml|xsl)$ { 
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0"; 
    expires -1;
}
#禁止緩存 wp-cron.php 定時任務
location /wp-cron.php { 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0"; 
    expires -1; 
}
#此處是我用到的webp圖片兼容跳轉(zhuǎn)規(guī)則,你們一般用不到可以刪除
location ~* ^/?wp-content/uploads/.*\.(?:jpg|jpeg|png)$ {
    if ($http_accept !~* "webp"){
        return 301 https://r2.asiayun.com$request_uri;
        break;
    }
    return 301 https://webp.asiayun.com$request_uri;
    break;
}
#此處是 Rank Math 插件的 Sitemaps 重寫規(guī)則,如果沒有安裝這個插件也可以刪除
# START Nginx Rewrites for Rank Math Sitemaps
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
rewrite ^/([a-z]+)?-sitemap\.xsl$ /index.php?xsl=$1 last;
# END Nginx Rewrites for Rank Math Sitemaps
#此處是 WordPress 默認的偽靜態(tài)規(guī)則
location / {
    try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

由于 nginx 緩存沒有預生成緩存功能,需要通過訪問請求觸發(fā)生成緩存,所以下面給出個批量預生成緩存的方法。

添加下方命令到寶塔面板定時任務中,任務類型為 shell 腳本。
下方命令的意思是通過 wget 訪問站點地圖 sitemap.xml 內(nèi)的所有鏈接,從而達到生成 nginx 緩存的目的。

wget --quiet http://paheal.cn/product_cat-sitemap.xml --output-document - | egrep -o "https?://[^<]+" | wget --output-document=/dev/null -i - >/dev/null 2>&1

 

目錄結(jié)構(gòu)
全文
QQ通知群 QQ通知群
微信公眾號 微信公眾號
服務熱線: 400-0505-056
電子郵箱: [email protected]