0

I am setting up a new web server and I'm nearly ready to go live but can't iron out 1 last issue - and thats I have multiple WordPress websites setup.

Each WordPress website has its own install and installation directory and a separate database.

I have configured nginx with the fastcgi_cache module and it works - but only for the very first website i set up on the server. Every subsequent website gets nothing cached.

Running nginx/php7 on Ubuntu Server 16.04

Here is my nginx/nginx.conf file

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;
    }
}

Here is the "cache working" websites config

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";
    }
}

Here is 1 of the non working cache websites config (all other non working site configs are the same apart from the website specific info and paths)

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";
    }
}

I think its to do with the very top line of both config files?

fastcgi_cache_path /var/www/html/2ndwebiste.co.uk/cache levels=1:2 keys_zone=2ndwebiste.co.uk:100m inactive=60m;

Does this need to be in the main nginx.conf file and not in each individual website config with a single keys_zone directive for all websites (ie WORDPRESS)

If so - am i not meant to have a cache folder for each individual website, should there just be 1 central cache folder for all websites?

I thought the keys_zone directive needs to be individual for each website, and thus created a seperate cache location for each website hosted.

Thanks to anybody that can walk with me over the finishing line

UPDATE 17/10/2016

As requested Tero,

Please see amended configs now:

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_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 (That caches ok)

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 (that doesnt cache anything)

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";
    }
}
NginxNewb
  • 1
  • 2

1 Answers1

0

You are setting the fastcgi_cache_path three times in the http context. Only one of those is used this way.

The correct procedure is to set one cache path for all virtual hosts, and then define fastcgi_cache_key inside each server block to define unique key for that virtual host's cached files.

For example, you could the following for 1stwebsite.com server block:

fastcgi_cache_key 1stwebsite.com$request_uri;

Then, you use same shared memory zone that matches your fastcgi_cache_path setting in each of your virtual hosts. For example, if you leave only fastcgi_cache_path for your 1stwebsite.com defined, then your fastcgi_cache looks like this everywhere:

fastcgi_cache 1stwebsite.com;
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Im not quite sure i fully follow your reply...apologies. – NginxNewb Oct 17 '16 at 11:49
  • So should it be like this...... take out the fastcgi_cache_path from each of the vhost configs and put a single one into nginx.conf, but instead of calling it per website just call it say "cache" - ie /var/www/html/cache. so directive reads fastcgi_cache_path /var/www/html/cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; Then in each vhost config just have fastcgi_cache_key respectivewebsite.com$request_uri; What do i then have as the fastcgi_cache settings in each vhost config if ive set it to WORDPRESS above? Sorry for the confusion – NginxNewb Oct 17 '16 at 11:56
  • All other `fastcgi_cache_*` settings are the same inside the virtual host configuration, except for the `fastcgi_cache`, which only refers to the one cache zone defined in `fastcgi_cache_path`. – Tero Kilkanen Oct 17 '16 at 12:29
  • Ok so i have changed my configs to this: – NginxNewb Oct 17 '16 at 13:42
  • 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; – NginxNewb Oct 17 '16 at 13:47
  • And made changes to these lines Removed fastcgi_cache_path /var/www/html/website1.com levels=1:2 keys_zone=website1.com:100m Inactive=60m; and fastcgi_cache_path /var/www/html/website2.com levels=1:2 keys_zone=website2.com:100m Inactive=60m; Changed fastcgi_cache to WORDPRESS Changed fastcgi_cache_pure to WORDPRESS – NginxNewb Oct 17 '16 at 13:49
  • But still website 1 is the only website to populate the cache folder. Website 2 does diddly. What have i missed? Thanks – NginxNewb Oct 17 '16 at 13:49
  • Forgot to put - the fastcgi_cache_* changes 3 up from this post are in the nginx.conf file. And the other changes 2 up from this post are in the vhost configs – NginxNewb Oct 17 '16 at 13:52
  • Please edit your original question with these configurations so they can be properly formatted. – Tero Kilkanen Oct 17 '16 at 14:19
  • Edited as requested Tero. Thankyou – NginxNewb Oct 17 '16 at 15:47