![nginx fastcgi_cache 仅适用于 1 个网站,其他网站均未获取缓存数据](https://linux22.com/image/691728/nginx%20fastcgi_cache%20%E4%BB%85%E9%80%82%E7%94%A8%E4%BA%8E%201%20%E4%B8%AA%E7%BD%91%E7%AB%99%EF%BC%8C%E5%85%B6%E4%BB%96%E7%BD%91%E7%AB%99%E5%9D%87%E6%9C%AA%E8%8E%B7%E5%8F%96%E7%BC%93%E5%AD%98%E6%95%B0%E6%8D%AE.png)
我正在设置一个新的网络服务器,并且几乎准备好上线,但无法解决最后一个问题 - 那就是我设置了多个 WordPress 网站。
每个 WordPress 网站都有自己的安装和安装目录以及单独的数据库。
我已经用该模块配置了 nginx fastcgi_cache
,它可以正常工作 - 但仅适用于我在服务器上设置的第一个网站。 后续每个网站都不会缓存任何内容。
在 Ubuntu Server 16.04 上运行 nginx/php7
这是我的nginx/nginx.conf
文件
user www-data;
worker_processes 1;
worker_rlimit_nofile 100000;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
server_tokens off;
reset_timedout_connection on;
add_header rt-Fastcgi-Cache $upstream_cache_status;
limit_req_status 403;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
fastcgi_read_timeout 300;
client_max_body_size 100m;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Log format Settings
log_format rt_cache '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] ''$http_host "$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent"';
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xm image/x-icon text/css text/plain text/x-component text/xml text/javascript;
# Fastcgi_Cache Additional entries
add_header Fastcgi-Cache $upstream_cache_status;
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;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80 default_server;
server_name _;
return 444;
}
}
这是“缓存工作”网站的配置
fastcgi_cache_path /var/www/html/1stwebsite.com/cache levels=1:2 keys_zone=1stwebsite.com:100m inactive=60m;
server {
server_name 1stwebsite.com www.1stwebsite.com;
access_log /var/www/html/1stwebsite.com/logs/access.log;
error_log /var/www/html/1stwebsite.com/logs/error.log;
root /var/www/html/1stwebsite.com/public/;
index index.php index.html;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
if ($http_cookie ~* "PHPSESSID"){
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache 1stwebsite.com;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge 1stwebsite.com "$scheme$request_method$host$1";
}
}
这是 1 个不起作用的缓存网站配置(除网站特定的信息和路径外,所有其他不起作用的网站配置都相同)
fastcgi_cache_path /var/www/html/2ndwebiste.co.uk/cache levels=1:2 keys_zone=2ndwebiste.co.uk:100m inactive=60m;
server {
server_name 2ndwebiste.co.uk www.2ndwebiste.co.uk;
access_log /var/www/html/2ndwebiste.co.uk/logs/access.log;
error_log /var/www/html/2ndwebiste.co.uk/logs/error.log;
root /var/www/html/2ndwebiste.co.uk/public/;
index index.php index.html index.htm;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/phpmyadmin|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
if ($http_cookie ~* "PHPSESSID"){
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location /phpmyadmin {
auth_basic "Admin Login";
auth_basic_user_file /etc/nginx/allow_phpmyadmin;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache 2ndwebiste.co.uk;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge 2ndwebiste.co.uk "$scheme$request_method$host$1";
}
}
我认为这与两个配置文件的最上面一行有关?
fastcgi_cache_path /var/www/html/2ndwebiste.co.uk/cache levels=1:2 keys_zone=2ndwebiste.co.uk:100m inactive=60m;
这是否需要放在主nginx.conf
文件中,而不是在每个单独的网站配置中,并为所有网站设置一个keys_zone
指令(即 WORDPRESS)
如果是这样 - 我是不是应该为每个单独的网站都设置一个缓存文件夹,而应该为所有网站设置一个中央缓存文件夹?
我认为该keys_zone
指令需要针对每个网站单独制定,因此为每个托管的网站创建单独的缓存位置。
感谢所有陪我走过终点线的人
更新日期:2016/10/17
根据 Tero 的要求,
请查看修改后的配置:
NGINX配置文件
user www-data;
worker_processes 1;
worker_rlimit_nofile 100000;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
server_tokens off;
reset_timedout_connection on;
add_header rt-Fastcgi-Cache $upstream_cache_status;
limit_req_status 403;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
fastcgi_read_timeout 300;
client_max_body_size 100m;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Log format Settings
log_format rt_cache '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] ''$http_host "$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent"';
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xm image/x-icon text/css text/plain text/x-component text/xml text/javascript;
# Fastcgi_Cache Additional entries
add_header Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_path /var/www/html/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;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80 default_server;
server_name _;
return 444;
}
}
WEBSITE1.CONF(缓存正常)
server {
server_name 1stwebsite.com www.1stwebsite.com;
access_log /var/www/html/1stwebsite.com/logs/access.log;
error_log /var/www/html/1stwebsite.com/logs/error.log;
root /var/www/html/1stwebsite.com/public/;
index index.php index.html;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
if ($http_cookie ~* "PHPSESSID"){
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
}
WEBSITE2.COM(不缓存任何内容)
server {
server_name 2ndwebiste.co.uk www.2ndwebiste.co.uk;
access_log /var/www/html/2ndwebiste.co.uk/logs/access.log;
error_log /var/www/html/2ndwebiste.co.uk/logs/error.log;
root /var/www/html/2ndwebiste.co.uk/public/;
index index.php index.html index.htm;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/phpmyadmin|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
if ($http_cookie ~* "PHPSESSID"){
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location /phpmyadmin {
auth_basic "Admin Login";
auth_basic_user_file /etc/nginx/allow_phpmyadmin;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
}
答案1
您在上下文中设置了fastcgi_cache_path
三次http
。只有其中一次以这种方式使用。
正确的程序是为所有虚拟主机设置一个缓存路径,然后fastcgi_cache_key
在每个server
块内定义该虚拟主机的缓存文件的唯一键。
例如,您可以对1stwebsite.com
服务器块执行以下操作:
fastcgi_cache_key 1stwebsite.com$request_uri;
fastcgi_cache_path
然后,您可以在每个虚拟主机中使用与您的设置相匹配的相同共享内存区域。例如,如果您只保留fastcgi_cache_path
您1stwebsite.com
定义的区域,那么您fastcgi_cache
在任何地方都看起来像这样:
fastcgi_cache 1stwebsite.com;