簡體   English   中英

Nginx 只為機器人緩存

[英]Nginx to cache only for bots

我有一個不錯的網站(nginx -> apache -> mod_php/mysql)來稍微調整一下,我發現最大的問題是搜索機器人過去常常超載它一次發送許多請求。

站點的核心(即PHP)有緩存,所以站點的作者報告應該沒有問題,但實際上瓶頸是apache的回復太長,頁面請求太多。

我能想象的是有一些基於 nginx 的緩存來緩存僅用於機器人的頁面。 TTL 可能足夠高(頁面上沒有任何動態不能再等 5-10 分鍾刷新)讓我們將“bot”定義為在其 UA 字符串中包含“Bot”的任何客戶端(“BingBot”作為一個例子)。

所以我嘗試做這樣的事情:

map $http_user_agent $isCache {
 default 0;
 ~*(google|bing|msnbot) 1;
 } 

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
    ...
    location / {
        proxy_cache my_cache;
        proxy_cache_bypass $isCache;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;
        proxy_pass http://my_upstream;
    }
    # location for images goes here
}

我的方法正確嗎? 看起來它不會工作。

還有其他限制機器人負載的方法嗎? 當然,無需向他們發送 5xx 代碼(因為搜索引擎可以降低過於 5xx 化的網站的排名)。

謝謝!

如果您的內容頁面可能不同(即假設用戶已登錄並且該頁面包含“welcome John Doe”,則該版本的頁面可能會被緩存,因為每個請求都在更新緩存的副本(即登錄的人將更新緩存版本,包括他們的會話 cookie,這是不好的)。

最好執行類似於以下內容的操作:

map $http_user_agent $isNotBot {
  ~*bot    "";
  default  "IAmNotARobot";
}

server {
  ...
  location / {
    ...
    # Bypass the cache for humans
    proxy_cache_bypass $isNotBot;
    # Don't cache copies of requests from humans
    proxy_no_cache     $isNotBot;
    ...
  }
  ...
}

這樣,只有機器人的請求會被緩存以備將來的機器人請求使用,並且只有機器人會被緩存頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM