您在這裡

使用Drupal的Fast 404來處理沒有找到的網址

James Qi 在 2017年12月9日 - 23:05 發表

  以前在翻看Drupal的Performance & Scalability插件的時候留意過有一個Fast 404模塊,知道是專門處理404報錯頁面,如果網站有很多404,這個模塊可以降低負載,但沒有詳細了解。

  最近還在不斷嘗試降低MySQL服務器的負載,今天幹脆把這個模塊安裝試了試,英文的說明看起來比較複雜,我就把README.txt中的内容把一些要點對照翻譯如下吧:

Fast_404 is Super Fast and Super Amazing. It is also very aggressive and
hard-core.
BE CAREFUL! TEST YOUR SITE THOROUGHLY AFTER ENABLING!

INSTALLATION INSTRUCTIONS
安裝指南

Basic Install
基本安裝

*NOTE, THIS ONLY CHECKS STATIC FILES AND NOT DRUPAL PATHS*
注意:這隻檢查靜态文件,不檢查Drupal路徑
(no settings.php modifications needed)
不需要修改settings.php文件
Step 1. Upload the module to your standard modules location (usually
  sites/all/modules).
Step 2. Enable the module in your modules page

Advanced Install
高級安裝
Step 1. Upload the module to your standard modules location (usually
  sites/all/modules).
Step 2. Place the code at the bottom of this file into your settings.php file
Step 3. Optionally, modify the include_once path if you did not put the module
  in sites/all/modules
Step 4. Enable the module in your modules page

-- Getting Extra Speed out of the Advanced Install --
-- 高級安裝得到的速度好處 --

#1) Check extensions from settings.php, not hook_boot.

  This method is faster as it checks for missing static files at bootstrap
  stage 3 rather than 7 when the module's hook_boot() is called. Also, if you
  are using aggressive or external caching, hook_boot is skipped, so you will
  *need* to do this.

  To enable this functionality, uncomment the line below near the bottom of the
  settings.php code:

  //fast_404_ext_check();

#2) Enable Drupal path checking

  This checks to see if the URL you being visited actually corresponds to a
  real page in Drupal. This feature has 2 switches:

    a). Global switch to turn this checking on and off:
        $conf['fast_404_path_check'] = FALSE;

    b). To perform this check in settings.php for the same reasons as listed
        above for the static file checking uncomment the following line in the
        settings.php code:
        //fast_404_path_check();

#3) Give the static file checking a kick in the pants!

  Static file checking does require you to keep an eye on the extension list
  as well as a bit of extra work with the preg_match (OK, a very small amount).
  Optionally, you can use whitelisting rather than blacklisting. To turn this
  on alter this setting in the settings.php:
  $conf['fast_404_url_whitelisting'] = FALSE;

  This setting requires you to do some serious testing to ensure your site's
  pages are all still loading. Also make sure this list is accurate for your
  site:

  $conf['fast_404_whitelist']  = array('index.php', 'rss.xml', 'install.php', 'cron.php', 'update.php', 'xmlrpc.php');

#4) Disallow imagecache file creation for anonymous users (NEW!)

  Normally the module skips out if imagecache is in the URL to the static file.
  There are times when you may not want this (it would be pretty easy for
  someone to take down your site by simply hammering you with URLs with
  imagecache in them.

  In an ideal situation, your logged in users should have verified the pages
  are loading correctly when they create them, so any needed imagecache images
  are already made. This new setting will make it so that imagecache URLs are
  not excluded and fall under the same static file rules as non-imagecache
  URLs. Set to false to enable this new feature.

  $conf['fast_404_allow_anon_imagecache'] = TRUE;

#5) Prevent conflicts with other modules (NEW!)

  Some performance modules create paths to files which don't exist on disk.
  These modules conflict with fast_404.  To workaround this limitation, you
  can whitelist the URL fragments used by these modules.

  For example if you are using the CDN module and have the far future date
  feature enabled add the following configuration:

  $conf['fast_404_string_whitelisting'] = array('cdn/farfuture');

  If you are using AdvAgg you can use this:
  $conf['fast_404_string_whitelisting'] = array('/advagg_');

-----------------------------------------------------------------
--- Copy the code below into the BOTTOM of your settings.php. ---
--- If you are using cacherouter, put this above cacherouter. ---
-----------------------------------------------------------------

  複制下面的代碼到settings.php底部,如果使用了緩存路由,放置到緩存路由的上面:

/**
 * Fast 404 settings:
 * 
 * Fast 404 will do two separate types of 404 checking. 
 * 
 * The first is to check for URLs which appear to be files or images. If Drupal
 * is handling these items, then they were not found in the file system and are
 * a 404.
 * 
 * The second is to check whether or not the URL exists in Drupal by checking
 * with the menu router, aliases and redirects. If the page does not exist, we
 * will server a fast 404 error and exit.
 */

# Load the fast_404.inc file. This is needed if you wish to do extension
# checking in settings.php.
include_once('./sites/all/modules/fast_404/fast_404.inc');

# Disallowed extensions. Any extension in here will not be served by Drupal and
# will get a fast 404.
# Default extension list, this is considered safe and is even in queue for
# Drupal 8 (see: http://drupal.org/node/76824).
$conf['fast_404_exts'] = '/^(?!robots).*\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i';

# If you use a private file system use the conf variable below and change the 
# 'sites/default/private' to your actual private files path
# $conf['fast_404_exts'] = '/^(?!robots)^(?!sites\/default\/private).*\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i';

# If you are using the Advanced Help module, the following config may be used
# to allow paths starting with 'help'.
$conf['fast_404_exts'] = '/^(?!help\/)(?!robots).*\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/';

# If you would prefer a stronger version of NO then return a 410 instead of a
# 404. This informs clients that not only is the resource currently not present
# but that it is not coming back and kindly do not ask again for it.
# Reference: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
# $conf['fast_404_return_gone'] = TRUE;

# Allow anonymous users to hit URLs containing 'imagecache' even if the file
# does not exist. TRUE is default behavior. If you know all imagecache
# variations are already made set this to FALSE.
$conf['fast_404_allow_anon_imagecache'] = TRUE;

# If you use FastCGI, uncomment this line to send the type of header it needs.
# Reference: http://php.net/manual/en/function.header.php
# $conf['fast_404_HTTP_status_method'] = 'FastCGI';

# Extension list requiring whitelisting to be activated **If you use this
# without whitelisting enabled your site will not load!
//$conf['fast_404_exts'] = '/\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|php|html?|xml)$/i';

# Default fast 404 error message.
$conf['fast_404_html'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server. (Fast 404)</p></body></html>';

# Check paths during bootstrap and see if they are legitimate.
$conf['fast_404_path_check'] = FALSE;
$conf['fast_404_path_check'] = TRUE;

# If enabled, you may add extensions such as xml and php to the
# $conf['fast_404_exts'] above. BE CAREFUL with this setting as some modules
# use their own php files and you need to be certain they do not bootstrap
# Drupal. If they do, you will need to whitelist them too.
$conf['fast_404_url_whitelisting'] = FALSE;

# Array of whitelisted files/urls. Used if whitelisting is set to TRUE.
$conf['fast_404_whitelist'] = array('index.php', 'rss.xml', 'install.php', 'cron.php', 'update.php', 'xmlrpc.php');

# Array of whitelisted URL fragment strings that conflict with fast_404.
$conf['fast_404_string_whitelisting'] = array('cdn/farfuture', '/advagg_');

# By default we will show a super plain 404, because usually errors like this are shown to browsers who only look at the headers.
# However, some cases (usually when checking paths for Drupal pages) you may want to show a regular 404 error. In this case you can
# specify a URL to another page and it will be read and displayed (it can't be redirected to because we have to give a 30x header to
# do that. This page needs to be in your docroot.
#$conf['fast_404_HTML_error_page'] = './my_page.html';

# By default the custom 404 page is only loaded for path checking. Load it for all 404s with the below option set to TRUE
$conf['fast_404_HTML_error_all_paths'] = FALSE;

# Call the extension checking now. This will skip any logging of 404s.
# Extension checking is safe to do from settings.php. There are many
# examples of this on Drupal.org.
//fast_404_ext_check();
fast_404_ext_check();

# Path checking. USE AT YOUR OWN RISK (only works with MySQL).
# Path checking at this phase is more dangerous, but faster. Normally
# Fast_404 will check paths during Drupal boostrap via hook_boot. Checking
# paths here is faster, but trickier as the Drupal database connections have
# not yet been made and the module must make a separate DB connection. Under
# most configurations this DB connection will be reused by Drupal so there
# is no waste.
# While this setting finds 404s faster, it adds a bit more load time to
# regular pages, so only use if you are spending too much CPU/Memory/DB on
# 404s and the trade-off is worth it.
# This setting will deliver 404s with less than 2MB of RAM.
//fast_404_path_check();
fast_404_path_check();

  紅色的部分我改的,實際使用後無論是不存在的文件或者不存在的路徑,都是返回的快速404頁面,不再載入Drupal的框架,速度肯定是快得多了,但網站日志中就不再有404的記錄了,對于發現一些錯誤網址的問題、排查來源不是很有利,但有弊有利吧,先這樣在幾個系列網站上安裝看看,是否都降低MySQL數據庫負載有效是關鍵。


2018年9月4日補充:這個辦法用了一段時間,降低MySQL負載的效果有一些,但也不是非常明顯,主要還得靠其它方式。這兩天準備在使用了Fast404的站點上安裝新模塊,但老是遇到報錯,說模塊找不到路徑,還有cron無法運行也看不到具體報錯信息,關閉這個模塊後,cron可以顯示報錯提示。

自由标簽:

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。