Nginx

Nginx – 禁用 Wordpress 管理區域的積極記憶體

  • January 21, 2021

我現在有一個完整的 Nginx 安裝,帶有 PHP-FPM。來自 Apache 的世界,前面有 Nginx 代理。

似乎 Nginx 有自己的喜怒無常的記憶體,非常具有侵略性。有幾層記憶體:

  1. Nginx 的 fastcgi 記憶體本身。在我的 nginx.conf 我有以下設置:
fastcgi_cache_path        /var/run/nginx-cache  levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key         "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale   error timeout invalid_header http_500;
fastcgi_ignore_headers    Cache-Control Expires Set-Cookie;
  1. 然後是 php opcache。我暫時禁用了它:
;zend_extension=opcache.so
opcache.enable=0
opcache.enable_cli=0
opcache.memory_consumption=250
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=6000
opcache.revalidate_freq=600
opcache.fast_shutdown=1
  1. 我在伺服器塊中也有以下跳過記憶體指令:
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
       set $skip_cache 1;
}
if ($query_string != "") {
       set $skip_cache 1;
}

#Don't cache the following URLs
if ($request_uri ~* "/(wp-login.php|wp-admin|login.php|backend|admin)"){
   set $skip_cache 1;
}

#Don't cache if there is a cookie called PHPSESSID
if ($http_cookie ~* "PHPSESSID"){
   set $skip_cache 1;
}

#Don't cache if there is a cookie called wordpress_logged_in_[hash]
if ($http_cookie ~* "wordpress_logged_in_"){
   set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "cms/|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
       set $skip_cache 1;
}

# For the arc 
if ($request_uri ~* "site/|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
       set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
       set $skip_cache 1;
}
  1. 然後在 PHP 塊中,我使用了 skip_cache 的東西:
location ~ \.php$ {

     try_files $uri $uri/ =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;

       fastcgi_cache_bypass                $skip_cache;
     fastcgi_no_cache                    $skip_cache;
       fastcgi_cache                       WORDPRESS;
       fastcgi_cache_valid  200 301 302    60m;
}

儘管如此,我的管理螢幕被大量記憶體,以至於當我啟動外掛時,它會顯示舊螢幕。當外掛被停用時。只有當我手動刷新頁面時,我才能看到外掛確實被啟動了。

我錯過了什麼?

@MichaelHampton 建議的方式是恕我直言最好的(最簡單,最有效),但這是一種替代/附加方法,在某些情況下可能有用。它來自我的Wordpress/Nginx 教程。一些主題以一種非常糟糕的方式與記憶體標題混淆,所以我基本上想為網站的不同區域以不同的方式重寫所有標題。我還刪除了舊的 Pragma 標頭。

我為特定的 URL 或子目錄定義塊,以便我可以更精確地限製或控制標頭。

請注意,“more_clear_headers”和“add_header”是Headers More Nginx extension的一部分。如果您查看我在上面連結到的教程,第二部分,它會逐步說明如何使用該模組建構 Nginx - 這非常簡單。

# Rate limiting for login pages
limit_req_zone $binary_remote_addr zone=login:1m rate=5r/m;

# Rate limit wp-login.php to help prevent brute force attacks
location = /blog/wp-login.php {
 # Next line applies the rate limit defined above
 limit_req zone=login burst=3;       
 fastcgi_keep_conn on;
 fastcgi_intercept_errors on;
 fastcgi_pass   php;
 include        fastcgi_params;
 fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
 more_clear_headers "Cache-Control";
 more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires";

 # No caching
 more_clear_headers "Cache-Control";
 add_header Cache-Control "private, max-age=0, no-cache, no-store";
 more_clear_headers "Expires";

 # Debugging aid - remove
 # add_header Z_LOCATION "WP-LOGIN"; add_header URI $uri;
}

# Wordpress admin caching headers are generally set correctly, for pages and resources. The only reason we define
# this block separately is to avoid messing with the headers in the main php block.
# This is probably unnecessary because of the skip_cache variable and may be removed
location ~* wp-admin {
 fastcgi_keep_conn on;
 fastcgi_intercept_errors on;
 fastcgi_pass   php;
 include        fastcgi_params;
 fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

 # Debugging aid - remove
 # add_header Z_LOCATION "WP_ADMIN"; add_header URI $uri; add_header "Z_SKIP_CACHE" $skip_cache;
}

要添加其他內容以添加到邁克爾的答案中,我也使用此塊。它以第二種方式禁用 Wordpress Admin 的記憶體,並禁用提要、站點地圖、xmlrpc 和其他一些記憶體。一部分是針對 Wordpress,我懷疑一部分是針對我執行的自定義編寫的 PHP 網站。

if ($request_uri ~* "/wp-admin/|/admin-*|/purge*|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml|wp-cron") {
 set $fastcgi_nocache "true";
}   

引用自:https://serverfault.com/questions/832463