SatelliteWP / rocket-nginx

Rocket-Nginx is a Nginx configuration that makes WP-Rocket even faster by serving static pages directly without loading WordPress or PHP.
MIT License
579 stars 129 forks source link

Does not work properly in a site network environment #174

Closed ghost closed 1 year ago

ghost commented 2 years ago

Describe the bug Does not work properly in a site network environment.

Versions Nginx 1.18 Rocket-Nginx latest WP Rocket 3.10 Are you using Nginx as a reverse proxy (with Apache for instance) ? No

To Reproduce Steps to reproduce the behaviour:

  1. Turn on rocket-nginx debug mode
  2. Go to any subsite
  3. Open the developer console
  4. Can't see any http header output(rocket-nginx not working)
maximejobin commented 2 years ago

Is it a subdirectory or subdomain network ?

Can you post your configuration?

ghost commented 2 years ago

subdirectory

configuration is:

###################################################################################################
# Rocket-Nginx
#
# Rocket-Nginx is a NGINX configuration to speedup your WordPress
# website with the cache plugin WP-Rocket (http://wp-rocket.me)
#
# Author: Maxime Jobin
# Maintainer: SatelliteWP.com
# URL: https://github.com/satellitewp/rocket-nginx
#
# Tested with WP-Rocket version: 3.10.5
# Tested with NGINX: 1.21.4 (mainline)
#
# Version 3.0.0
#
###################################################################################################

# Add debug information into header
set $rocket_debug 1;

###################################################################################################
# Do not alter theses values
#
set $rocket_bypass          1;      # Should NGINX bypass WordPress and call cache file directly ?
set $rocket_encryption      "";     # Is GZIP accepted by client ?
set $rocket_file            "";     # Filename to look for
set $rocket_is_bypassed     "MISS"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Serving-Static
set $rocket_reason          "";     # Reason why cache file was not used. If cache file is used, what file was used
set $rocket_https_prefix    "";     # HTTPS prefix to use when cached files are using HTTPS
set $rocket_has_query_cache 0;      # Checks if a query string from URL is found from the cached query string
set $rocket_is_https        0;      # Checks if the request is HTTPS
set $rocket_support_webp    0;      # Checks if the request supports WebP
set $rocket_dynamic         "";     # Dynamic value to add to cached filename

###################################################################################################
# PAGE CACHE
#

include /www/server/nginx/rocket-nginx/conf.d/default/start.*.conf;

# Define Rocket-Nginx $is_args
set $rocket_is_args $is_args;

set $rocket_uri_path "";
if ($request_uri ~ "^([^?]*)(\?.*)?$") {
    set $rocket_uri_path $1;
}

# Is GZIP accepted by client ?
if ($http_accept_encoding ~ gzip) {
    set $rocket_encryption "_gzip";
}

# Is Brotli accepted by client ?
if ($http_accept_encoding ~ br) {
    set $rocket_encryption "";
}

# Is HTTPS request ?
if ($https = "on") { set $rocket_is_https 1; }
if ($http_x_forwarded_proto = "https") { set $rocket_is_https 1; }
if ($http_front_end_https = "on") { set $rocket_is_https 1; }
if ($http_x_forwarded_protocol = "https") { set $rocket_is_https 1; }
if ($http_x_forwarded_ssl = "on") { set $rocket_is_https 1; }
if ($http_x_url_scheme = "https") { set $rocket_is_https 1; }
if ($http_forwarded ~ /proto=https/) { set $rocket_is_https 1; }

if ($rocket_is_https = "1") {
    set $rocket_https_prefix "-https";
}

# Check if request supports WebP ?
if ($http_accept ~* "webp") {
    set $rocket_support_webp "1";
}

# Set mobile detection file path
set $rocket_mobile_detection "$document_root/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active";

# Query strings to ignore
set $rocket_args $args;
if ($rocket_args ~ (.*)(?:&|^)utm_source=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)utm_campaign=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)utm_medium=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)utm_expid=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)utm_term=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)utm_content=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)fb_action_ids=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)fb_action_types=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)fb_source=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)fbclid=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)_ga=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)gclid=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)age-verified=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)ao_noptimize=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)usqp=[^&]*(.*)) { set $rocket_args $1$2; }
if ($rocket_args ~ (.*)(?:&|^)cn-reloaded=[^&]*(.*)) { set $rocket_args $1$2; }

# Remove & at the beginning (if needed)
if ($rocket_args ~ ^&(.*)) { set $rocket_args $1;  }

# Do not count arguments if part of caching arguments
if ($rocket_args ~ ^\?$) {
    set $rocket_is_args "";
}

if ($rocket_args = "") {
    set $rocket_is_args "";
}

# Query string to cache

# File/URL to return IF we must bypass WordPress
# Desktop: index.html
# Gzip:    index.html_gzip
# HTTPS:   index-https.html
# Mobile:  index-mobile-https.html

set $rocket_file_start "index$rocket_https_prefix";

set $rocket_pre_url "/wp-content/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/";
set $rocket_pre_file "$document_root/wp-content/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/";

# Standard cache file format
set $rocket_url "$rocket_pre_url$rocket_file_start$rocket_dynamic.html";
set $rocket_file "$rocket_pre_file$rocket_file_start$rocket_dynamic.html";

# Check if gzip version cached file is available
if (-f "$rocket_file$rocket_encryption") {
    set $rocket_file "$rocket_file$rocket_encryption";
    set $rocket_url  "$rocket_url$rocket_encryption";
}

# Do not bypass if the cached file does not exist
if (!-f "$rocket_file") {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "MISS";
    set $rocket_reason "File not cached";
}

# Do not bypass if it's a POST request
if ($request_method = POST) {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "BYPASS";
    set $rocket_reason "POST request";
}

# Do not bypass if arguments are found (e.g. ?page=2)
if ($rocket_is_args) {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "BYPASS";
    set $rocket_reason "Arguments found";
}

# Do not bypass if the site is in maintenance mode
if (-f "$document_root/.maintenance") {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "BYPASS";
    set $rocket_reason "Maintenance mode";
}

# Do not bypass if one of those cookie if found
# wordpress_logged_in_[hash] : When a user is logged in, this cookie is created (we'd rather let WP-Rocket handle that)
# wp-postpass_[hash] : When a protected post requires a password, this cookie is created.
if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle|comment_author_|comment_author_email_|litepress_bypass)") {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "BYPASS";
    set $rocket_reason "Cookie";
}

if (-f "$rocket_mobile_detection") {
    set $rocket_bypass 0;
    set $rocket_is_bypassed "BYPASS";
    set $rocket_reason "Specific mobile cache activated";   
}

# If the bypass token is still on, let's bypass WordPress with the cached URL
if ($rocket_bypass = 1) {
    set $rocket_is_bypassed "HIT";
    set $rocket_reason "$rocket_url";
}

# Clear variables if debug is not needed
if ($rocket_debug = 0) {
    set $rocket_reason "";
    set $rocket_file "";
}

# If the bypass token is still on, rewrite according to the file linked to the request
if ($rocket_bypass = 1) {
    rewrite .* "$rocket_url" last;
}

# Add header to HTML cached files
location ~ /wp-content/cache/wp-rocket/.*html$ {
    etag on;
    add_header Vary "Accept-Encoding, Cookie";
    add_header Cache-Control "public";
    add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
    add_header X-Rocket-Nginx-Reason $rocket_reason;
    add_header X-Rocket-Nginx-File $rocket_file;
    include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;
    include /www/server/nginx/rocket-nginx/conf.d/default/http.*.conf;
}

# Do not gzip cached files that are already gzipped
location ~ /wp-content/cache/wp-rocket/.*_gzip$ {
    etag on;
    gzip off;
    types {}
    default_type text/html;
    add_header Content-Encoding gzip;
    add_header Vary "Accept-Encoding, Cookie";
    add_header Cache-Control "public";
    add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
    add_header X-Rocket-Nginx-Reason $rocket_reason;
    add_header X-Rocket-Nginx-File $rocket_file;
    include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;
    include /www/server/nginx/rocket-nginx/conf.d/default/http.*.conf;
}

# Debug header (when file is not cached)
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
add_header X-Rocket-Nginx-Reason $rocket_reason;
add_header X-Rocket-Nginx-File $rocket_file;
include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;

###################################################################################################
# BROWSER CSS CACHE
#
location ~* \.css$ {
    etag on;
    gzip_vary on;
    expires 30d;
    include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;
    include /www/server/nginx/rocket-nginx/conf.d/default/css.*.conf;
}

###################################################################################################
# BROWSER JS CACHE
#
location ~* \.js$ {
    etag on;
    gzip_vary on;
    expires 30d;
    include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;
    include /www/server/nginx/rocket-nginx/conf.d/default/js.*.conf;
}

###################################################################################################
# BROWSER MEDIA CACHE
#
location ~* \.(ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp)$ {
    etag on;
    expires 30d;
    include /www/server/nginx/rocket-nginx/conf.d/default/global.*.conf;
    include /www/server/nginx/rocket-nginx/conf.d/default/media.*.conf;
}
ghost commented 2 years ago

my cache dictionary like this:

root@litepress-server:/www/wwwroot/litepress.cn/wp-content/cache/wp-rocket/litepress.cn# ll
total 232
drwxr-xr-x  25 www www   4096 Jan 13 11:10 ./
drwxr-xr-x   3 www www   4096 Jan  2 15:42 ../
drwxr-xr-x   2 www www   4096 Jan  8 10:49 auth/
drwxr-xr-x   2 www www   4096 Jan  5 16:12 create/
drwxr-xr-x   3 www www   4096 Jan  6 13:48 experience-sharing/
drwxr-xr-x   3 www www   4096 Jan  7 12:39 forum-help/
drwxr-xr-x   3 www www   4096 Jan  5 14:33 forum-proposal/
drwxr-xr-x   6 www www   4096 Jan 11 00:04 forums/
drwxr-xr-x   2 www www   4096 Jan  5 13:23 forum-translate/
drwxr-xr-x   2 www www   4096 Jan 12 19:30 index/
-rwxr-xr-x   1 www www 113040 Jan 12 16:48 index-https.html*
-rwxr-xr-x   1 www www  17898 Jan 12 16:48 index-https.html_gzip*
drwxr-xr-x   2 www www   4096 Jan  9 20:19 license/
drwxr-xr-x   3 www www   4096 Jan  5 13:22 manual/
drwxr-xr-x   2 www www   4096 Jan  8 10:49 needs/
drwxr-xr-x   3 www www   4096 Jan 13 04:29 news/
drwxr-xr-x   7 www www   4096 Jan 12 22:04 page/
drwxr-xr-x   2 www www   4096 Jan  6 12:39 password-reset/
drwxr-xr-x   2 www www   4096 Jan  5 18:18 promoters/
drwxr-xr-x   2 www www   4096 Jan  6 14:13 Promoters/
drwxr-xr-x   6 www www   4096 Jan 13 11:42 store/
drwxr-xr-x   3 www www   4096 Jan 13 09:55 support/
drwxr-xr-x   4 www www   4096 Jan  5 11:48 svn/
drwxr-xr-x   3 www www   4096 Jan 12 11:53 topics/
drwxr-xr-x   5 www www   4096 Jan 10 09:17 translate/
drwxr-xr-x   2 www www   4096 Jan  9 08:36 trans-waiting-list/
drwxr-xr-x 182 www www   4096 Jan 13 11:29 user/

I only can see debug header in domain home page.

maximejobin commented 2 years ago

Please validate that your configuration looks like this one: https://github.com/SatelliteWP/rocket-nginx/wiki/Nginx-configuration-for-WordPress#creating-a-nginx-configuration-for-wordpress-multisite-subdirectories

It should work if Nginx is well configured.