jworkman/ wordpress-fastcgi-cache
快速工具,用于管理启用fastcgi缓存的服务器
1.1.3
2021-05-03 15:10 UTC
Requires
- php: >=5.5.9
This package is auto-updated.
Last update: 2024-08-29 04:58:44 UTC
README
快速工具,用于管理启用fastcgi缓存的服务器
Nginx配置
为了设置Nginx使用fast cgi缓存,我们必须在Nginx的几个不同地方进行配置。此插件假定fast cgi缓存将位于目录/var/cache/nginxfastcgi
因此,首先让我们在服务器上创建该目录
mkdir -p /var/cache/nginxfastcgi
同时在您的nginx etc 目录中创建一个global目录
mkdir /etc/nginx/global
确保您将所有权设置为nginx用户正在运行的任何用户。运行以下命令,并用owner_name替换nginx使用的运行用户名
chown -R owner_name:owner_name /var/cache/nginxfastcgi
之后,让我们创建一个名为/etc/nginx/global/wordpress_cache.conf的文件,并将以下代码放入其中。您可以随意更改它,但是请注意$fastcgi_skipcache的内容。此外,请确保更新您的fast cgi套接字文件位置到php-fpm放置套接字的位置。
# example FastCGI cache exception rules
set $fastcgi_skipcache 0;
if ($http_cookie ~ "users_login_cookie") {
set $fastcgi_skipcache 1;
}
if ($request_method = POST) {
set $fastcgi_skipcache 1;
}
if ($query_string != "") {
set $fastcgi_skipcache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $fastcgi_skipcache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $fastcgi_skipcache 1;
}
if ( $request_uri ~ "/wp/wp-login.php" ) {
set $fastcgi_skipcache 1;
}
if ( $request_uri ~ "/wp/wp-admin" ) {
set $fastcgi_skipcache 1;
}
# Global restrictions configuration file.
# Designed to be included in any server {} block.</p>
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
location /admin {
return 301 /wp/wp-admin;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
# WordPress single blog rules.
# Designed to be included in any server {} block.
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Accept-Encoding' 'gzip';
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
add_header X-Cache $upstream_cache_status;
fastcgi_cache fastcgicache;
fastcgi_cache_bypass $fastcgi_skipcache;
fastcgi_no_cache $fastcgi_skipcache;
# This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)
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_param APP_ENV prod;
fastcgi_read_timeout 600;
}
创建全局wordpress缓存配置后,您需要通过添加文件/etc/nginx/global/cache.conf并在nginx中定义您的缓存系统,添加以下行
fastcgi_cache_path /var/cache/nginxfastcgi levels=1:2 keys_zone=fastcgicache:5m inactive=5m max_size=64m;
fastcgi_cache_key $scheme$request_method$host$request_uri;
# note: can also use HTTP headers to form the cache key, e.g.
fastcgi_cache_lock on;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_cache_valid 5m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
最后,在创建该文件后,您必须在nginx配置中包含它,通过在/etc/nginx/nginx.conf中的client_max_body_size下方添加以下行
include /etc/nginx/global/cache.conf;
之后,您必须在任何wordpress站点配置下替换以下fast cgi块
include global/wordpress_cache.conf;