From 568f5ec5338dc6d399ab634d0966927248d9b5a5 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 23 Jul 2016 13:14:26 -0400 Subject: [PATCH 01/94] Added WooCommerce cookie support. --- rocket-nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 5f77227..2e1f5c4 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -82,7 +82,7 @@ if (-f "$document_root/.maintenance") { # 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_)") { +if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash)") { set $rocket_bypass 0; set $rocket_reason "Cookie"; } From da8cf5646c3d61f10a1a137c45e7e167e69f2557 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 23 Jul 2016 13:26:17 -0400 Subject: [PATCH 02/94] Manage the Mobile/Desktop bug. --- rocket-nginx.conf | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 2e1f5c4..71578a1 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -10,7 +10,7 @@ # Tested with WP-Rocket version: 2.6.15 # Tested with NGINX: 1.8.0 (stable) # -# Version 1.1 +# Version 1.2 # ################################################################################################### @@ -57,8 +57,13 @@ if ($rocket_hsts = "0") { } # File/URL to return IF we must bypass WordPress +# index-mobile.html +# index-mobile-https.html set $rocket_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; +set $rocket_mobile_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index-mobile$rocket_https_prefix.html$rocket_encryption"; + set $rocket_file "$document_root$rocket_url"; +set $rocket_mobile_file "$document_root$rocket_mobile_url"; # Do not bypass if it's a POST request @@ -87,6 +92,11 @@ if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_ca set $rocket_reason "Cookie"; } +if (-f "$rocket_mobile_file") { + set $rocket_bypass 0; + set $rocket_reason "Mobile/Desktop cache activated"; +} + # Do not bypass if the cached file does not exist if (!-f "$rocket_file") { set $rocket_bypass 0; From 0ae8cfef80350a306c60ebeaa1200246b825cb2d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 23 Jul 2016 13:27:44 -0400 Subject: [PATCH 03/94] Gzip and cache in browser all JS and CSS files. --- rocket-nginx.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 71578a1..86596ab 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -155,7 +155,7 @@ add_header X-Rocket-Nginx-File $rocket_file; ################################################################################################### # BROWSER CSS CACHE # -location ~ /wp-content/cache/min/.*\.css$ { +location ~* \.css$ { gzip_vary on; expires max; } @@ -164,7 +164,7 @@ location ~ /wp-content/cache/min/.*\.css$ { ################################################################################################### # BROWSER JS CACHE # -location ~ /wp-content/cache/min/.*\.js$ { +location ~* \.js$ { gzip_vary on; expires max; } From 8de1d24d11f71e58c65b697cb919b25a179126e2 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 23 Jul 2016 13:56:51 -0400 Subject: [PATCH 04/94] Cookie added for WP Touch. Specific mobile cache detection. --- rocket-nginx.conf | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 86596ab..25f7836 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -60,10 +60,8 @@ if ($rocket_hsts = "0") { # index-mobile.html # index-mobile-https.html set $rocket_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; -set $rocket_mobile_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index-mobile$rocket_https_prefix.html$rocket_encryption"; - set $rocket_file "$document_root$rocket_url"; -set $rocket_mobile_file "$document_root$rocket_mobile_url"; +set $rocket_mobile_detection "/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # Do not bypass if it's a POST request @@ -87,14 +85,14 @@ if (-f "$document_root/.maintenance") { # 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)") { +if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle|comment_author_|comment_author_email_)") { set $rocket_bypass 0; set $rocket_reason "Cookie"; } -if (-f "$rocket_mobile_file") { +if (-f "$rocket_mobile_detection") { set $rocket_bypass 0; - set $rocket_reason "Mobile/Desktop cache activated"; + set $rocket_reason "Specific mobile cache activated"; } # Do not bypass if the cached file does not exist From 61020abf249700c61cf60df92f7971ed1d2eae34 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 23 Jul 2016 14:06:58 -0400 Subject: [PATCH 05/94] Wrong path in the specific mobile detection. --- rocket-nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 25f7836..d1b4fc3 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -61,7 +61,7 @@ if ($rocket_hsts = "0") { # index-mobile-https.html set $rocket_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; set $rocket_file "$document_root$rocket_url"; -set $rocket_mobile_detection "/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; +set $rocket_mobile_detection "$document_root/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # Do not bypass if it's a POST request From ebbfeb01b5f214e49a976a5065178eb4d13c4917 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 5 Sep 2016 13:32:03 -0400 Subject: [PATCH 06/94] Change version --- rocket-nginx.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index d1b4fc3..4781772 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 2.6.15 -# Tested with NGINX: 1.8.0 (stable) +# Tested with WP-Rocket version: 2.8.11 +# Tested with NGINX: 1.11.3 (mainline) # # Version 1.2 # From 33d67f3d184ea0c5cf7b603a48edd94a07932d36 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 5 Sep 2016 13:32:51 -0400 Subject: [PATCH 07/94] Better if statement --- rocket-nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 4781772..02a001f 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -71,7 +71,7 @@ if ($request_method = POST) { } # Do not bypass if arguments are found (e.g. ?page=2) -if ($args != "") { +if ($is_args) { set $rocket_bypass 0; set $rocket_reason "Arguments found"; } From ec042f131a12f102254b12b707ad023ee011d64d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 5 Sep 2016 14:06:45 -0400 Subject: [PATCH 08/94] Add wp-content variables for directory and URL --- rocket-nginx.conf | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 02a001f..c33afce 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -14,8 +14,17 @@ # ################################################################################################### -set $rocket_debug 0; # Add debug information into header -set $rocket_hsts_value ""; # HTTP Strict Transport Security (to overwrite default) +# Add debug information into header +set $rocket_debug 0; + +# HTTP Strict Transport Security (to overwrite default) +set $rocket_hsts_value ""; + +# WP-Content directory (leave as is if you did not alter WordPress' default value) +set $rocket_wpcontent_directory "$document_root/wp-content"; + +# WP-Content URL (leave as is if you did not alter WordPress' default value) +set $rocket_wpcontent_url "/wp-content"; ################################################################################################### # Do not alter theses values @@ -59,9 +68,10 @@ if ($rocket_hsts = "0") { # File/URL to return IF we must bypass WordPress # index-mobile.html # index-mobile-https.html -set $rocket_url "/wp-content/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; -set $rocket_file "$document_root$rocket_url"; -set $rocket_mobile_detection "$document_root/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; +set $rocket_end "/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; +set $rocket_url "$rocket_wpcontent_url$rocket_end"; +set $rocket_file "$rocket_wpcontent_directory$rocket_end"; +set $rocket_mobile_detection "$rocket_wpcontent_directory/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # Do not bypass if it's a POST request From b41e115a532e14add529a9e0bf9585ffb891f442 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 5 Sep 2016 17:48:19 -0400 Subject: [PATCH 09/94] Add comment headers to adjust script automatically --- rocket-nginx.conf | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index c33afce..0acceb6 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -136,6 +136,10 @@ location ~ /wp-content/cache/wp-rocket/.*html$ { add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; expires 30d; + + #!# HEADER_HTTP #!# + #!# HEADER_HTTPS #!# + #!# HEADER_NON_GZIP #!# } # Do not gzip cached files that are already gzipped @@ -150,6 +154,10 @@ location ~ /wp-content/cache/wp-rocket/.*_gzip$ { add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; expires 30d; + + #!# HEADER_HTTP #!# + #!# HEADER_HTTPS #!# + #!# HEADER_GZIP #!# } # Debug header (when file is not cached) @@ -165,7 +173,9 @@ add_header X-Rocket-Nginx-File $rocket_file; # location ~* \.css$ { gzip_vary on; - expires max; + expires 30d; + + #!# HEADER_CSS #!# } @@ -174,7 +184,9 @@ location ~* \.css$ { # location ~* \.js$ { gzip_vary on; - expires max; + expires 30d; + + #!# HEADER_JS #!# } @@ -182,5 +194,7 @@ location ~* \.js$ { # BROWSER MEDIA CACHE # location ~* \.(ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { - expires max; + expires 30d; + + #!# HEADER_MEDIAS #!# } From a6c3bfabbc7992bf7147f057e09d597da4edf284 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 5 Sep 2016 17:53:44 -0400 Subject: [PATCH 10/94] Remove HTTPS header as it cannot be validated --- rocket-nginx.conf | 2 -- 1 file changed, 2 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 0acceb6..c7c3fd0 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -138,7 +138,6 @@ location ~ /wp-content/cache/wp-rocket/.*html$ { expires 30d; #!# HEADER_HTTP #!# - #!# HEADER_HTTPS #!# #!# HEADER_NON_GZIP #!# } @@ -156,7 +155,6 @@ location ~ /wp-content/cache/wp-rocket/.*_gzip$ { expires 30d; #!# HEADER_HTTP #!# - #!# HEADER_HTTPS #!# #!# HEADER_GZIP #!# } From 5fd7ed71c9375a85b27a910095125645de6cb52d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 7 Sep 2016 10:58:35 -0400 Subject: [PATCH 11/94] New bypass reason : specific mobile cache activated. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9a60f29..7e6b6c1 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ Reasons for not serving a cached file: * **Arguments found**: One or more argument was found in the request (e.g. ?page=2). * **Maintenance mode**: The .maintenance file was found. Therefore, let's WordPress handle what should be displayed. * **Cookie**: A specific cookie was found and tells to not serve the cached page (e.g. user is logged in, post with password). + * **Specific mobile cache activated**: If you activated specific cache (one for mobile and one for desktop) in WP-Rocket, HTML files (pages, posts, ...) won't be served directly because Rocket-Nginx cannot know if the request was made by mobile or desktop device. * **File not cached**: No cached file was found for that request. ## FAQ From 31d74c9a79e4dc7dbf3c63a79d5e6543bf9787a1 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 24 Nov 2016 20:23:00 -0500 Subject: [PATCH 12/94] Changing license from GPL to MIT --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e6b6c1..cca458b 100644 --- a/README.md +++ b/README.md @@ -136,4 +136,4 @@ No. People love benchmark as much as they hate them. All benchmarks have people Yes! Rocket-Nginx will detect if the request was made through HTTP or HTTPS and serve the right file depending on the request type. Both protocols are handled automagically since version 1.0. ## License -Released under the [GPL](http://www.gnu.org/licenses/gpl.html). See the link for details. +Released under the MIT License. See the license file for details. From 391fac10961e65577c18bf0f2694b3818788a390 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 24 Nov 2016 20:24:08 -0500 Subject: [PATCH 13/94] MIT License file --- license.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 license.md diff --git a/license.md b/license.md new file mode 100644 index 0000000..7b16b33 --- /dev/null +++ b/license.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Maxime Jobin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From c85fa83a7494883447cd3780cb55992da9e3dbc2 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 24 Nov 2016 20:28:07 -0500 Subject: [PATCH 14/94] Update version number --- rocket-nginx.conf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index c7c3fd0..a900294 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -7,10 +7,10 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 2.8.11 -# Tested with NGINX: 1.11.3 (mainline) +# Tested with WP-Rocket version: 2.8.23 +# Tested with NGINX: 1.11.6 (mainline) # -# Version 1.2 +# Version 2.0 # ################################################################################################### From e4cb2f9d19c8ac6c00435572669b1e0d04f4c2bb Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 26 Nov 2016 14:00:42 -0500 Subject: [PATCH 15/94] Dynamic wp-content folder --- rocket-nginx.conf | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index a900294..6b41e64 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -20,12 +20,6 @@ set $rocket_debug 0; # HTTP Strict Transport Security (to overwrite default) set $rocket_hsts_value ""; -# WP-Content directory (leave as is if you did not alter WordPress' default value) -set $rocket_wpcontent_directory "$document_root/wp-content"; - -# WP-Content URL (leave as is if you did not alter WordPress' default value) -set $rocket_wpcontent_url "/wp-content"; - ################################################################################################### # Do not alter theses values # @@ -66,12 +60,12 @@ if ($rocket_hsts = "0") { } # File/URL to return IF we must bypass WordPress -# index-mobile.html -# index-mobile-https.html +# Desktop: index.html or index-https.html +# Mobile: index-mobile.html or index-mobile-https.html set $rocket_end "/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; -set $rocket_url "$rocket_wpcontent_url$rocket_end"; -set $rocket_file "$rocket_wpcontent_directory$rocket_end"; -set $rocket_mobile_detection "$rocket_wpcontent_directory/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; +set $rocket_url "/#!# WP_CONTENT_URI #!#$rocket_end"; +set $rocket_file "$document_root/#!# WP_CONTENT_URI #!#$rocket_end"; +set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # Do not bypass if it's a POST request @@ -129,7 +123,7 @@ if ($rocket_bypass = 1) { } # Add header to HTML cached files -location ~ /wp-content/cache/wp-rocket/.*html$ { +location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { add_header Vary "Accept-Encoding, Cookie"; add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; add_header X-Rocket-Nginx-Reason $rocket_reason; @@ -142,7 +136,7 @@ location ~ /wp-content/cache/wp-rocket/.*html$ { } # Do not gzip cached files that are already gzipped -location ~ /wp-content/cache/wp-rocket/.*_gzip$ { +location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { gzip off; types {} default_type text/html; From 08be022ed45b3e521c8df968304f960df82a0a7d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 26 Nov 2016 14:08:09 -0500 Subject: [PATCH 16/94] Set Etag on and set cache control to no-cache for HTML files. --- rocket-nginx.conf | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.conf b/rocket-nginx.conf index 6b41e64..0d4136f 100644 --- a/rocket-nginx.conf +++ b/rocket-nginx.conf @@ -124,12 +124,14 @@ if ($rocket_bypass = 1) { # Add header to HTML cached files location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { + etag on; add_header Vary "Accept-Encoding, Cookie"; + add_header Cache-Control "no-cache"; add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - expires 30d; + #!# HEADER_HTTP #!# #!# HEADER_NON_GZIP #!# @@ -137,6 +139,7 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { # Do not gzip cached files that are already gzipped location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { + etag on; gzip off; types {} default_type text/html; @@ -146,7 +149,6 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - expires 30d; #!# HEADER_HTTP #!# #!# HEADER_GZIP #!# @@ -164,6 +166,7 @@ add_header X-Rocket-Nginx-File $rocket_file; # BROWSER CSS CACHE # location ~* \.css$ { + etag on; gzip_vary on; expires 30d; @@ -175,6 +178,7 @@ location ~* \.css$ { # BROWSER JS CACHE # location ~* \.js$ { + etag on; gzip_vary on; expires 30d; @@ -186,6 +190,7 @@ location ~* \.js$ { # BROWSER MEDIA CACHE # location ~* \.(ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { + etag on; expires 30d; #!# HEADER_MEDIAS #!# From 1f4385f9176a21253fa5b4c589a1fba2a96bc683 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 3 Dec 2016 13:28:43 -0500 Subject: [PATCH 17/94] Rename rocket-nginx.conf to rocket-nginx.tmpl --- rocket-nginx.conf => rocket-nginx.tmpl | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rocket-nginx.conf => rocket-nginx.tmpl (100%) diff --git a/rocket-nginx.conf b/rocket-nginx.tmpl similarity index 100% rename from rocket-nginx.conf rename to rocket-nginx.tmpl From 5992b5d249547ad4c0611615ad001ce9f386dd31 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 3 Dec 2016 14:34:30 -0500 Subject: [PATCH 18/94] New ini configuration. --- rocket-nginx.ini.disabled | 90 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 rocket-nginx.ini.disabled diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled new file mode 100644 index 0000000..71d9777 --- /dev/null +++ b/rocket-nginx.ini.disabled @@ -0,0 +1,90 @@ +; Rocket-Nginx configuration file + +; Default configuration +[default] + +; Enable debug information +; This adds debug headers to help understand what is going on when a problem occurs with the configuration +; Default value: false +debug = false + +; WP-Content folder +; Default value: wp-content +wp_content_folder = wp-content + +; Cookies +; Cookies can be specified to avoid serving a cached file. Some plugins may need to bypass the use of a +; file that was previously cached. List all cookies that must invalidate the use of cache. +; The cookie must contain the text. +; Example: cookie_invalidate[] = wordpress_logged_in_ +cookie_invalidate[] = wordpress_logged_in_ +cookie_invalidate[] = wp-postpass_ +cookie_invalidate[] = woocommerce_items_in_cart +cookie_invalidate[] = woocommerce_cart_hash +cookie_invalidate[] = wptouch_switch_toogle +cookie_invalidate[] = comment_author_ +cookie_invalidate[] = comment_author_email_ + +; HTTP(S) headers +; Custom headers can be added to the HTTP(S) requests for cached files (pages, posts, custom post types, +; etc). Brackets must be added at the end of the http_header variable name. That way, multiple headers +; can be added. +; Format: name "value" +; Example: http_header[] = My-Domain "www.mydomain.com" + +; HTTP(S) gzipped headers +; Custom headers can be added to the gzipped requests for cached files (pages, posts, custom post types, +; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers +; can be added. +; Format: name "value" +; Example: gzip_header[] = Is-Compressed "Yes" + +; HTTP Strict Transport Security (HSTS) +; HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web +; application through the use of a special response header. Once a supported browser receives this +; header that browser will prevent any communications from being sent over HTTP to the specified +; domain and will instead send all communications over HTTPS. It also prevents HTTPS click through +; prompts on browsers. +; Source: https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet + +; HSTS header value +; Default value: max-age=31536000; includeSubDomains +header_hsts = max-age=31536000; includeSubDomains + +; CSS headers +; Custom headers can be added to the CSS requests. +; Brackets must be added at the end of the css_header variable name. That way, multiple headers +; can be added. +; Format: name "value" +; Example: css_header[] = Is-CSS "Yes" + +; JavaScript headers +; Custom headers can be added to the javascript requests. +; Brackets must be added at the end of the js_header variable name. That way, multiple headers +; can be added. +; Format: name "value" +; Example: js_header[] = Is-JavaScript "Yes" + +; Medias extensions +; Medias extensions for images, icons, fonts and other cachable medias. All extensions are separated +; by pipes (|). +; Default value: ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg +media_extensions = ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg + +; Medias headers +; Custom headers can be added to the medias requests for +; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers +; can be added. +; Format: name "value" +; Example: media_header[] = Is-Media "Yes" + + +; Multiple sections can be created for multiple websites with different configurations +; It is recommeneded to use the default value as a base for custom validations. A new configuration +; file will be created for each new section created. + +; In the following example, the 'default' configuration is used and added a custom media_header. +;[example.com : default] +;media_header[] = Is-Media "Yes" + + From d263fa3ff831c7b9e78b24ebbcf70e06166e6691 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 7 Jan 2017 13:31:49 -0500 Subject: [PATCH 19/94] Parser... 1st incomplete draft. --- rocket-parser.php | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 rocket-parser.php diff --git a/rocket-parser.php b/rocket-parser.php new file mode 100644 index 0000000..ccdb56c --- /dev/null +++ b/rocket-parser.php @@ -0,0 +1,50 @@ +configFile, true); + $config = array(); + foreach($p_ini as $namespace => $properties) { + list($name, $extends) = explode(':', $namespace); + $name = trim($name); + $extends = trim($extends); + + // create namespace if necessary + if(!isset($config[$name])) $config[$name] = array(); + + // inherit base namespace + if(isset($p_ini[$extends])) { + foreach($p_ini[$extends] as $prop => $val) + $config[$name][$prop] = $val; + } + // overwrite / set current namespace values + foreach($properties as $prop => $val) + $config[$name][$prop] = $val; + } + return $config; + } + + public function start() { + + if (file_exists($this->configFile) { + } + } +} + + + From 764799936e4b7b9affcc84ef2b14a0cb8ad18945 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 8 Jan 2017 14:08:03 -0500 Subject: [PATCH 20/94] Ignore ini files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index e69de29..3a3cb4f 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +; Ignore all ini files +*.ini From 8de49070c4b55b245e0f03b1ce105a8988225877 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Mar 2017 16:39:04 -0500 Subject: [PATCH 21/94] Do not add *.conf files to repository --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3a3cb4f..0456e00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ ; Ignore all ini files *.ini +*.conf From 5511ffbdad0c1c8ae31212c729ba6d45b74bc24e Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 12 Mar 2017 21:28:02 -0400 Subject: [PATCH 22/94] Finished linking the template with the ini file. --- rocket-nginx.tmpl | 6 +- rocket-parser.php | 189 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 179 insertions(+), 16 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 0d4136f..8ebf1f9 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -15,7 +15,7 @@ ################################################################################################### # Add debug information into header -set $rocket_debug 0; +set $rocket_debug #!# DEBUG #!#; # HTTP Strict Transport Security (to overwrite default) set $rocket_hsts_value ""; @@ -89,7 +89,7 @@ if (-f "$document_root/.maintenance") { # 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_)") { +if ($http_cookie ~* "(#!# COOKIE_INVALIDATE #!#)") { set $rocket_bypass 0; set $rocket_reason "Cookie"; } @@ -189,7 +189,7 @@ location ~* \.js$ { ################################################################################################### # BROWSER MEDIA CACHE # -location ~* \.(ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { +location ~* \.(#!# EXTENSION_MEDIAS #!#)$ { etag on; expires 30d; diff --git a/rocket-parser.php b/rocket-parser.php index ccdb56c..30622b4 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -14,37 +14,200 @@ class RocketParser { - public $configFile = "rocket-nginx.ini"; + public $configFile = 'rocket-nginx.ini'; + public $templateFile = 'rocket-nginx.tmpl'; - function parseIniFile() { - $p_ini = parse_ini_file($this->configFile, true); + /** + * Parse the ini configuration file + */ + protected function parseIniFile() { + $data = parse_ini_file($this->configFile, true); $config = array(); - foreach($p_ini as $namespace => $properties) { - list($name, $extends) = explode(':', $namespace); - $name = trim($name); - $extends = trim($extends); + + foreach($data as $namespace => $properties) { + $parts = explode(':', $namespace); + $name = trim($parts[0]); + $extends = isset($parts[1]) ? trim($parts[1]) : null; // create namespace if necessary - if(!isset($config[$name])) $config[$name] = array(); + if(!isset($config[$name])) { + $config[$name] = array(); + } // inherit base namespace - if(isset($p_ini[$extends])) { - foreach($p_ini[$extends] as $prop => $val) + if(isset($data[$extends])) { + foreach($data[$extends] as $prop => $val) { $config[$name][$prop] = $val; + } } // overwrite / set current namespace values - foreach($properties as $prop => $val) + foreach($properties as $prop => $val) { $config[$name][$prop] = $val; + } } + return $config; } - public function start() { + /** + * Generate all configuration files + */ + protected function generateConfigurationFiles($config) { - if (file_exists($this->configFile) { + // Load template + $template = $this->getTemplate(); + + foreach($config as $name => $section) { + $output = $template; + + // Debug + $debug = false; + if (isset($section['debug']) && $section['debug'] === '1') { + $debug = true; + } + $output = str_replace('#!# DEBUG #!#', $debug ? '1' : '0', $output); + + // WP Content URI + $wp_content_folder = 'wp-content'; + if (isset($section['wp_content_folder']) && !empty($section['wp_content_folder'])) { + $wp_content_folder = $section['wp_content_folder']; + } + $output = str_replace('#!# WP_CONTENT_URI #!#', $wp_content_folder, $output); + + // Cookies + $cookies = ''; + if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { + $cookies = implode('|', $section['cookie_invalidate']); + } + $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); + + // HTTP headers + $header_http = ''; + if (isset($section['http_header']) && is_array($section['http_header'])) { + $header_http = $this->getGeneratedHeaders($section['http_header']); + } + $output = str_replace('#!# HEADER_HTTP #!#', $header_http, $output); + + // GZIP headers + $gzip_header = ''; + if (isset($section['gzip_header']) && is_array($section['gzip_header'])) { + $gzip_header = $this->getGeneratedHeaders($section['gzip_header']); + } + $output = str_replace('#!# HEADER_GZIP #!#', $gzip_header, $output); + + // Non-GZIP headers + $nongzip_header = ''; + if (isset($section['nongzip_header']) && is_array($section['nongzip_header'])) { + $nongzip_header = $this->getGeneratedHeaders($section['nongzip_header']); + } + $output = str_replace('#!# HEADER_NON_GZIP #!#', $nongzip_header, $output); + + // CSS headers + $css_header = ''; + if (isset($section['css_header']) && is_array($section['css_header'])) { + $css_header = $this->getGeneratedHeaders($section['css_header']); + } + $output = str_replace('#!# HEADER_CSS #!#', $css_header, $output); + + // JS headers + $js_header = ''; + if (isset($section['js_header']) && is_array($section['js_header'])) { + $js_header = $this->getGeneratedHeaders($section['js_header']); + } + $output = str_replace('#!# HEADER_JS #!#', $js_header, $output); + + // Media headers + $medias_header = ''; + if (isset($section['medias_header']) && is_array($section['medias_header'])) { + $medias_header = $this->getGeneratedHeaders($section['medias_header']); + } + $output = str_replace('#!# HEADER_MEDIAS #!#', $medias_header, $output); + + // Media extensions + $medias_extension = ''; + if (isset($section['medias_extension']) && is_array($section['medias_extension'])) { + $medias_extension = $this->getGeneratedHeaders($section['medias_extension']); + } + $output = str_replace('#!# EXTENSION_MEDIAS #!#', $medias_extension, $output); + + + // Output the file + file_put_contents($name . ".conf", $output); + } + + } + + /** + * Returns generated Nginx headers + * @param $headers array Headers + * + * @return string Nginx headers + */ + protected function getGeneratedHeaders($headers) { + $result = ''; + + if (isset($headers) && is_array($headers)) { + $iteration = 1; + + foreach ($headers as $name => $value) { + + if ($iteration > 1) { + $result .= "\t"; + } + $result .= "add_header {$name} \"{$value}\";\n"; + + $iteration++; + } + } + + return $result; + } + + /** + * Get the template file if it exists + */ + protected function getTemplate() { + + if (file_exists($this->templateFile) === false) { + die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . + "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); } + + return file_get_contents('rocket-nginx.tmpl'); + } + + /** + * Check if configuration file exists + */ + protected function checkConfigurationFile() { + if (file_exists($this->configFile) === false) { + die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . + "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); + } + } + + /** + * Generate configuration files + */ + public function go() { + $this->checkConfigurationFile(); + + $data = $this->parseIniFile(); + + var_dump($data); + + $this->generateConfigurationFiles($data); } } +// If file is included, we assume it will call the class automatically. +// Otherwise, let's generate the configuration files. +$includedFiles = count(get_included_files()); +if ($includedFiles == 0) { + $rp = new RocketParser(); + $rp->go(); +} + + From 997d9091e6ad7e304f44866e928c5c3cd49f9a0e Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 13 Apr 2017 21:18:04 -0400 Subject: [PATCH 23/94] Ini file values adjusted. --- rocket-nginx.ini.disabled | 58 ++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 71d9777..f932ec8 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -1,10 +1,14 @@ ; Rocket-Nginx configuration file +; Author: Maxime Jobin +; URL: https://github.com/maximejobin/rocket-nginx +; Version: 2.0 ; Default configuration [default] ; Enable debug information ; This adds debug headers to help understand what is going on when a problem occurs with the configuration +; Expected values: true, false ; Default value: false debug = false @@ -12,32 +16,44 @@ debug = false ; Default value: wp-content wp_content_folder = wp-content +; HTML Cache-Control +; Cache control header to use for html files +html_cache_control = "no-cache, no-store, must-revalidate" + ; Cookies ; Cookies can be specified to avoid serving a cached file. Some plugins may need to bypass the use of a ; file that was previously cached. List all cookies that must invalidate the use of cache. -; The cookie must contain the text. -; Example: cookie_invalidate[] = wordpress_logged_in_ -cookie_invalidate[] = wordpress_logged_in_ -cookie_invalidate[] = wp-postpass_ -cookie_invalidate[] = woocommerce_items_in_cart -cookie_invalidate[] = woocommerce_cart_hash -cookie_invalidate[] = wptouch_switch_toogle -cookie_invalidate[] = comment_author_ -cookie_invalidate[] = comment_author_email_ +; The cookie name must contain the text value set. +; Note that each value will be used in a REGEX. Make sure characters are escaped when needed. +; Example: cookie_invalidate[] = "wordpress_logged_in_" +cookie_invalidate[] = "wordpress_logged_in_" +cookie_invalidate[] = "wp\-postpass_" +cookie_invalidate[] = "woocommerce_items_in_cart" +cookie_invalidate[] = "woocommerce_cart_hash" +cookie_invalidate[] = "wptouch_switch_toogle" +cookie_invalidate[] = "comment_author_" +cookie_invalidate[] = "comment_author_email_" ; HTTP(S) headers ; Custom headers can be added to the HTTP(S) requests for cached files (pages, posts, custom post types, ; etc). Brackets must be added at the end of the http_header variable name. That way, multiple headers ; can be added. ; Format: name "value" -; Example: http_header[] = My-Domain "www.mydomain.com" +; Example: http_header[My-Domain] = "mydomain.com" ; HTTP(S) gzipped headers -; Custom headers can be added to the gzipped requests for cached files (pages, posts, custom post types, +; Custom headers can be added to the requests that serve already gzipped cached files (pages, posts, custom +; post types,etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple +; headers can be added. +; Format: name "value" +; Example: gzip_header[Is-Already-Gzipped] = "Yes" + +; HTTP(S) non gzipped headers +; Custom headers can be added to the requests that serve cached files (pages, posts, custom post types, ; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers ; can be added. ; Format: name "value" -; Example: gzip_header[] = Is-Compressed "Yes" +; Example: gzip_header[Is-Compressed] = "Yes" ; HTTP Strict Transport Security (HSTS) ; HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web @@ -49,42 +65,40 @@ cookie_invalidate[] = comment_author_email_ ; HSTS header value ; Default value: max-age=31536000; includeSubDomains -header_hsts = max-age=31536000; includeSubDomains +header_hsts = "max-age=31536000; includeSubDomains" ; CSS headers ; Custom headers can be added to the CSS requests. ; Brackets must be added at the end of the css_header variable name. That way, multiple headers ; can be added. ; Format: name "value" -; Example: css_header[] = Is-CSS "Yes" +; Example: css_header[Is-CSS] = "Yes" ; JavaScript headers ; Custom headers can be added to the javascript requests. ; Brackets must be added at the end of the js_header variable name. That way, multiple headers ; can be added. -; Format: name "value" -; Example: js_header[] = Is-JavaScript "Yes" +; Example: js_header[Is-JavaScript] = "Yes" ; Medias extensions ; Medias extensions for images, icons, fonts and other cachable medias. All extensions are separated ; by pipes (|). ; Default value: ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg -media_extensions = ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg +media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg" ; Medias headers ; Custom headers can be added to the medias requests for ; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers ; can be added. -; Format: name "value" -; Example: media_header[] = Is-Media "Yes" +; Example: media_header[Is-Media] = "Yes" -; Multiple sections can be created for multiple websites with different configurations -; It is recommeneded to use the default value as a base for custom validations. A new configuration +; Multiple sections can be created for multiple websites with different configurations. +; It is recommended to use the default values as a base for custom validations. A new configuration ; file will be created for each new section created. ; In the following example, the 'default' configuration is used and added a custom media_header. ;[example.com : default] -;media_header[] = Is-Media "Yes" +;media_header[Is-NewSection] = "Yes" From 3de170668e5375ec96c64911a500d7b1bbbad5be Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 13 Apr 2017 21:33:33 -0400 Subject: [PATCH 24/94] Updating template file. Modify Bypass header for something more clear. --- rocket-nginx.tmpl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 8ebf1f9..1a5077d 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 2.8.23 -# Tested with NGINX: 1.11.6 (mainline) +# Tested with WP-Rocket version: 2.9.11 +# Tested with NGINX: 1.11.13 (mainline) # # Version 2.0 # @@ -126,13 +126,11 @@ if ($rocket_bypass = 1) { location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { etag on; add_header Vary "Accept-Encoding, Cookie"; - add_header Cache-Control "no-cache"; + add_header Cache-Control "#!# HTML_CACHE_CONTROL #!#"; add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - - #!# HEADER_HTTP #!# #!# HEADER_NON_GZIP #!# } @@ -145,11 +143,11 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { default_type text/html; add_header Content-Encoding gzip; add_header Vary "Accept-Encoding, Cookie"; + add_header Cache-Control "#!# HTML_CACHE_CONTROL #!#"; add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - #!# HEADER_HTTP #!# #!# HEADER_GZIP #!# } @@ -169,7 +167,6 @@ location ~* \.css$ { etag on; gzip_vary on; expires 30d; - #!# HEADER_CSS #!# } @@ -181,7 +178,6 @@ location ~* \.js$ { etag on; gzip_vary on; expires 30d; - #!# HEADER_JS #!# } @@ -192,6 +188,5 @@ location ~* \.js$ { location ~* \.(#!# EXTENSION_MEDIAS #!#)$ { etag on; expires 30d; - #!# HEADER_MEDIAS #!# } From 35adf6ac2c35b69d4e07b423c1c148720ecfc526 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 13 Apr 2017 21:33:59 -0400 Subject: [PATCH 25/94] Updating template file. Modify Bypass header for something more clear. --- rocket-nginx.tmpl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 1a5077d..3ccff99 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -26,7 +26,7 @@ set $rocket_hsts_value ""; 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 use -set $rocket_is_bypassed "No"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Bypass +set $rocket_is_bypassed "No"; # 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_hsts 0; # Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS @@ -127,7 +127,7 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { etag on; add_header Vary "Accept-Encoding, Cookie"; add_header Cache-Control "#!# HTML_CACHE_CONTROL #!#"; - add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; + 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; add_header Strict-Transport-Security "$rocket_hsts_value"; @@ -144,7 +144,7 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { add_header Content-Encoding gzip; add_header Vary "Accept-Encoding, Cookie"; add_header Cache-Control "#!# HTML_CACHE_CONTROL #!#"; - add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; + 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; add_header Strict-Transport-Security "$rocket_hsts_value"; @@ -153,7 +153,7 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { } # Debug header (when file is not cached) -add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed; +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; From 821c20f19da9697ab941a07475bd8468d5d432ae Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 13 Apr 2017 21:35:01 -0400 Subject: [PATCH 26/94] Finalizing new PHP parser. --- rocket-parser.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/rocket-parser.php b/rocket-parser.php index 30622b4..63b99cb 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -74,6 +74,13 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# WP_CONTENT_URI #!#', $wp_content_folder, $output); + // Cache Control + $html_cache_control = ''; + if (isset($section['html_cache_control']) && !empty($section['html_cache_control'])) { + $html_cache_control = $section['html_cache_control']; + } + $output = str_replace('#!# HTML_CACHE_CONTROL #!#', $html_cache_control, $output); + // Cookies $cookies = ''; if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { @@ -194,8 +201,6 @@ public function go() { $data = $this->parseIniFile(); - var_dump($data); - $this->generateConfigurationFiles($data); } } @@ -203,7 +208,8 @@ public function go() { // If file is included, we assume it will call the class automatically. // Otherwise, let's generate the configuration files. $includedFiles = count(get_included_files()); -if ($includedFiles == 0) { + +if ($includedFiles === 1) { $rp = new RocketParser(); $rp->go(); } From 714e22f42c17dd8991b4a39ba2c04b24c365f23b Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 13 Apr 2017 21:56:05 -0400 Subject: [PATCH 27/94] Change header name in documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cca458b..83fa37e 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ To: `set $rocket_debug 1;` The following header is present no matter if debug is set to 0 or 1: - * **X-Rocket-Nginx-Bypass**: Did the configuration served the cached file directly (did it bypass WordPress): Yes or No. + * **X-Rocket-Nginx-Serving-Static**: Did the configuration served the cached file directly (did it bypass WordPress): Yes or No. This will add the following headers to your response request: * **X-Rocket-Nginx-Reason**: If Bypass is set to "No", what is the reason for calling WordPress. If "Yes", what is the file used (URL). From b320c15041cbf24fdf49219b2bc7ca779fc02695 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 17 Apr 2017 19:35:30 -0400 Subject: [PATCH 28/94] Edit documentation to fit version 2.0 --- README.md | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 83fa37e..7651435 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Make sure you test that your tasks still run after this change! In order to use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/maximejobin/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. -Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP-Rocket. +Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP-Rocket. That said, you can generate as many configuration files as needed. You can create a folder `rocket-nginx` directory in your Nginx configuration directory. If you are using Ubuntu, your Nginx configuration (nginx.conf) should be found in: `/etc/nginx/`. @@ -53,6 +53,14 @@ To install, you can: git clone https://github.com/maximejobin/rocket-nginx.git ``` +Since version 2.0, the configuration must be generated. To generate the default configuration, you must rename the disabled ini file and run the configuration parser: +``` +cd rocket-nginx +mv rocket-nginx.ini.disabled rocket-nginx.ini +php rocket-parser.php +``` +This will generate the `default.conf` configuration that can be included for all websites. If you need to alter the default configuration, you can edit the ini file and add another section at the bottom of the file. + Then, in your configuration file, you must [include](http://nginx.org/en/docs/ngx_core_module.html#include) the configuration. If your websites configurations are in `/etc/nginx/sites-available`, you need to alter your configuration: ``` @@ -60,7 +68,7 @@ server { ... # Rocket-Nginx configuration - include rocket-nginx/rocket-nginx.conf; + include rocket-nginx/default.conf; ... } @@ -77,27 +85,7 @@ That's it. ## Configuration There is no configuration to do. It will work out of the box. But, you can edit a couple of things... -#### Cache expiration -By default, files such as CSS, JS and medias (images, fonts, ...) are cached until December 31st, 2037 (`expires max;`). As of the latest Nginx stable version, it is not possible to set these values into variables. You can manually change the values if needed. - -#### HTTP Strict Transport Security (HSTS) -By default, HSTS (see [HTTP Strict Transport Security](https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security)) is enabled by default for all subdomains and the cache will expire after 1 year. If you want to overwrite the default value, you can simply insert your desired value between the quotes of the `$rocket_hsts_value` variable at the top of the `rocket-nginx.conf` file. - -If you leave the variable as-is: - - set $rocket_hsts_value ""; - -Rocket-Nginx will display the default HSTS header: - - Strict-Transport-Security: max-age=16070400; includeSubDomains - -If you change the variable value with another value: - - set $rocket_hsts_value "my-value-here"; - -Rocket-Nginx will display the following HSTS header: - - Strict-Transport-Security: my-value-here +Just open the `rocket-nginx.ini` file and see all the options in it. ## Debug From f1bf706eb7bf4695a40b3117370032679f5e4172 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 17 Apr 2017 20:25:17 -0400 Subject: [PATCH 29/94] Added HSTS configuration. --- rocket-nginx.tmpl | 11 ++--------- rocket-parser.php | 7 +++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 3ccff99..bde8717 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -17,8 +17,6 @@ # Add debug information into header set $rocket_debug #!# DEBUG #!#; -# HTTP Strict Transport Security (to overwrite default) -set $rocket_hsts_value ""; ################################################################################################### # Do not alter theses values @@ -31,8 +29,8 @@ set $rocket_reason ""; # Reason why cache file was not used. If cache file is set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are using HTTPS set $rocket_hsts 0; # Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS -# HSTS Default value : 1 year, include subdomains. -set $rocket_hsts_value_default "max-age=31536000; includeSubDomains"; +# HSTS value +set $rocket_hsts_value "#!# HEADER_HSTS #!#"; ################################################################################################### # PAGE CACHE @@ -49,11 +47,6 @@ if ($https = "on") { set $rocket_hsts 1; } -# If HSTS value is not set, use default value -if ($rocket_hsts_value = "") { - set $rocket_hsts_value "$rocket_hsts_value_default"; -} - # If HSTS is disabled, unset HSTS set for Rocket-Nginx configuration if ($rocket_hsts = "0") { set $rocket_hsts_value ""; diff --git a/rocket-parser.php b/rocket-parser.php index 63b99cb..bc2466f 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -81,6 +81,13 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# HTML_CACHE_CONTROL #!#', $html_cache_control, $output); + // HSTS + $header_hsts = ''; + if (isset($section['header_hsts']) && !empty($section['header_hsts'])) { + $header_hsts = $section['header_hsts']; + } + $output = str_replace('#!# HEADER_HSTS #!#', $header_hsts, $output); + // Cookies $cookies = ''; if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { From 9bd080149595c5427ca5441a5ffedd69aced99c5 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 17 Apr 2017 21:23:27 -0400 Subject: [PATCH 30/94] Added an example to the documentation. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 7651435..8c267cb 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,16 @@ That's it. There is no configuration to do. It will work out of the box. But, you can edit a couple of things... Just open the `rocket-nginx.ini` file and see all the options in it. + +You can add a new section based on the default configuration like this: +``` +# This creates the new section and will generate a new configuration +[example.com : default] + +# This will add a new header named "Is-NewSection" with the value "Yes" to the medias section +media_header[Is-NewSection] = "Yes" +``` + ## Debug From 716b1a527e2238c04ee4c69420b35ffb8f9bf9fd Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 19 Apr 2017 14:45:27 -0400 Subject: [PATCH 31/94] Changed rename (mv) to copy (cp) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c267cb..f196dd0 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ To install, you can: Since version 2.0, the configuration must be generated. To generate the default configuration, you must rename the disabled ini file and run the configuration parser: ``` cd rocket-nginx -mv rocket-nginx.ini.disabled rocket-nginx.ini +cp rocket-nginx.ini.disabled rocket-nginx.ini php rocket-parser.php ``` This will generate the `default.conf` configuration that can be included for all websites. If you need to alter the default configuration, you can edit the ini file and add another section at the bottom of the file. From 5e8cbe1d9f1ba9cee35138ace1cb1765fffe3a03 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 19 Apr 2017 14:49:33 -0400 Subject: [PATCH 32/94] Added more information on configuration edits. --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f196dd0..517bf53 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,14 @@ You can add a new section based on the default configuration like this: media_header[Is-NewSection] = "Yes" ``` - +Once you edit the ini file, you must regenerate your Nginx configuration file by running the parser: + +``` +php rocket-parser.php +``` + +Then, newly added or modified sections will generate update configuration file (*.conf). + ## Debug You may want to check if your files are served directly by Nginx and not calling any PHP. To do that, open the `rocket-nginx.conf` file and change the debug value from: From 290a0b68a5218b46fbcdb803df22402fa130db7c Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 11 May 2017 15:43:28 -0400 Subject: [PATCH 33/94] Change the way configuration information are written in files. --- rocket-parser.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/rocket-parser.php b/rocket-parser.php index bc2466f..aaa9629 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -8,7 +8,7 @@ * Author: Maxime Jobin * URL: https://github.com/maximejobin/rocket-nginx * -* Version 2.0 +* Version 2.0.1 * **************************************************************************************************/ @@ -146,9 +146,20 @@ protected function generateConfigurationFiles($config) { // Output the file - file_put_contents($name . ".conf", $output); - } + $filename = $name . ".conf"; + + if (!$handle = fopen($filename, 'w')) { + echo "Cannot open file: {$filename}.\n"; + continue; + } + if (fwrite($handle, $output) === FALSE) { + echo "Cannot write to file {$filename}.\n"; + continue; + } + + fclose($handle); + } } /** @@ -217,8 +228,10 @@ public function go() { $includedFiles = count(get_included_files()); if ($includedFiles === 1) { + error_reporting(-1); + $rp = new RocketParser(); - $rp->go(); + $rp->go(); } From 7f7f1286852eb68ee58f34849885b9b11bd01bbb Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 1 Jun 2017 11:26:59 -0400 Subject: [PATCH 34/94] Correction as suggested by Stuart Nelson --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 517bf53..1bd2f7e 100644 --- a/README.md +++ b/README.md @@ -104,15 +104,25 @@ php rocket-parser.php Then, newly added or modified sections will generate update configuration file (*.conf). +Finally, **each time** you generate (or regenerate) the configurations files, you have to: + +1. Test it to make sure it did not produce any error: + + `nginx -t` + +1. Reload the configuration: + + `service nginx reload` + ## Debug -You may want to check if your files are served directly by Nginx and not calling any PHP. To do that, open the `rocket-nginx.conf` file and change the debug value from: +You may want to check if your files are served directly by Nginx and not calling any PHP. To do that, open the `rocket-nginx.ini` file and change the debug value from: -`set $rocket_debug 0;` +`debug = false` To: -`set $rocket_debug 1;` +`debug = true` The following header is present no matter if debug is set to 0 or 1: * **X-Rocket-Nginx-Serving-Static**: Did the configuration served the cached file directly (did it bypass WordPress): Yes or No. From 32b1214131321936ca830a08ddcf1d10d15e256d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 5 Oct 2017 19:28:39 -0400 Subject: [PATCH 35/94] Add Brotli compatibility based on issue #48 --- rocket-nginx.tmpl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index bde8717..eaedfd8 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -10,7 +10,7 @@ # Tested with WP-Rocket version: 2.9.11 # Tested with NGINX: 1.11.13 (mainline) # -# Version 2.0 +# Version 2.1 # ################################################################################################### @@ -41,6 +41,11 @@ if ($http_accept_encoding ~ gzip) { set $rocket_encryption "_gzip"; } +# Is Brotli accepted by client ? +if ($http_accept_encoding ~ br) { + set $rocket_encryption ""; +} + # Is SSL request ? if ($https = "on") { set $rocket_https_prefix "-https"; From e96b0b3f0aef3f01a394af58f930b2bf999dd947 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 5 Oct 2017 19:36:19 -0400 Subject: [PATCH 36/94] Update test version. --- rocket-nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index eaedfd8..4cb2e08 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 2.9.11 -# Tested with NGINX: 1.11.13 (mainline) +# Tested with WP-Rocket version: 2.10.9 +# Tested with NGINX: 1.13.4 (mainline) # # Version 2.1 # From ebb1826df11741f89dbe366ac78996e21ddbc092 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 19 Oct 2017 16:06:11 -0400 Subject: [PATCH 37/94] Forgot to upgrade version number --- rocket-nginx.ini.disabled | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index f932ec8..07a1140 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -1,7 +1,7 @@ ; Rocket-Nginx configuration file ; Author: Maxime Jobin ; URL: https://github.com/maximejobin/rocket-nginx -; Version: 2.0 +; Version: 2.1 ; Default configuration [default] From 4806e0b4c53d2fc34d02c1ccb0045591f216806a Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 19 Oct 2017 16:06:31 -0400 Subject: [PATCH 38/94] Forgot to upgrade version number --- rocket-parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-parser.php b/rocket-parser.php index aaa9629..b8e0017 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -8,7 +8,7 @@ * Author: Maxime Jobin * URL: https://github.com/maximejobin/rocket-nginx * -* Version 2.0.1 +* Version 2.1 * **************************************************************************************************/ From b562ecba3fb74cb8dff3336d43dadf7835af531d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 28 Oct 2017 21:39:06 -0400 Subject: [PATCH 39/94] Media extensions not populated correctly. --- rocket-nginx.ini.disabled | 2 +- rocket-nginx.tmpl | 2 +- rocket-parser.php | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 07a1140..332ba11 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -1,7 +1,7 @@ ; Rocket-Nginx configuration file ; Author: Maxime Jobin ; URL: https://github.com/maximejobin/rocket-nginx -; Version: 2.1 +; Version: 2.1.1 ; Default configuration [default] diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 4cb2e08..2950b90 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -10,7 +10,7 @@ # Tested with WP-Rocket version: 2.10.9 # Tested with NGINX: 1.13.4 (mainline) # -# Version 2.1 +# Version 2.1.1 # ################################################################################################### diff --git a/rocket-parser.php b/rocket-parser.php index b8e0017..c640c42 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -8,7 +8,7 @@ * Author: Maxime Jobin * URL: https://github.com/maximejobin/rocket-nginx * -* Version 2.1 +* Version 2.1.1 * **************************************************************************************************/ @@ -138,11 +138,11 @@ protected function generateConfigurationFiles($config) { $output = str_replace('#!# HEADER_MEDIAS #!#', $medias_header, $output); // Media extensions - $medias_extension = ''; - if (isset($section['medias_extension']) && is_array($section['medias_extension'])) { - $medias_extension = $this->getGeneratedHeaders($section['medias_extension']); + $media_extensions = ''; + if (isset($section['media_extensions']) && !empty($section['media_extensions'])) { + $media_extensions = $section['media_extensions']; } - $output = str_replace('#!# EXTENSION_MEDIAS #!#', $medias_extension, $output); + $output = str_replace('#!# EXTENSION_MEDIAS #!#', $media_extensions, $output); // Output the file @@ -218,7 +218,6 @@ public function go() { $this->checkConfigurationFile(); $data = $this->parseIniFile(); - $this->generateConfigurationFiles($data); } } From 1a7d0530929f793e0d78f57140d32dcc514bda52 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Tue, 21 Nov 2017 12:33:55 -0500 Subject: [PATCH 40/94] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bd2f7e..8ecae04 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ Rocket-Nginx Rocket-Nginx is a [Nginx](http://nginx.org) configuration for the [WordPress](http://wordpress.org) cache plugin [WP-Rocket](http://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. +This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com). + +![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2017/03/logo-dark.png) + ## Table of Contents 1. [Contributors](#contributors) @@ -15,7 +19,7 @@ Rocket-Nginx is a [Nginx](http://nginx.org) configuration for the [WordPress](ht ## Contributors -The configuration was created and is maintained by [Maxime Jobin](http://www.maximejobin.com) ([@maximejobin](http://twitter.com/maximejobin)). +The configuration was created and is maintained by [Maxime Jobin](http://www.maximejobin.com) ([@maximejobin](http://twitter.com/maximejobin)). ## Before You Start As the configuration's goal is to serve cached files directly without having to execute any PHP from WordPress, this may cause your scheduled jobs to not be called. As you may already know, WP-Cron jobs are not real cron jobs and are executed only when you have visits on your site. From 110c0b091761543539e22ba46138b4867d3271a6 Mon Sep 17 00:00:00 2001 From: Mathieu Aubin Date: Fri, 15 Dec 2017 16:06:34 -0500 Subject: [PATCH 41/94] Updates README.md Updates all non-https links to https-enabled (and verified) Shortens example output code to nil from >/dev/null 2>&1 to &>/dev/null --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8ecae04..b3db153 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Rocket-Nginx ============ -Rocket-Nginx is a [Nginx](http://nginx.org) configuration for the [WordPress](http://wordpress.org) cache plugin [WP-Rocket](http://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. +Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](https://wordpress.org) cache plugin [WP-Rocket](https://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com). @@ -19,7 +19,7 @@ This project is sponsored by SatelliteWP, a [WordPress maintenance service](http ## Contributors -The configuration was created and is maintained by [Maxime Jobin](http://www.maximejobin.com) ([@maximejobin](http://twitter.com/maximejobin)). +The configuration was created and is maintained by [Maxime Jobin](https://www.maximejobin.com) ([@maximejobin](https://twitter.com/maximejobin)). ## Before You Start As the configuration's goal is to serve cached files directly without having to execute any PHP from WordPress, this may cause your scheduled jobs to not be called. As you may already know, WP-Cron jobs are not real cron jobs and are executed only when you have visits on your site. @@ -31,15 +31,15 @@ To disable WordPress cron job, add the following line to your `wp-config.php`: Then, manually a cron job every 15 minutes (it should be enough for most websites): -`*/15 * * * * wget -q -O - http://www.website.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1` +`*/15 * * * * wget -q -O - http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null` or -`*/15 * * * * curl http://www.website.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1` +`*/15 * * * * curl http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null` or -`*/15 * * * * cd /home/user/public_html; php wp-cron.php > /dev/null 2>&1` +`*/15 * * * * cd /home/user/public_html; php wp-cron.php &>/dev/null` Make sure you test that your tasks still run after this change! @@ -65,7 +65,7 @@ php rocket-parser.php ``` This will generate the `default.conf` configuration that can be included for all websites. If you need to alter the default configuration, you can edit the ini file and add another section at the bottom of the file. -Then, in your configuration file, you must [include](http://nginx.org/en/docs/ngx_core_module.html#include) the configuration. If your websites configurations are in `/etc/nginx/sites-available`, you need to alter your configuration: +Then, in your configuration file, you must [include](https://nginx.org/en/docs/ngx_core_module.html#include) the configuration. If your websites configurations are in `/etc/nginx/sites-available`, you need to alter your configuration: ``` server { From 7b337f5ff52388cd452b36cf11b99b929cc63c7f Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 22 Jan 2018 09:49:08 -0500 Subject: [PATCH 42/94] Edit image URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3db153..781bd62 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](h This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com). -![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2017/03/logo-dark.png) +[![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2017/03/logo-dark.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en) ## Table of Contents From 258420b636db7793b03240e5cf921b8eba20a40f Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 11 Jun 2018 16:07:48 -0400 Subject: [PATCH 43/94] Update compatibility versions --- rocket-nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 2950b90..121b9ec 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 2.10.9 -# Tested with NGINX: 1.13.4 (mainline) +# Tested with WP-Rocket version: 3.0.5.1 +# Tested with NGINX: 1.15 (mainline) # # Version 2.1.1 # From 8a35f1fc0db81b01e2ac83b86cebbf9033e9d9f1 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 1 Nov 2018 10:53:05 -0400 Subject: [PATCH 44/94] Fixed an error in the comment example --- rocket-nginx.ini.disabled | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 332ba11..0fe42ce 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -53,7 +53,7 @@ cookie_invalidate[] = "comment_author_email_" ; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers ; can be added. ; Format: name "value" -; Example: gzip_header[Is-Compressed] = "Yes" +; Example: nongzip_header[Is-Compressed] = "Yes" ; HTTP Strict Transport Security (HSTS) ; HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web From 443c8fe4a6d969c3edf6061ea44b2aa44b750599 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 2 Dec 2018 13:32:03 -0500 Subject: [PATCH 45/94] Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..b0a3531 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,33 @@ +--- +name: Bug report +about: Create a report to help us improve + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + + +**Versions** +What version of Nginx are you using ? +What version of Rocket-Nginx are you using ? +What version of WP Rocket are you using ? +Are you using Nginx as a reverse proxy (with Apache for instance) ? + +**To Reproduce** +Steps to reproduce the behaviour: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +Did you activate the debug in Rocket-Nginx ? Please do and include any headers. + +Make sure you include ALL Nginx configuration files you are using. Remove any sensitive information before submitting. + +**Expected behavior** +A clear and concise description of what you expected to happen. + + +**Additional context** +Add any other context about the problem here. From 2022fa129c2da43f925d2ac69fe192d1c036faf0 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 19 Apr 2019 21:30:46 -0400 Subject: [PATCH 46/94] Rocket-Nginx is now owned by SatelliteWP --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 781bd62..ef9c41b 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ Rocket-Nginx Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](https://wordpress.org) cache plugin [WP-Rocket](https://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. -This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com). +This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). -[![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2017/03/logo-dark.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en) +[![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2019/03/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) ## Table of Contents @@ -19,7 +19,7 @@ This project is sponsored by SatelliteWP, a [WordPress maintenance service](http ## Contributors -The configuration was created and is maintained by [Maxime Jobin](https://www.maximejobin.com) ([@maximejobin](https://twitter.com/maximejobin)). +The configuration was created by [https://www.satellitewp.com/en?utm_source=rocket-nginx Jobin](https://www.maximejobin.com) ([@maximejobin](github.com/maximejobin)) and is now maintained by [SatelliteWP](https://www.satellitewp.com/en?utm_source=rocket-nginx). ## Before You Start As the configuration's goal is to serve cached files directly without having to execute any PHP from WordPress, this may cause your scheduled jobs to not be called. As you may already know, WP-Cron jobs are not real cron jobs and are executed only when you have visits on your site. @@ -45,7 +45,7 @@ Make sure you test that your tasks still run after this change! ## Installation -In order to use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/maximejobin/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. +In order to use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/satellitewp/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP-Rocket. That said, you can generate as many configuration files as needed. @@ -54,7 +54,7 @@ You can create a folder `rocket-nginx` directory in your Nginx configuration dir To install, you can: ``` cd /etc/nginx - git clone https://github.com/maximejobin/rocket-nginx.git + git clone https://github.com/satellitewp/rocket-nginx.git ``` Since version 2.0, the configuration must be generated. To generate the default configuration, you must rename the disabled ini file and run the configuration parser: From 25a3b6f3bd3504d65ac3920cd96612d74043f87f Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 20 Apr 2019 21:45:48 -0400 Subject: [PATCH 47/94] Spelling fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef9c41b..db398d8 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Rocket-Nginx Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](https://wordpress.org) cache plugin [WP-Rocket](https://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. -This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Their service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). +This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Our service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). [![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2019/03/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) @@ -19,7 +19,7 @@ This project is sponsored by SatelliteWP, a [WordPress maintenance service](http ## Contributors -The configuration was created by [https://www.satellitewp.com/en?utm_source=rocket-nginx Jobin](https://www.maximejobin.com) ([@maximejobin](github.com/maximejobin)) and is now maintained by [SatelliteWP](https://www.satellitewp.com/en?utm_source=rocket-nginx). +The configuration was created by [Maxime Jobin](https://www.maximejobin.com) ([@maximejobin](https://github.com/maximejobin)) and is now maintained by [SatelliteWP](https://www.satellitewp.com/en?utm_source=rocket-nginx). ## Before You Start As the configuration's goal is to serve cached files directly without having to execute any PHP from WordPress, this may cause your scheduled jobs to not be called. As you may already know, WP-Cron jobs are not real cron jobs and are executed only when you have visits on your site. From e3fb1b960f720b1130d36e6ccc9d2f22edb92a38 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 15 Jun 2020 10:50:50 -0400 Subject: [PATCH 48/94] Update "testing" versions --- rocket-nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 121b9ec..4b595c8 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 3.0.5.1 -# Tested with NGINX: 1.15 (mainline) +# Tested with WP-Rocket version: 3.6.0.1 +# Tested with NGINX: 1.19 (mainline) # # Version 2.1.1 # From 691827db5cd946686a3e3c072b30b518822174f3 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 13:15:52 -0400 Subject: [PATCH 49/94] Change licence date --- license.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/license.md b/license.md index 7b16b33..88fadad 100644 --- a/license.md +++ b/license.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016 Maxime Jobin +Copyright (c) 2016-2020 Maxime Jobin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 863357e6c2a3f2d6791ae9f6df8cf9a7f6c3d1f2 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 13:21:38 -0400 Subject: [PATCH 50/94] Version bump --- rocket-nginx.ini.disabled | 7 +++++-- rocket-nginx.tmpl | 6 +++--- rocket-parser.php | 9 ++++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 0fe42ce..3de741b 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -1,7 +1,10 @@ ; Rocket-Nginx configuration file -; Author: Maxime Jobin +; Maintainer: SatelliteWP ; URL: https://github.com/maximejobin/rocket-nginx -; Version: 2.1.1 +; +; Original author: Maxime Jobin +; URL: https://www.maximejobin.com +; Version: 3.0.0 ; Default configuration [default] diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 4b595c8..4f8535b 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -5,12 +5,12 @@ # website with the cache plugin WP-Rocket (http://wp-rocket.me) # # Author: Maxime Jobin -# URL: https://github.com/maximejobin/rocket-nginx +# URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.6.0.1 +# Tested with WP-Rocket version: 3.6.0.3 # Tested with NGINX: 1.19 (mainline) # -# Version 2.1.1 +# Version 3.0.0 # ################################################################################################### diff --git a/rocket-parser.php b/rocket-parser.php index c640c42..6802db1 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -5,10 +5,13 @@ * Rocket-Parser is a configuration generator for Rocket-Nginx to speedup your * website with the cache plugin WP-Rocket (http://wp-rocket.me) * -* Author: Maxime Jobin -* URL: https://github.com/maximejobin/rocket-nginx +* Maintainer: SatelliteWP +* URL: https://github.com/satellitewp/rocket-nginx * -* Version 2.1.1 +* Original author: Maxime Jobin +* URL: https://www.maximejobin.com +* +* Version 3.0.0 * **************************************************************************************************/ From 79048ecc539435df38557fad44a98b8d95506c39 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 14:46:02 -0400 Subject: [PATCH 51/94] Create configuration folders --- .gitignore | 1 + rocket-parser.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0456e00..d5f3253 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ; Ignore all ini files *.ini *.conf +conf.d/ diff --git a/rocket-parser.php b/rocket-parser.php index 6802db1..635f8af 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -148,8 +148,20 @@ protected function generateConfigurationFiles($config) { $output = str_replace('#!# EXTENSION_MEDIAS #!#', $media_extensions, $output); + // Create main configuration folder if it doesn't exist + $main_confd = 'conf.d'; + if (!is_dir($main_confd)) { + mkdir( $main_confd, 0770 ); + } + + // Create configuration folder + $confd = $main_confd . '/' . $name; + if (!is_dir($confd)) { + mkdir( $confd, 0770 ); + } + // Output the file - $filename = $name . ".conf"; + $filename = $confd . '/' . $name . ".conf"; if (!$handle = fopen($filename, 'w')) { echo "Cannot open file: {$filename}.\n"; From cb594a77051160d6143996ee2dcf64aa4f1cd670 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 15:31:09 -0400 Subject: [PATCH 52/94] Convert adding headers to including files --- rocket-nginx.tmpl | 19 ++++++++------ rocket-parser.php | 63 +++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 4f8535b..3af08ec 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -129,8 +129,8 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - #!# HEADER_HTTP #!# - #!# HEADER_NON_GZIP #!# + #!# INCLUDE_GLOBAL #!# + #!# INCLUDE_HTTP #!# } # Do not gzip cached files that are already gzipped @@ -146,8 +146,8 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { add_header X-Rocket-Nginx-Reason $rocket_reason; add_header X-Rocket-Nginx-File $rocket_file; add_header Strict-Transport-Security "$rocket_hsts_value"; - #!# HEADER_HTTP #!# - #!# HEADER_GZIP #!# + #!# INCLUDE_GLOBAL #!# + #!# INCLUDE_HTTP #!# } # Debug header (when file is not cached) @@ -165,7 +165,8 @@ location ~* \.css$ { etag on; gzip_vary on; expires 30d; - #!# HEADER_CSS #!# + #!# INCLUDE_GLOBAL #!# + #!# INCLUDE_CSS #!# } @@ -176,15 +177,17 @@ location ~* \.js$ { etag on; gzip_vary on; expires 30d; - #!# HEADER_JS #!# + #!# INCLUDE_GLOBAL #!# + #!# INCLUDE_JS #!# } ################################################################################################### # BROWSER MEDIA CACHE # -location ~* \.(#!# EXTENSION_MEDIAS #!#)$ { +location ~* \.(#!# MEDIA_EXTENSIONS #!#)$ { etag on; expires 30d; - #!# HEADER_MEDIAS #!# + #!# INCLUDE_GLOBAL #!# + #!# INCLUDE_MEDIA #!# } diff --git a/rocket-parser.php b/rocket-parser.php index 635f8af..d056a6e 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -98,54 +98,32 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); - // HTTP headers - $header_http = ''; - if (isset($section['http_header']) && is_array($section['http_header'])) { - $header_http = $this->getGeneratedHeaders($section['http_header']); - } - $output = str_replace('#!# HEADER_HTTP #!#', $header_http, $output); - - // GZIP headers - $gzip_header = ''; - if (isset($section['gzip_header']) && is_array($section['gzip_header'])) { - $gzip_header = $this->getGeneratedHeaders($section['gzip_header']); - } - $output = str_replace('#!# HEADER_GZIP #!#', $gzip_header, $output); + // Global include + $include_global = $this->getGeneratedInclude($name, 'global'); + $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); - // Non-GZIP headers - $nongzip_header = ''; - if (isset($section['nongzip_header']) && is_array($section['nongzip_header'])) { - $nongzip_header = $this->getGeneratedHeaders($section['nongzip_header']); - } - $output = str_replace('#!# HEADER_NON_GZIP #!#', $nongzip_header, $output); + // HTTP include + $include_http = $this->getGeneratedInclude($name, 'http'); + $output = str_replace('#!# INCLUDE_HTTP #!#', $include_http, $output); // CSS headers - $css_header = ''; - if (isset($section['css_header']) && is_array($section['css_header'])) { - $css_header = $this->getGeneratedHeaders($section['css_header']); - } - $output = str_replace('#!# HEADER_CSS #!#', $css_header, $output); + $include_css = $this->getGeneratedInclude($name, 'css'); + $output = str_replace('#!# INCLUDE_CSS #!#', $include_css, $output); // JS headers - $js_header = ''; - if (isset($section['js_header']) && is_array($section['js_header'])) { - $js_header = $this->getGeneratedHeaders($section['js_header']); - } - $output = str_replace('#!# HEADER_JS #!#', $js_header, $output); + $include_js = $this->getGeneratedInclude($name, 'js'); + $output = str_replace('#!# INCLUDE_JS #!#', $include_js, $output); // Media headers - $medias_header = ''; - if (isset($section['medias_header']) && is_array($section['medias_header'])) { - $medias_header = $this->getGeneratedHeaders($section['medias_header']); - } - $output = str_replace('#!# HEADER_MEDIAS #!#', $medias_header, $output); + $include_media = $this->getGeneratedInclude($name, 'media'); + $output = str_replace('#!# INCLUDE_MEDIA #!#', $include_media, $output); // Media extensions $media_extensions = ''; if (isset($section['media_extensions']) && !empty($section['media_extensions'])) { $media_extensions = $section['media_extensions']; } - $output = str_replace('#!# EXTENSION_MEDIAS #!#', $media_extensions, $output); + $output = str_replace('#!# MEDIA_EXTENSIONS #!#', $media_extensions, $output); // Create main configuration folder if it doesn't exist @@ -183,6 +161,7 @@ protected function generateConfigurationFiles($config) { * * @return string Nginx headers */ + /* protected function getGeneratedHeaders($headers) { $result = ''; @@ -200,6 +179,20 @@ protected function getGeneratedHeaders($headers) { } } + return $result; + }*/ + + /** + * Returns generated include for a section headers + * + * @param $config string Configuration name + * @param $section string Section name + * + * @return string Include statement + */ + protected function getGeneratedInclude($config, $section) { + $result = "include conf.d/{$config}/{$section}.*.conf;"; + return $result; } From 34c2ba4aa1560478170d710e2b6cd127fd942112 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 15:32:40 -0400 Subject: [PATCH 53/94] Removing HSTS support --- rocket-nginx.tmpl | 13 ------------- rocket-parser.php | 7 ------- 2 files changed, 20 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 3af08ec..972fe7e 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -27,10 +27,6 @@ set $rocket_file ""; # Filename to use set $rocket_is_bypassed "No"; # 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_hsts 0; # Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS - -# HSTS value -set $rocket_hsts_value "#!# HEADER_HSTS #!#"; ################################################################################################### # PAGE CACHE @@ -52,11 +48,6 @@ if ($https = "on") { set $rocket_hsts 1; } -# If HSTS is disabled, unset HSTS set for Rocket-Nginx configuration -if ($rocket_hsts = "0") { - set $rocket_hsts_value ""; -} - # File/URL to return IF we must bypass WordPress # Desktop: index.html or index-https.html # Mobile: index-mobile.html or index-mobile-https.html @@ -128,7 +119,6 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { 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; - add_header Strict-Transport-Security "$rocket_hsts_value"; #!# INCLUDE_GLOBAL #!# #!# INCLUDE_HTTP #!# } @@ -145,7 +135,6 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { 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; - add_header Strict-Transport-Security "$rocket_hsts_value"; #!# INCLUDE_GLOBAL #!# #!# INCLUDE_HTTP #!# } @@ -155,8 +144,6 @@ 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; -# No HSTS header added here. We suppose it's correctly added in the site configuration - ################################################################################################### # BROWSER CSS CACHE diff --git a/rocket-parser.php b/rocket-parser.php index d056a6e..9fcd515 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -84,13 +84,6 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# HTML_CACHE_CONTROL #!#', $html_cache_control, $output); - // HSTS - $header_hsts = ''; - if (isset($section['header_hsts']) && !empty($section['header_hsts'])) { - $header_hsts = $section['header_hsts']; - } - $output = str_replace('#!# HEADER_HSTS #!#', $header_hsts, $output); - // Cookies $cookies = ''; if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { From 48ef12c2a3fe6042696be2df989c2f563c91255c Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 16:18:59 -0400 Subject: [PATCH 54/94] Adjust default configuration in the ini file --- rocket-nginx.ini.disabled | 60 +++------------------------------------ 1 file changed, 4 insertions(+), 56 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 3de741b..8aa9b57 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -37,71 +37,19 @@ cookie_invalidate[] = "wptouch_switch_toogle" cookie_invalidate[] = "comment_author_" cookie_invalidate[] = "comment_author_email_" -; HTTP(S) headers -; Custom headers can be added to the HTTP(S) requests for cached files (pages, posts, custom post types, -; etc). Brackets must be added at the end of the http_header variable name. That way, multiple headers -; can be added. -; Format: name "value" -; Example: http_header[My-Domain] = "mydomain.com" - -; HTTP(S) gzipped headers -; Custom headers can be added to the requests that serve already gzipped cached files (pages, posts, custom -; post types,etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple -; headers can be added. -; Format: name "value" -; Example: gzip_header[Is-Already-Gzipped] = "Yes" - -; HTTP(S) non gzipped headers -; Custom headers can be added to the requests that serve cached files (pages, posts, custom post types, -; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers -; can be added. -; Format: name "value" -; Example: nongzip_header[Is-Compressed] = "Yes" - -; HTTP Strict Transport Security (HSTS) -; HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web -; application through the use of a special response header. Once a supported browser receives this -; header that browser will prevent any communications from being sent over HTTP to the specified -; domain and will instead send all communications over HTTPS. It also prevents HTTPS click through -; prompts on browsers. -; Source: https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet - -; HSTS header value -; Default value: max-age=31536000; includeSubDomains -header_hsts = "max-age=31536000; includeSubDomains" - -; CSS headers -; Custom headers can be added to the CSS requests. -; Brackets must be added at the end of the css_header variable name. That way, multiple headers -; can be added. -; Format: name "value" -; Example: css_header[Is-CSS] = "Yes" - -; JavaScript headers -; Custom headers can be added to the javascript requests. -; Brackets must be added at the end of the js_header variable name. That way, multiple headers -; can be added. -; Example: js_header[Is-JavaScript] = "Yes" - ; Medias extensions ; Medias extensions for images, icons, fonts and other cachable medias. All extensions are separated ; by pipes (|). -; Default value: ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg -media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg" - -; Medias headers -; Custom headers can be added to the medias requests for -; etc). Brackets must be added at the end of the gzip_header variable name. That way, multiple headers -; can be added. -; Example: media_header[Is-Media] = "Yes" +; Default value: ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp +media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp" ; Multiple sections can be created for multiple websites with different configurations. ; It is recommended to use the default values as a base for custom validations. A new configuration ; file will be created for each new section created. -; In the following example, the 'default' configuration is used and added a custom media_header. +; In the following example, the 'default' configuration is used and added a custom cookie invalidation. ;[example.com : default] -;media_header[Is-NewSection] = "Yes" +;cookie_invalidate[] = "my_custome_cookie" From 13d90da9f4fe279ef461c4041add12f253ebe9f5 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 16:30:57 -0400 Subject: [PATCH 55/94] Handle query strings to ignore --- rocket-nginx.ini.disabled | 22 +++++++++++++++++- rocket-nginx.tmpl | 6 +++++ rocket-parser.php | 48 +++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 8aa9b57..19f40c8 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -37,6 +37,26 @@ cookie_invalidate[] = "wptouch_switch_toogle" cookie_invalidate[] = "comment_author_" cookie_invalidate[] = "comment_author_email_" +; Query strings to ignore +; Query strings invalidate the caching process made by Rocket-Nginx. But some strings are added, for instance, +; for JavaScript and do not influence the output provided by the server. List all query strings that won't +; invalidate the use of a cached page. +query_string_ignore[] = "utm_source" +query_string_ignore[] = "utm_content" +query_string_ignore[] = "utm_term" +query_string_ignore[] = "utm_campaign" +query_string_ignore[] = "utm_medium" +query_string_ignore[] = "gclid" +query_string_ignore[] = "utm_expid" +query_string_ignore[] = "fb_action_ids" +query_string_ignore[] = "fb_action_types" +query_string_ignore[] = "fb_source" +query_string_ignore[] = "permalink_name" +query_string_ignore[] = "lp-variation-id" +query_string_ignore[] = "lang" +query_string_ignore[] = "age-verified" +query_string_ignore[] = "ao_noptimize" + ; Medias extensions ; Medias extensions for images, icons, fonts and other cachable medias. All extensions are separated ; by pipes (|). @@ -50,6 +70,6 @@ media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp" ; In the following example, the 'default' configuration is used and added a custom cookie invalidation. ;[example.com : default] -;cookie_invalidate[] = "my_custome_cookie" +;cookie_invalidate[] = "my_custom_cookie" diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 972fe7e..e5383d4 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -63,6 +63,12 @@ if ($request_method = POST) { set $rocket_reason "POST request"; } +# Define Rocket-Nginx $is_args +set $rocket_is_args $is_args; + +# Query strings to ignore +#!# QUERY_STRING_IGNORE #!# + # Do not bypass if arguments are found (e.g. ?page=2) if ($is_args) { set $rocket_bypass 0; diff --git a/rocket-parser.php b/rocket-parser.php index 9fcd515..24fad4e 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -91,6 +91,13 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); + // Query strings to ignore + $query_strings_ignore = ''; + if (isset($section['query_string_ignore']) && is_array($section['query_string_ignore'])) { + $query_strings_ignore = $this->getGeneratedQueryStringsToIgnore($section['query_string_ignore']); + } + $output = str_replace('#!# QUERY_STRING_IGNORE #!#', $query_strings_ignore, $output); + // Global include $include_global = $this->getGeneratedInclude($name, 'global'); $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); @@ -148,32 +155,45 @@ protected function generateConfigurationFiles($config) { } } - /** - * Returns generated Nginx headers - * @param $headers array Headers - * - * @return string Nginx headers +/** + * Returns generated query strings statement to ignore + * @param $queryStrings array Query strings to ignore + * + * @return string Nginx "if" statements */ - /* - protected function getGeneratedHeaders($headers) { + protected function getGeneratedQueryStringsToIgnore($queryStrings) { $result = ''; - if (isset($headers) && is_array($headers)) { + if (isset($queryStrings) && is_array($queryStrings)) { $iteration = 1; - foreach ($headers as $name => $value) { + $result .= 'set $rocket_args $args;' . "\n"; + foreach ($queryStrings as $name => $value) { - if ($iteration > 1) { - $result .= "\t"; - } - $result .= "add_header {$name} \"{$value}\";\n"; + $result .= 'if ($rocket_args ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_args $1$2; '; + $result .= "}\n"; $iteration++; } + + $result .= "\n"; + $result .= '# Remove & at the beginning (if needed)' . "\n"; + $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n\n"; + $result .= 'set $rocket_args $is_args$rocket_args;' . "\n"; + $result .= "\n"; + $result .= '# Do not count arguments if part of caching arguments' . "\n"; + $result .= 'if ($rocket_args ~ ^\?$) {' . "\n"; + $result .= "\t" . 'set $rocket_args "";' . "\n"; + $result .= "\t" . 'set $rocket_is_args "";' . "\n"; + $result .= '}' . "\n"; + } + else { + $result = "# None.\n"; } return $result; - }*/ + } /** * Returns generated include for a section headers From 92ce516795d0d3ef50caa8c84187c43fb6d332e0 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 24 Jun 2020 16:52:34 -0400 Subject: [PATCH 56/94] Query strings to cache --- rocket-nginx.ini.disabled | 11 ++++++++-- rocket-nginx.tmpl | 4 ++++ rocket-parser.php | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 19f40c8..ad1c48b 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -41,6 +41,7 @@ cookie_invalidate[] = "comment_author_email_" ; Query strings invalidate the caching process made by Rocket-Nginx. But some strings are added, for instance, ; for JavaScript and do not influence the output provided by the server. List all query strings that won't ; invalidate the use of a cached page. +; Reference: https://docs.wp-rocket.me/article/971-caching-query-strings query_string_ignore[] = "utm_source" query_string_ignore[] = "utm_content" query_string_ignore[] = "utm_term" @@ -56,6 +57,14 @@ query_string_ignore[] = "lp-variation-id" query_string_ignore[] = "lang" query_string_ignore[] = "age-verified" query_string_ignore[] = "ao_noptimize" +query_string_ignore[] = "usqp" +query_string_ignore[] = "cn-reloaded" + +; Query strings to cache +; Query strings invalidate the caching process made by Rocket-Nginx. Some strings may need to be cached. +; List all query strings that needs to be cached as found in the WP Rocket configuration. +; Reference: https://docs.wp-rocket.me/article/971-caching-query-strings +;query_string_cache[] = "country" ; Medias extensions ; Medias extensions for images, icons, fonts and other cachable medias. All extensions are separated @@ -71,5 +80,3 @@ media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp" ; In the following example, the 'default' configuration is used and added a custom cookie invalidation. ;[example.com : default] ;cookie_invalidate[] = "my_custom_cookie" - - diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index e5383d4..77e1a3d 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -27,6 +27,7 @@ set $rocket_file ""; # Filename to use set $rocket_is_bypassed "No"; # 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 ################################################################################################### # PAGE CACHE @@ -69,6 +70,9 @@ set $rocket_is_args $is_args; # Query strings to ignore #!# QUERY_STRING_IGNORE #!# +# Query string to cache +#!# QUERY_STRING_CACHE #!# + # Do not bypass if arguments are found (e.g. ?page=2) if ($is_args) { set $rocket_bypass 0; diff --git a/rocket-parser.php b/rocket-parser.php index 24fad4e..3e5f10b 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -98,6 +98,13 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# QUERY_STRING_IGNORE #!#', $query_strings_ignore, $output); + // Query strings to cache + $query_strings_cache = ''; + if (isset($section['query_string_cache']) && is_array($section['query_string_cache'])) { + $query_strings_cache = $this->getGeneratedQueryStringsToCache($section['query_string_cache']); + } + $output = str_replace('#!# QUERY_STRING_CACHE #!#', $query_strings_cache, $output); + // Global include $include_global = $this->getGeneratedInclude($name, 'global'); $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); @@ -195,6 +202,42 @@ protected function getGeneratedQueryStringsToIgnore($queryStrings) { return $result; } + /** + * Returns generated query strings statement to cache + * + * @param $queryStrings array Query strings to cache + * + * @return string Nginx "if" statements + */ + protected function getGeneratedQueryStringsToCache($queryStrings) { + $result = ''; + + if (isset($queryStrings) && is_array($queryStrings)) { + $iteration = 1; + + $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; + foreach ($queryStrings as $name => $value) { + + $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_has_query_cache 1; '; + $result .= "}\n"; + + $iteration++; + } + + $result .= "\n"; + $result .= 'if ($rocket_has_query_cache = 1) {' . "\n"; + $result .= "\t" . 'set $rocket_args "";' . "\n"; + $result .= "\t" . 'set $rocket_is_args "";' . "\n"; + $result .= '}' . "\n"; + } + else { + $result = "# None.\n"; + } + + return $result; + } + /** * Returns generated include for a section headers * From 2d4640f31999697853efc84919ffc34a11c9cb0a Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Thu, 25 Jun 2020 07:33:39 -0400 Subject: [PATCH 57/94] WebP Management --- rocket-nginx.tmpl | 42 ++++++++++++++++++++++++++++++++---------- rocket-parser.php | 4 ++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 77e1a3d..59b2548 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -21,12 +21,14 @@ set $rocket_debug #!# DEBUG #!#; ################################################################################################### # 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 use -set $rocket_is_bypassed "No"; # 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_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 "No"; # 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_url_webp ""; # HTML URL containing WebP +set $rocket_file_webp ""; # HTML file on disk containing WebP set $rocket_has_query_cache 0; # Checks if a query string from URL is found from the cached query string ################################################################################################### @@ -46,15 +48,28 @@ if ($http_accept_encoding ~ br) { # Is SSL request ? if ($https = "on") { set $rocket_https_prefix "-https"; - set $rocket_hsts 1; } + # File/URL to return IF we must bypass WordPress # Desktop: index.html or index-https.html # Mobile: index-mobile.html or index-mobile-https.html -set $rocket_end "/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption"; -set $rocket_url "/#!# WP_CONTENT_URI #!#$rocket_end"; -set $rocket_file "$document_root/#!# WP_CONTENT_URI #!#$rocket_end"; +# WebP: index-https-webp.html or index-mobile-https.html +set $rocket_file "index$rocket_https_prefix.html$rocket_encryption"; +set $rocket_file_webp "index$rocket_https_prefix-webp.html$rocket_encryption"; + +set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/"; +set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/"; + +set $rocket_url "$rocket_pre_url$rocket_file"; +set $rocket_file "$rocket_pre_file$rocket_file"; + +# Request supports WebP ? +if ($http_accept ~* "webp") { + set $rocket_url_webp "$rocket_pre_url$rocket_file_webp"; + set $rocket_file_webp "$rocket_pre_file$rocket_file_webp"; +} + set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; @@ -98,6 +113,13 @@ if (-f "$rocket_mobile_detection") { set $rocket_reason "Specific mobile cache activated"; } +# If WebP is active and cache file was generated, use this +# instead of the default file +if (-f "$rocket_file_webp") { + set $rocket_file "$rocket_file_webp"; + set $rocket_url "$rocket_url_webp"; +} + # Do not bypass if the cached file does not exist if (!-f "$rocket_file") { set $rocket_bypass 0; diff --git a/rocket-parser.php b/rocket-parser.php index 3e5f10b..a91f38d 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -187,11 +187,11 @@ protected function getGeneratedQueryStringsToIgnore($queryStrings) { $result .= "\n"; $result .= '# Remove & at the beginning (if needed)' . "\n"; $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n\n"; - $result .= 'set $rocket_args $is_args$rocket_args;' . "\n"; + ##$result .= 'set $rocket_args $is_args$rocket_args;' . "\n"; $result .= "\n"; $result .= '# Do not count arguments if part of caching arguments' . "\n"; $result .= 'if ($rocket_args ~ ^\?$) {' . "\n"; - $result .= "\t" . 'set $rocket_args "";' . "\n"; + ##$result .= "\t" . 'set $rocket_args "";' . "\n"; $result .= "\t" . 'set $rocket_is_args "";' . "\n"; $result .= '}' . "\n"; } From b299ca2e442b2b0fca20a024a161325210fb370b Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 1 Jul 2020 14:48:09 -0400 Subject: [PATCH 58/94] Synchronize ignored query string with the WP Rocket --- rocket-nginx.ini.disabled | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index ad1c48b..f3313f6 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -43,18 +43,17 @@ cookie_invalidate[] = "comment_author_email_" ; invalidate the use of a cached page. ; Reference: https://docs.wp-rocket.me/article/971-caching-query-strings query_string_ignore[] = "utm_source" -query_string_ignore[] = "utm_content" -query_string_ignore[] = "utm_term" query_string_ignore[] = "utm_campaign" query_string_ignore[] = "utm_medium" -query_string_ignore[] = "gclid" query_string_ignore[] = "utm_expid" +query_string_ignore[] = "utm_term" +query_string_ignore[] = "utm_content" query_string_ignore[] = "fb_action_ids" query_string_ignore[] = "fb_action_types" query_string_ignore[] = "fb_source" -query_string_ignore[] = "permalink_name" -query_string_ignore[] = "lp-variation-id" -query_string_ignore[] = "lang" +query_string_ignore[] = "fbclid" +query_string_ignore[] = "_ga" +query_string_ignore[] = "gclid" query_string_ignore[] = "age-verified" query_string_ignore[] = "ao_noptimize" query_string_ignore[] = "usqp" From 525b7555ba85661fa4f970b8a60828ff063859c7 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 1 Jul 2020 14:48:33 -0400 Subject: [PATCH 59/94] Fix folder for configuration --- rocket-parser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-parser.php b/rocket-parser.php index a91f38d..852f91f 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -146,7 +146,7 @@ protected function generateConfigurationFiles($config) { } // Output the file - $filename = $confd . '/' . $name . ".conf"; + $filename = $main_confd . '/' . $name . ".conf"; if (!$handle = fopen($filename, 'w')) { echo "Cannot open file: {$filename}.\n"; @@ -162,7 +162,7 @@ protected function generateConfigurationFiles($config) { } } -/** + /** * Returns generated query strings statement to ignore * @param $queryStrings array Query strings to ignore * From 79fd2323b33c2df54961fa113509720846dd6196 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 10 Jul 2020 19:21:41 -0400 Subject: [PATCH 60/94] Add SatelliteWP as maintainer --- rocket-nginx.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 59b2548..78ea856 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -5,6 +5,7 @@ # 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.6.0.3 From 73a13c3064a7e32a6e226576b0917e94d0c853e1 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 10 Jul 2020 19:28:10 -0400 Subject: [PATCH 61/94] Make HTTPS support better --- rocket-nginx.tmpl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 78ea856..02b5e44 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -31,6 +31,7 @@ set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are usi set $rocket_url_webp ""; # HTML URL containing WebP set $rocket_file_webp ""; # HTML file on disk containing WebP 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 ################################################################################################### # PAGE CACHE @@ -46,8 +47,16 @@ if ($http_accept_encoding ~ br) { set $rocket_encryption ""; } -# Is SSL request ? -if ($https = "on") { +# 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"; } From 97375050fcc552e1816f3a0951998510c8dc2d19 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 29 Aug 2020 19:20:10 -0400 Subject: [PATCH 62/94] Adjust versions --- rocket-nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 02b5e44..5fb4dcc 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,8 +8,8 @@ # Maintainer: SatelliteWP.com # URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.6.0.3 -# Tested with NGINX: 1.19 (mainline) +# Tested with WP-Rocket version: 3.6.4 +# Tested with NGINX: 1.19.2 (mainline) # # Version 3.0.0 # From 6770ce1f2c2b055f53c12e85f2b2637ab255edf2 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 19 Sep 2020 14:47:41 -0400 Subject: [PATCH 63/94] Some refactoring --- rocket-nginx.tmpl | 58 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 5fb4dcc..e138cf8 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -31,7 +31,8 @@ set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are usi set $rocket_url_webp ""; # HTML URL containing WebP set $rocket_file_webp ""; # HTML file on disk containing WebP 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_is_https 0; # Checks if the request is HTTPS +set $rocket_support_webp 0; # Checks if the request supports WebP ################################################################################################### # PAGE CACHE @@ -60,27 +61,51 @@ 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_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # File/URL to return IF we must bypass WordPress -# Desktop: index.html or index-https.html -# Mobile: index-mobile.html or index-mobile-https.html -# WebP: index-https-webp.html or index-mobile-https.html -set $rocket_file "index$rocket_https_prefix.html$rocket_encryption"; -set $rocket_file_webp "index$rocket_https_prefix-webp.html$rocket_encryption"; +# Desktop: index.html +# Gzip: index.html_gzip +# HTTPS: index-https.html +# Mobile: index-mobile-https.html +# WebP: index-https-webp.html +set $rocket_test_file ""; -set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/"; -set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/"; +set $rocket_file "index$rocket_https_prefix"; +#set $rocket_file_webp "index$rocket_https_prefix-webp.html"; -set $rocket_url "$rocket_pre_url$rocket_file"; -set $rocket_file "$rocket_pre_file$rocket_file"; +set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$uri/$rocket_args/"; +set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$uri/$rocket_args/"; -# Request supports WebP ? -if ($http_accept ~* "webp") { - set $rocket_url_webp "$rocket_pre_url$rocket_file_webp"; - set $rocket_file_webp "$rocket_pre_file$rocket_file_webp"; +# Standard cache file format +set $rocket_url "$rocket_pre_url$rocket_file.html"; +set $rocket_file "$rocket_pre_file$rocket_file.html"; + +# Check if WebP cache file exists +if (-f "$rocket_pre_file$rocket_file-webp.html") { + $rocket_test_file "$rocket_file-webp.html"; + set $rocket_support_webp "$rocket_support_webp1"; } -set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; +if ($rocket_support_webp = "11") { + set $rocket_url "$rocket_pre_url$rocket_test_file"; + set $rocket_file "$rocket_pre_file$rocket_test_file"; +} + +#set $rocket_url "$rocket_pre_url$rocket_file"; +#set $rocket_file "$rocket_pre_file$rocket_file"; + +# 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 it's a POST request @@ -98,6 +123,9 @@ set $rocket_is_args $is_args; # Query string to cache #!# QUERY_STRING_CACHE #!# +# For dev purposes only (TO REMOVE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!) +add_header rocket-args $rocket_args; + # Do not bypass if arguments are found (e.g. ?page=2) if ($is_args) { set $rocket_bypass 0; From 9ee1e3a95f1a3188cfba691aa9fba1b19b77e0fb Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 4 Jan 2021 13:11:52 -0500 Subject: [PATCH 64/94] Update tested versions based on request #145 --- rocket-nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 4b595c8..5a6dfa7 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -7,8 +7,8 @@ # Author: Maxime Jobin # URL: https://github.com/maximejobin/rocket-nginx # -# Tested with WP-Rocket version: 3.6.0.1 -# Tested with NGINX: 1.19 (mainline) +# Tested with WP-Rocket version: 3.8.1 +# Tested with NGINX: 1.19.6 (mainline) # # Version 2.1.1 # From 288e1deb34ab3fe8b657ace96adac414bd921759 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 5 Dec 2021 14:08:09 -0500 Subject: [PATCH 65/94] Remove WebP support and final adjustments --- rocket-nginx.tmpl | 99 ++++---- rocket-parser.php | 570 ++++++++++++++++++++++++---------------------- 2 files changed, 335 insertions(+), 334 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index e138cf8..026049e 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,8 +8,8 @@ # Maintainer: SatelliteWP.com # URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.6.4 -# Tested with NGINX: 1.19.2 (mainline) +# Tested with WP-Rocket version: 3.10.4 +# Tested with NGINX: 1.21.4 (mainline) # # Version 3.0.0 # @@ -25,19 +25,28 @@ set $rocket_debug #!# DEBUG #!#; 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 "No"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Serving-Static +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_url_webp ""; # HTML URL containing WebP -set $rocket_file_webp ""; # HTML file on disk containing WebP 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_START #!# + +# 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"; @@ -69,37 +78,26 @@ if ($http_accept ~* "webp") { # Set mobile detection file path set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; +# Query strings to ignore +#!# QUERY_STRING_IGNORE #!# + +# Query string to cache +#!# QUERY_STRING_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 -# WebP: index-https-webp.html -set $rocket_test_file ""; -set $rocket_file "index$rocket_https_prefix"; -#set $rocket_file_webp "index$rocket_https_prefix-webp.html"; +set $rocket_file_start "index$rocket_https_prefix"; -set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$uri/$rocket_args/"; -set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$uri/$rocket_args/"; +set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/"; +set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/"; # Standard cache file format -set $rocket_url "$rocket_pre_url$rocket_file.html"; -set $rocket_file "$rocket_pre_file$rocket_file.html"; - -# Check if WebP cache file exists -if (-f "$rocket_pre_file$rocket_file-webp.html") { - $rocket_test_file "$rocket_file-webp.html"; - set $rocket_support_webp "$rocket_support_webp1"; -} - -if ($rocket_support_webp = "11") { - set $rocket_url "$rocket_pre_url$rocket_test_file"; - set $rocket_file "$rocket_pre_file$rocket_test_file"; -} - -#set $rocket_url "$rocket_pre_url$rocket_file"; -#set $rocket_file "$rocket_pre_file$rocket_file"; +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") { @@ -107,34 +105,31 @@ if (-f "$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"; } -# Define Rocket-Nginx $is_args -set $rocket_is_args $is_args; - -# Query strings to ignore -#!# QUERY_STRING_IGNORE #!# - -# Query string to cache -#!# QUERY_STRING_CACHE #!# - -# For dev purposes only (TO REMOVE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!) -add_header rocket-args $rocket_args; - # Do not bypass if arguments are found (e.g. ?page=2) -if ($is_args) { +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"; } @@ -143,30 +138,19 @@ if (-f "$document_root/.maintenance") { # wp-postpass_[hash] : When a protected post requires a password, this cookie is created. if ($http_cookie ~* "(#!# COOKIE_INVALIDATE #!#)") { 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 WebP is active and cache file was generated, use this -# instead of the default file -if (-f "$rocket_file_webp") { - set $rocket_file "$rocket_file_webp"; - set $rocket_url "$rocket_url_webp"; -} - -# Do not bypass if the cached file does not exist -if (!-f "$rocket_file") { - set $rocket_bypass 0; - set $rocket_reason "File not cached"; -} - # If the bypass token is still on, let's bypass WordPress with the cached URL if ($rocket_bypass = 1) { - set $rocket_is_bypassed "Yes"; + set $rocket_is_bypassed "HIT"; set $rocket_reason "$rocket_url"; } @@ -213,6 +197,7 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { 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_GLOBAL #!# ################################################################################################### @@ -221,7 +206,7 @@ add_header X-Rocket-Nginx-File $rocket_file; location ~* \.css$ { etag on; gzip_vary on; - expires 30d; + expires #!# CSS_EXPIRATION #!#; #!# INCLUDE_GLOBAL #!# #!# INCLUDE_CSS #!# } @@ -233,7 +218,7 @@ location ~* \.css$ { location ~* \.js$ { etag on; gzip_vary on; - expires 30d; + expires #!# JS_EXPIRATION #!#; #!# INCLUDE_GLOBAL #!# #!# INCLUDE_JS #!# } @@ -244,7 +229,7 @@ location ~* \.js$ { # location ~* \.(#!# MEDIA_EXTENSIONS #!#)$ { etag on; - expires 30d; + expires #!# MEDIA_EXPIRATION #!#; #!# INCLUDE_GLOBAL #!# #!# INCLUDE_MEDIA #!# } diff --git a/rocket-parser.php b/rocket-parser.php index 852f91f..8f2f062 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -16,287 +16,303 @@ **************************************************************************************************/ class RocketParser { - - public $configFile = 'rocket-nginx.ini'; - public $templateFile = 'rocket-nginx.tmpl'; - - /** - * Parse the ini configuration file - */ - protected function parseIniFile() { - $data = parse_ini_file($this->configFile, true); - $config = array(); - - foreach($data as $namespace => $properties) { - $parts = explode(':', $namespace); - $name = trim($parts[0]); - $extends = isset($parts[1]) ? trim($parts[1]) : null; - - // create namespace if necessary - if(!isset($config[$name])) { - $config[$name] = array(); - } - - // inherit base namespace - if(isset($data[$extends])) { - foreach($data[$extends] as $prop => $val) { - $config[$name][$prop] = $val; - } - } - // overwrite / set current namespace values - foreach($properties as $prop => $val) { - $config[$name][$prop] = $val; - } - } - - return $config; - } - - /** - * Generate all configuration files - */ - protected function generateConfigurationFiles($config) { - - // Load template - $template = $this->getTemplate(); - - foreach($config as $name => $section) { - $output = $template; - - // Debug - $debug = false; - if (isset($section['debug']) && $section['debug'] === '1') { - $debug = true; - } - $output = str_replace('#!# DEBUG #!#', $debug ? '1' : '0', $output); - - // WP Content URI - $wp_content_folder = 'wp-content'; - if (isset($section['wp_content_folder']) && !empty($section['wp_content_folder'])) { - $wp_content_folder = $section['wp_content_folder']; - } - $output = str_replace('#!# WP_CONTENT_URI #!#', $wp_content_folder, $output); - - // Cache Control - $html_cache_control = ''; - if (isset($section['html_cache_control']) && !empty($section['html_cache_control'])) { - $html_cache_control = $section['html_cache_control']; - } - $output = str_replace('#!# HTML_CACHE_CONTROL #!#', $html_cache_control, $output); - - // Cookies - $cookies = ''; - if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { - $cookies = implode('|', $section['cookie_invalidate']); - } - $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); - - // Query strings to ignore - $query_strings_ignore = ''; - if (isset($section['query_string_ignore']) && is_array($section['query_string_ignore'])) { - $query_strings_ignore = $this->getGeneratedQueryStringsToIgnore($section['query_string_ignore']); - } - $output = str_replace('#!# QUERY_STRING_IGNORE #!#', $query_strings_ignore, $output); - - // Query strings to cache - $query_strings_cache = ''; - if (isset($section['query_string_cache']) && is_array($section['query_string_cache'])) { - $query_strings_cache = $this->getGeneratedQueryStringsToCache($section['query_string_cache']); - } - $output = str_replace('#!# QUERY_STRING_CACHE #!#', $query_strings_cache, $output); - - // Global include - $include_global = $this->getGeneratedInclude($name, 'global'); - $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); - - // HTTP include - $include_http = $this->getGeneratedInclude($name, 'http'); - $output = str_replace('#!# INCLUDE_HTTP #!#', $include_http, $output); - - // CSS headers - $include_css = $this->getGeneratedInclude($name, 'css'); - $output = str_replace('#!# INCLUDE_CSS #!#', $include_css, $output); - - // JS headers - $include_js = $this->getGeneratedInclude($name, 'js'); - $output = str_replace('#!# INCLUDE_JS #!#', $include_js, $output); - - // Media headers - $include_media = $this->getGeneratedInclude($name, 'media'); - $output = str_replace('#!# INCLUDE_MEDIA #!#', $include_media, $output); - - // Media extensions - $media_extensions = ''; - if (isset($section['media_extensions']) && !empty($section['media_extensions'])) { - $media_extensions = $section['media_extensions']; - } - $output = str_replace('#!# MEDIA_EXTENSIONS #!#', $media_extensions, $output); - - - // Create main configuration folder if it doesn't exist - $main_confd = 'conf.d'; - if (!is_dir($main_confd)) { - mkdir( $main_confd, 0770 ); - } - - // Create configuration folder - $confd = $main_confd . '/' . $name; - if (!is_dir($confd)) { - mkdir( $confd, 0770 ); - } - - // Output the file - $filename = $main_confd . '/' . $name . ".conf"; - - if (!$handle = fopen($filename, 'w')) { - echo "Cannot open file: {$filename}.\n"; - continue; - } - - if (fwrite($handle, $output) === FALSE) { - echo "Cannot write to file {$filename}.\n"; - continue; - } - - fclose($handle); - } - } - - /** - * Returns generated query strings statement to ignore - * @param $queryStrings array Query strings to ignore - * - * @return string Nginx "if" statements - */ - protected function getGeneratedQueryStringsToIgnore($queryStrings) { - $result = ''; - - if (isset($queryStrings) && is_array($queryStrings)) { - $iteration = 1; - - $result .= 'set $rocket_args $args;' . "\n"; - foreach ($queryStrings as $name => $value) { - - $result .= 'if ($rocket_args ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; - $result .= 'set $rocket_args $1$2; '; - $result .= "}\n"; - - $iteration++; - } - - $result .= "\n"; - $result .= '# Remove & at the beginning (if needed)' . "\n"; - $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n\n"; - ##$result .= 'set $rocket_args $is_args$rocket_args;' . "\n"; - $result .= "\n"; - $result .= '# Do not count arguments if part of caching arguments' . "\n"; - $result .= 'if ($rocket_args ~ ^\?$) {' . "\n"; - ##$result .= "\t" . 'set $rocket_args "";' . "\n"; - $result .= "\t" . 'set $rocket_is_args "";' . "\n"; - $result .= '}' . "\n"; - } - else { - $result = "# None.\n"; - } - - return $result; - } - - /** - * Returns generated query strings statement to cache - * - * @param $queryStrings array Query strings to cache - * - * @return string Nginx "if" statements - */ - protected function getGeneratedQueryStringsToCache($queryStrings) { - $result = ''; - - if (isset($queryStrings) && is_array($queryStrings)) { - $iteration = 1; - - $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; - foreach ($queryStrings as $name => $value) { - - $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; - $result .= 'set $rocket_has_query_cache 1; '; - $result .= "}\n"; - - $iteration++; - } - - $result .= "\n"; - $result .= 'if ($rocket_has_query_cache = 1) {' . "\n"; - $result .= "\t" . 'set $rocket_args "";' . "\n"; - $result .= "\t" . 'set $rocket_is_args "";' . "\n"; - $result .= '}' . "\n"; - } - else { - $result = "# None.\n"; - } - - return $result; - } - - /** - * Returns generated include for a section headers - * - * @param $config string Configuration name - * @param $section string Section name - * - * @return string Include statement - */ - protected function getGeneratedInclude($config, $section) { - $result = "include conf.d/{$config}/{$section}.*.conf;"; - - return $result; - } - - /** - * Get the template file if it exists - */ - protected function getTemplate() { - - if (file_exists($this->templateFile) === false) { - die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . - "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); - } - - return file_get_contents('rocket-nginx.tmpl'); - } - - /** - * Check if configuration file exists - */ - protected function checkConfigurationFile() { - if (file_exists($this->configFile) === false) { - die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . - "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); - } - } - - /** - * Generate configuration files - */ - public function go() { - $this->checkConfigurationFile(); - - $data = $this->parseIniFile(); - $this->generateConfigurationFiles($data); - } + + public $configFile = 'rocket-nginx.ini'; + public $templateFile = 'rocket-nginx.tmpl'; + + /** + * Parse the ini configuration file + */ + protected function parseIniFile() { + $data = parse_ini_file($this->configFile, true); + $config = array(); + + foreach($data as $namespace => $properties) { + $parts = explode(':', $namespace); + $name = trim($parts[0]); + $extends = isset($parts[1]) ? trim($parts[1]) : null; + + // create namespace if necessary + if(!isset($config[$name])) { + $config[$name] = array(); + } + + // inherit base namespace + if(isset($data[$extends])) { + foreach($data[$extends] as $prop => $val) { + $config[$name][$prop] = $val; + } + } + // overwrite / set current namespace values + foreach($properties as $prop => $val) { + $config[$name][$prop] = $val; + } + } + + return $config; + } + + /** + * Generate all configuration files + */ + protected function generateConfigurationFiles($config) { + + // Load template + $template = $this->getTemplate(); + + foreach($config as $name => $section) { + $output = $template; + + // Debug + $debug = false; + if (isset($section['debug']) && $section['debug'] === '1') { + $debug = true; + } + $output = str_replace('#!# DEBUG #!#', $debug ? '1' : '0', $output); + + // WP Content URI + $wp_content_folder = 'wp-content'; + if (isset($section['wp_content_folder']) && !empty($section['wp_content_folder'])) { + $wp_content_folder = $section['wp_content_folder']; + } + $output = str_replace('#!# WP_CONTENT_URI #!#', $wp_content_folder, $output); + + // Cache Control + $html_cache_control = ''; + if (isset($section['html_cache_control']) && !empty($section['html_cache_control'])) { + $html_cache_control = $section['html_cache_control']; + } + $output = str_replace('#!# HTML_CACHE_CONTROL #!#', $html_cache_control, $output); + + // Cookies + $cookies = ''; + if (isset($section['cookie_invalidate']) && is_array($section['cookie_invalidate'])) { + $cookies = implode('|', $section['cookie_invalidate']); + } + $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); + + // Query strings to ignore + $query_strings_ignore = ''; + if (isset($section['query_string_ignore']) && is_array($section['query_string_ignore'])) { + $query_strings_ignore = $this->getGeneratedQueryStringsToIgnore($section['query_string_ignore']); + } + $output = str_replace('#!# QUERY_STRING_IGNORE #!#', $query_strings_ignore, $output); + + // Query strings to cache + $query_strings_cache = ''; + if (isset($section['query_string_cache']) && is_array($section['query_string_cache'])) { + $query_strings_cache = $this->getGeneratedQueryStringsToCache($section['query_string_cache']); + } + $output = str_replace('#!# QUERY_STRING_CACHE #!#', $query_strings_cache, $output); + + // CSS expiration delay + $css_expiration = '30d'; + if (isset($section['css_expiration']) && !empty($section['css_expiration'])) { + $css_expiration = $section['css_expiration']; + } + $output = str_replace('#!# CSS_EXPIRATION #!#', $css_expiration, $output); + + // JS expiration delay + $js_expiration = '30d'; + if (isset($section['js_expiration']) && !empty($section['js_expiration'])) { + $js_expiration = $section['js_expiration']; + } + $output = str_replace('#!# JS_EXPIRATION #!#', $js_expiration, $output); + + // Media expiration delay + $media_expiration = '30d'; + if (isset($section['media_expiration']) && !empty($section['media_expiration'])) { + $media_expiration = $section['media_expiration']; + } + $output = str_replace('#!# MEDIA_EXPIRATION #!#', $media_expiration, $output); + + // Global include + $include_global = $this->getGeneratedInclude($name, 'global'); + $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); + + // HTTP include + $include_http = $this->getGeneratedInclude($name, 'http'); + $output = str_replace('#!# INCLUDE_HTTP #!#', $include_http, $output); + + // CSS headers + $include_css = $this->getGeneratedInclude($name, 'css'); + $output = str_replace('#!# INCLUDE_CSS #!#', $include_css, $output); + + // JS headers + $include_js = $this->getGeneratedInclude($name, 'js'); + $output = str_replace('#!# INCLUDE_JS #!#', $include_js, $output); + + // Media headers + $include_media = $this->getGeneratedInclude($name, 'media'); + $output = str_replace('#!# INCLUDE_MEDIA #!#', $include_media, $output); + + // Media extensions + $media_extensions = ''; + if (isset($section['media_extensions']) && !empty($section['media_extensions'])) { + $media_extensions = $section['media_extensions']; + } + $output = str_replace('#!# MEDIA_EXTENSIONS #!#', $media_extensions, $output); + + + // Create main configuration folder if it doesn't exist + $main_confd = 'conf.d'; + if (!is_dir($main_confd)) { + mkdir( $main_confd, 0770 ); + } + + // Create configuration folder + $confd = $main_confd . '/' . $name; + if (!is_dir($confd)) { + mkdir( $confd, 0770 ); + } + + // Output the file + $filename = $main_confd . '/' . $name . ".conf"; + + if (!$handle = fopen($filename, 'w')) { + echo "Cannot open file: {$filename}.\n"; + continue; + } + + if (fwrite($handle, $output) === FALSE) { + echo "Cannot write to file {$filename}.\n"; + continue; + } + + fclose($handle); + } + } + + /** + * Returns generated query strings statement to ignore + * @param $queryStrings array Query strings to ignore + * + * @return string Nginx "if" statements + */ + protected function getGeneratedQueryStringsToIgnore($queryStrings) { + $result = ''; + + if (isset($queryStrings) && is_array($queryStrings)) { + $iteration = 1; + + $result .= 'set $rocket_args $args;' . "\n"; + foreach ($queryStrings as $name => $value) { + + $result .= 'if ($rocket_args ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_args $1$2; '; + $result .= "}\n"; + + $iteration++; + } + + $result .= "\n"; + $result .= '# Remove & at the beginning (if needed)' . "\n"; + $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n\n"; + $result .= "\n"; + $result .= '# Do not count arguments if part of caching arguments' . "\n"; + $result .= 'if ($rocket_args ~ ^\?$) {' . "\n"; + $result .= "\t" . 'set $rocket_is_args "";' . "\n"; + $result .= '}' . "\n"; + } + else { + $result = "# None.\n"; + } + + return $result; + } + + /** + * Returns generated query strings statement to cache + * + * @param $queryStrings array Query strings to cache + * + * @return string Nginx "if" statements + */ + protected function getGeneratedQueryStringsToCache($queryStrings) { + $result = ''; + + if (isset($queryStrings) && is_array($queryStrings)) { + $iteration = 1; + + $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; + foreach ($queryStrings as $name => $value) { + + $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_has_query_cache 1; '; + $result .= "}\n"; + + $iteration++; + } + + $result .= "\n"; + $result .= 'if ($rocket_has_query_cache = 1) {' . "\n"; + $result .= "\t" . 'set $rocket_args "#$rocket_args";' . "\n"; + $result .= "\t" . 'set $rocket_is_args "";' . "\n"; + $result .= '}' . "\n"; + } + else { + $result = "# None.\n"; + } + + return $result; + } + + /** + * Returns generated include for a section headers + * + * @param $config string Configuration name + * @param $section string Section name + * + * @return string Include statement + */ + protected function getGeneratedInclude($config, $section) { + $dir = dirname(__FILE__); + $result = "include {$dir}/conf.d/{$config}/{$section}.*.conf;"; + + return $result; + } + + /** + * Get the template file if it exists + */ + protected function getTemplate() { + + if (file_exists($this->templateFile) === false) { + die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . + "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); + } + + return file_get_contents('rocket-nginx.tmpl'); + } + + /** + * Check if configuration file exists + */ + protected function checkConfigurationFile() { + if (file_exists($this->configFile) === false) { + die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . + "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); + } + } + + /** + * Generate configuration files + */ + public function go() { + $this->checkConfigurationFile(); + + $data = $this->parseIniFile(); + $this->generateConfigurationFiles($data); + } } -// If file is included, we assume it will call the class automatically. -// Otherwise, let's generate the configuration files. +// If this file is included (using 'include' or 'require'), we assume it will call +// the class automatically. Otherwise, let's generate the configuration files. $includedFiles = count(get_included_files()); if ($includedFiles === 1) { - error_reporting(-1); + error_reporting(-1); - $rp = new RocketParser(); - $rp->go(); + $rp = new RocketParser(); + $rp->go(); } - - - - From 0543ca1329a1bfabb14a69c7d52650173bf3b032 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 5 Dec 2021 14:59:27 -0500 Subject: [PATCH 66/94] Addind info for version 3.0 --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index db398d8..489e899 100644 --- a/README.md +++ b/README.md @@ -27,19 +27,20 @@ As the configuration's goal is to serve cached files directly without having to In order to make sure your scheduled tasks run when they should, it is strongly suggested to disable WordPress cron jobs and create a real cron job. To disable WordPress cron job, add the following line to your `wp-config.php`: -`define('DISABLE_WP_CRON', true);` + + define('DISABLE_WP_CRON', true); Then, manually a cron job every 15 minutes (it should be enough for most websites): -`*/15 * * * * wget -q -O - http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null` + */15 * * * * wget -q -O - http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null or -`*/15 * * * * curl http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null` + */15 * * * * curl http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null or -`*/15 * * * * cd /home/user/public_html; php wp-cron.php &>/dev/null` + */15 * * * * cd /home/user/public_html; php wp-cron.php &>/dev/null Make sure you test that your tasks still run after this change! @@ -47,7 +48,7 @@ Make sure you test that your tasks still run after this change! In order to use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/satellitewp/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. -Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP-Rocket. That said, you can generate as many configuration files as needed. +Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP Rocket. You can generate as many configuration files as needed. You can create a folder `rocket-nginx` directory in your Nginx configuration directory. If you are using Ubuntu, your Nginx configuration (nginx.conf) should be found in: `/etc/nginx/`. @@ -65,7 +66,7 @@ php rocket-parser.php ``` This will generate the `default.conf` configuration that can be included for all websites. If you need to alter the default configuration, you can edit the ini file and add another section at the bottom of the file. -Then, in your configuration file, you must [include](https://nginx.org/en/docs/ngx_core_module.html#include) the configuration. If your websites configurations are in `/etc/nginx/sites-available`, you need to alter your configuration: +Then, in your Nginx configuration file, you must [include](https://nginx.org/en/docs/ngx_core_module.html#include) the generated configuration. If your websites configurations are in `/etc/nginx/sites-available`, you need to alter your configuration: ``` server { @@ -118,6 +119,33 @@ Finally, **each time** you generate (or regenerate) the configurations files, yo `service nginx reload` +Starting at version 3.0, a `conf.d`folder is created. For each different profile you create, a subfolder is created inside that folder. In it, you can create files that will be included within the generated configuration file. + +You can include configuration files at different times. + +### Before Rocket-Nginx starts + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `start.*.conf`. + +### Globally in every section + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `global.*.conf`. + +### In the HTTP section + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `http.*.conf`. + +### In the CSS section + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `css.*.conf`. + +### In the JS section + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `js.*.conf`. + +### In the Media section + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `media.*.conf`. ## Debug You may want to check if your files are served directly by Nginx and not calling any PHP. To do that, open the `rocket-nginx.ini` file and change the debug value from: @@ -128,12 +156,11 @@ To: `debug = true` -The following header is present no matter if debug is set to 0 or 1: - * **X-Rocket-Nginx-Serving-Static**: Did the configuration served the cached file directly (did it bypass WordPress): Yes or No. - +The following header is present no matter if debug is set to true or false: + * **X-Rocket-Nginx-Serving-Static**: Did the configuration served the cached file directly : HIT, MISS, BYPASS. This will add the following headers to your response request: - * **X-Rocket-Nginx-Reason**: If Bypass is set to "No", what is the reason for calling WordPress. If "Yes", what is the file used (URL). - * **X-Rocket-Nginx-File**: If "Yes", what is the file used (path on disk). + * **X-Rocket-Nginx-Reason**: If serving static is not set to "HIT", what is the reason for calling WordPress. If "HIT", what is the file used (URL). + * **X-Rocket-Nginx-File**: If "HIT", what is the file used (path on disk). Reasons for not serving a cached file: @@ -146,6 +173,20 @@ Reasons for not serving a cached file: ## FAQ +**How do I upgrade from version 1 or 2 to version 3?** + +We suggest that you save your previous configuration and start over. Take this opportunity to review everything as many things have changed. Officially, version 3.x is not backward-compatible with previous versions. Starging from scratch should not take more than 15 minutes. + +**What is new in version 3.x?** + +Many thing! + +- Query strings to cache are supported via the ini file. See the WP Rocket [Cache query strings](https://docs.wp-rocket.me/article/971-caching-query-strings?utm_source=rocket-nginx) documentation for configuration. +- Query string to ignore are supported. See the WP Rocket [Cache query strings to ignore](https://docs.wp-rocket.me/article/971-caching-query-strings?utm_source=rocket-nginx#ignored-parameters) documentation for configuration. +- Default HSTS value was removed. +- Custom configurations can be included in every sections +- Custom expiration are supported for CSS, JS and medias + **Do you have any benchmark about the project ?** No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP-Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → PHP → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP-Rocket... at least) before serving the static file. From c78f4949946d73885de6d1e191293af8a9019532 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 5 Dec 2021 15:06:10 -0500 Subject: [PATCH 67/94] Fixing the readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 489e899..7ce3e40 100644 --- a/README.md +++ b/README.md @@ -97,8 +97,8 @@ You can add a new section based on the default configuration like this: # This creates the new section and will generate a new configuration [example.com : default] -# This will add a new header named "Is-NewSection" with the value "Yes" to the medias section -media_header[Is-NewSection] = "Yes" +# This will add a value to invalidate the cache with a cookie +cookie_invalidate[] = "my_custom_cookie" ``` Once you edit the ini file, you must regenerate your Nginx configuration file by running the parser: @@ -179,7 +179,7 @@ We suggest that you save your previous configuration and start over. Take this o **What is new in version 3.x?** -Many thing! +Many things! - Query strings to cache are supported via the ini file. See the WP Rocket [Cache query strings](https://docs.wp-rocket.me/article/971-caching-query-strings?utm_source=rocket-nginx) documentation for configuration. - Query string to ignore are supported. See the WP Rocket [Cache query strings to ignore](https://docs.wp-rocket.me/article/971-caching-query-strings?utm_source=rocket-nginx#ignored-parameters) documentation for configuration. From f05bb17ddc277296661c4779256fcdb513d98c39 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sun, 5 Dec 2021 19:33:09 -0500 Subject: [PATCH 68/94] Documentation adjutments --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7ce3e40..625fd52 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ Reasons for not serving a cached file: We suggest that you save your previous configuration and start over. Take this opportunity to review everything as many things have changed. Officially, version 3.x is not backward-compatible with previous versions. Starging from scratch should not take more than 15 minutes. + **What is new in version 3.x?** Many things! @@ -187,13 +188,16 @@ Many things! - Custom configurations can be included in every sections - Custom expiration are supported for CSS, JS and medias + **Do you have any benchmark about the project ?** No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP-Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → PHP → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP-Rocket... at least) before serving the static file. + **Will Rocket-Nginx work if my website uses a SSL certificate (https) ?** Yes! Rocket-Nginx will detect if the request was made through HTTP or HTTPS and serve the right file depending on the request type. Both protocols are handled automagically since version 1.0. + ## License Released under the MIT License. See the license file for details. From e2e14eeaed814369c7d38cf7fe71e23133d059f8 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 6 Dec 2021 19:07:38 -0500 Subject: [PATCH 69/94] Add code spacing, more doc. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 625fd52..5f48260 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ In order to make sure your scheduled tasks run when they should, it is strongly To disable WordPress cron job, add the following line to your `wp-config.php`: - define('DISABLE_WP_CRON', true); + define( 'DISABLE_WP_CRON', true ); Then, manually a cron job every 15 minutes (it should be enough for most websites): @@ -187,6 +187,7 @@ Many things! - Default HSTS value was removed. - Custom configurations can be included in every sections - Custom expiration are supported for CSS, JS and medias +- Allowing adding headers from the config file was removed. **Do you have any benchmark about the project ?** From 3534857190e51becbe7132cba816f1aafbd89177 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:51:09 -0500 Subject: [PATCH 70/94] Add expiration delays --- rocket-nginx.ini.disabled | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index f3313f6..f403d75 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -71,6 +71,12 @@ query_string_ignore[] = "cn-reloaded" ; Default value: ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp media_extensions = "ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg|webp" +; Cache expiration delay +; Cache expiration delay for CSS, JS and media files +; Default value: 30d +css_expiration = "30d" +js_expiration = "30d" +media_expiration = "30d" ; Multiple sections can be created for multiple websites with different configurations. ; It is recommended to use the default values as a base for custom validations. A new configuration From 435ac27c0763c5e28a8436cbe51eb226ec870f16 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:51:49 -0500 Subject: [PATCH 71/94] Adjust variable if args are empty --- rocket-nginx.tmpl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 026049e..ce9fd80 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -81,6 +81,10 @@ set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-roc # Query strings to ignore #!# QUERY_STRING_IGNORE #!# +if ($rocket_args = "") { + set $rocket_is_args ""; +} + # Query string to cache #!# QUERY_STRING_CACHE #!# From 2aa3b53a034c4959a7ab36660443d9f5ef8acbe8 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:52:57 -0500 Subject: [PATCH 72/94] Replace include variable --- rocket-parser.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rocket-parser.php b/rocket-parser.php index 8f2f062..fbafeef 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -126,6 +126,10 @@ protected function generateConfigurationFiles($config) { } $output = str_replace('#!# MEDIA_EXPIRATION #!#', $media_expiration, $output); + // Start include + $include_start = $this->getGeneratedInclude($name, 'start'); + $output = str_replace('#!# INCLUDE_START #!#', $include_start, $output); + // Global include $include_global = $this->getGeneratedInclude($name, 'global'); $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); From b8dea3c8973b5f5f1dd68ab35c61bd0be41a93e1 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:53:10 -0500 Subject: [PATCH 73/94] Add test file --- tests/go.php | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 tests/go.php diff --git a/tests/go.php b/tests/go.php new file mode 100644 index 0000000..1642e24 --- /dev/null +++ b/tests/go.php @@ -0,0 +1,277 @@ + $headers, 'body' => $body]; +} +echo "****************************************************************************\n"; +echo "* Rocket-Nginx automated testing\n"; +echo "****************************************************************************\n"; +echo "Let's go!\n"; + +// Home - Should be a HIT +$response = call_url(http://github.com/$base_url); + +if (!isset($response['headers']['x-rocket-nginx-reason'])) { + die('Please set debug to true before running the tests...'); +} +$homepage_cached = false; +echo "Getting cached homepage..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'HIT') { + echo "OK\n"; + $homepage_cached = true; +} +else { + echo "FAILED (Cache was not primed)\n"; +} + +// Retesting as cache might not be primed yet +if (!$homepage_cached) { + $response = call_url(http://github.com/$base_url); + + echo "Getting cached homepage again..."; + if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'HIT') { + echo "OK\n"; + $homepage_cached = true; + } + else { + echo "FAILED\n"; + exit; + } +} + +// Home with a valid argument - Should be a HIT +$response = call_url(http://github.com/$base_url%20.%20'?country=canada'); + +$homepage_cached = false; +echo "Getting cached homepage with a valid argument..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'HIT') { + echo "OK\n"; + $homepage_cached = true; +} +else { + echo "FAILED (Cache was not primed)\n"; +} + +// Retesting as cache might not be primed yet +if (!$homepage_cached) { + $response = call_url(http://github.com/$base_url%20.%20'?country=canada'); + + echo "Getting cached homepage with a valid argument again..."; + if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'HIT') { + echo "OK\n"; + $homepage_cached = true; + } + else { + echo "FAILED\n"; + exit; + } +} + +// Never cached - Should be a MISS +$response = call_url(http://github.com/$base_url%20.%20'never-cached/'); + +if (!isset($response['headers']['x-rocket-nginx-reason'])) { + die('Please set debug to true before running the tests...'); +} +$homepage_cached = false; +echo "Getting never cached page..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'MISS') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + $homepage_cached = true; +} + +// Retesting as cache might not be primed yet +if ($homepage_cached) { + $response = call_url(http://github.com/$base_url%20.%20'never-cached/'); + + echo "Getting never cached page again..."; + if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'MISS') { + echo "OK\n"; + $homepage_cached = true; + } + else { + echo "FAILED\n"; + exit; + } +} + +// Home - With an argument +$response = call_url(http://github.com/$base_url,%20'rocket=nginx'); +echo "Getting cached homepage with an argument..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'BYPASS') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + exit; +} + +// Home - With an allowed ignored argument +$response = call_url(http://github.com/$base_url,%20'utm_source=nginx'); +echo "Getting cached homepage with an allowed argument..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'HIT') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + exit; +} + +// Home - With both ignored allowed and unallowed arguments +$response = call_url(http://github.com/$base_url,%20'rocket=nginx&utm_source=rocket'); +echo "Getting cached homepage with an argument..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'BYPASS') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + exit; +} + +// Home - With an unallowed cookie +$response = call_url(http://github.com/$base_url,%20null,%20null,%20'woocommerce_items_in_cart=yes'); +echo "Getting cached homepage with an unallowed cookie..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'BYPASS') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + exit; +} + +// Home - With post data +$response = call_url(http://github.com/$base_url,%20null,%20['post'%20=%3E%20'data']); +echo "Getting cached homepage with POST data..."; +if ($response['headers']['x-rocket-nginx-serving-static'][0] == 'BYPASS') { + echo "OK\n"; +} +else { + echo "FAILED\n"; + exit; +} + +// CSS - Checking expiration +$response = call_url(http://github.com/$base_url%20.%20'wp-includes/css/dashicons.min.css'); +echo "Getting CSS file with expiration..."; +if (isset($response['headers']['expires'])) { + $now = new DateTime(); + $expiration = new DateTime($response['headers']['expires'][0]); + + $interval = $expiration->diff($now); + $days = $interval->format('%a'); + + if ($days == '370') { + echo "OK\n"; + } + else { + echo "FAILED\n"; + exit; + } +} +else { + echo "FAILED\n"; + exit; +} + +// JS - Checking expiration +$response = call_url(http://github.com/$base_url%20.%20'wp-includes/js/wp-embed.min.js'); +echo "Getting JS file with expiration..."; +if (isset($response['headers']['expires'])) { + $now = new DateTime(); + $expiration = new DateTime($response['headers']['expires'][0]); + + $interval = $expiration->diff($now); + $days = $interval->format('%a'); + + if ($days == '740') { + echo "OK\n"; + } + else { + echo "FAILED\n"; + exit; + } +} +else { + echo "FAILED\n"; + exit; +} + +// Media - Checking expiration +$response = call_url(http://github.com/$base_url%20.%20'wp-admin/images/wordpress-logo.svg'); +echo "Getting media file with expiration..."; +if (isset($response['headers']['expires'])) { + $now = new DateTime(); + $expiration = new DateTime($response['headers']['expires'][0]); + + $interval = $expiration->diff($now); + $days = $interval->format('%a'); + + if ($days == '1110') { + echo "OK\n"; + } + else { + echo "FAILED\n"; + exit; + } +} +else { + echo "FAILED\n"; + exit; +} + +echo "All tests successful!\n"; +#var_dump($response['headers']); +#function call_url(http://github.com/$baseUrl,%20$queryString%20=%20null,%20$post_data%20=%20null%20,%20$cookies%20=%20null) From 105da4b47206e04934e5e8bf5201b08e9b75aebc Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:55:01 -0500 Subject: [PATCH 74/94] Edit include path example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f48260..d3f2527 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ server { ... # Rocket-Nginx configuration - include rocket-nginx/default.conf; + include rocket-nginx/cond.d/default.conf; ... } From eaa97fb550216cfd4435e743e3acf9ea3e5cb42d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 11 Dec 2021 19:59:22 -0500 Subject: [PATCH 75/94] Edit the latest WP Rocket version tested --- rocket-nginx.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index ce9fd80..7edece9 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,7 +8,7 @@ # Maintainer: SatelliteWP.com # URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.10.4 +# Tested with WP-Rocket version: 3.10.5 # Tested with NGINX: 1.21.4 (mainline) # # Version 3.0.0 From 1d55b33a70a63d1adbf9a4fd8397e75993f595a4 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Tue, 14 Dec 2021 10:18:18 -0500 Subject: [PATCH 76/94] Fix a typo in code... oops! --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3f2527..5fe26fd 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ server { ... # Rocket-Nginx configuration - include rocket-nginx/cond.d/default.conf; + include rocket-nginx/conf.d/default.conf; ... } From 593003567b371ff4daf6c73b7a846813d96974d9 Mon Sep 17 00:00:00 2001 From: Jonathan Allard <86688525+j-Allard-22@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:53:40 -0500 Subject: [PATCH 77/94] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5fe26fd..3a49c85 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](h This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Our service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). -[![SatelliteWP - WordPress Maintenance](https://cdn.satellitewp.com/wp-content/uploads/2019/03/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) +[![SatelliteWP - WordPress Maintenance](https://www.satellitewp.com/wp-content/uploads/2019/03/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) ## Table of Contents From 85f986510d7faf0eb723b8e39db9bc33b3941592 Mon Sep 17 00:00:00 2001 From: Jonathan Allard <86688525+j-Allard-22@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:54:11 -0500 Subject: [PATCH 78/94] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a49c85..9b84608 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](h This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Our service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). -[![SatelliteWP - WordPress Maintenance](https://www.satellitewp.com/wp-content/uploads/2019/03/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) +[![SatelliteWP - WordPress Maintenance](https://www.satellitewp.com/wp-content/signature/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) ## Table of Contents From d5c7e948ef1dac10185e882cb5e4444b2c8aa74b Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 31 May 2023 11:53:59 -0400 Subject: [PATCH 79/94] Update ignored query strings --- rocket-nginx.ini.disabled | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index f403d75..3cc40e6 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -4,7 +4,7 @@ ; ; Original author: Maxime Jobin ; URL: https://www.maximejobin.com -; Version: 3.0.0 +; Version: 3.0.1 ; Default configuration [default] @@ -42,22 +42,41 @@ cookie_invalidate[] = "comment_author_email_" ; for JavaScript and do not influence the output provided by the server. List all query strings that won't ; invalidate the use of a cached page. ; Reference: https://docs.wp-rocket.me/article/971-caching-query-strings +; Google Analytics and Ads: query_string_ignore[] = "utm_source" query_string_ignore[] = "utm_campaign" query_string_ignore[] = "utm_medium" query_string_ignore[] = "utm_expid" query_string_ignore[] = "utm_term" query_string_ignore[] = "utm_content" +query_string_ignore[] = "_ga" +query_string_ignore[] = "gclid" +query_string_ignore[] = "campaignid" +query_string_ignore[] = "adgroupid" +query_string_ignore[] = "adid" +query_string_ignore[] = "gbraid" +query_string_ignore[] = "wbraid" +; Google DoubleClick +query_string_ignore[] = "gclsrc" +; Adobe Advertising Cloud +query_string_ignore[] = "ef_id" +; Facebook query_string_ignore[] = "fb_action_ids" query_string_ignore[] = "fb_action_types" query_string_ignore[] = "fb_source" query_string_ignore[] = "fbclid" -query_string_ignore[] = "_ga" -query_string_ignore[] = "gclid" +; Mailchimp +query_string_ignore[] = "mc_cid" +query_string_ignore[] = "mc_eid" +; Klavyo +query_string_ignore[] = "_ke" +; Other query_string_ignore[] = "age-verified" query_string_ignore[] = "ao_noptimize" query_string_ignore[] = "usqp" query_string_ignore[] = "cn-reloaded" +query_string_ignore[] = "sscid" +query_string_ignore[] = "msclkid" ; Query strings to cache ; Query strings invalidate the caching process made by Rocket-Nginx. Some strings may need to be cached. From ad4311e6efc2a613a3f7c12b5db9d26b2a2f8139 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 31 May 2023 11:55:40 -0400 Subject: [PATCH 80/94] Update tested versions --- rocket-nginx.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 7edece9..4e5fe92 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,10 +8,10 @@ # 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) +# Tested with WP-Rocket version: 3.13.3 +# Tested with NGINX: 1.25.0 (mainline) # -# Version 3.0.0 +# Version 3.0.1 # ################################################################################################### From 0110ec8d73aa2750710ead12e9b361aba1c9f89f Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 31 May 2023 11:59:46 -0400 Subject: [PATCH 81/94] Fix indentation --- rocket-parser.php | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/rocket-parser.php b/rocket-parser.php index fbafeef..5ffb92f 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -34,18 +34,18 @@ protected function parseIniFile() { // create namespace if necessary if(!isset($config[$name])) { - $config[$name] = array(); + $config[$name] = array(); } // inherit base namespace if(isset($data[$extends])) { - foreach($data[$extends] as $prop => $val) { - $config[$name][$prop] = $val; - } + foreach($data[$extends] as $prop => $val) { + $config[$name][$prop] = $val; + } } // overwrite / set current namespace values foreach($properties as $prop => $val) { - $config[$name][$prop] = $val; + $config[$name][$prop] = $val; } } @@ -201,12 +201,11 @@ protected function getGeneratedQueryStringsToIgnore($queryStrings) { $result .= 'set $rocket_args $args;' . "\n"; foreach ($queryStrings as $name => $value) { + $result .= 'if ($rocket_args ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_args $1$2; '; + $result .= "}\n"; - $result .= 'if ($rocket_args ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; - $result .= 'set $rocket_args $1$2; '; - $result .= "}\n"; - - $iteration++; + $iteration++; } $result .= "\n"; @@ -240,12 +239,11 @@ protected function getGeneratedQueryStringsToCache($queryStrings) { $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; foreach ($queryStrings as $name => $value) { + $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_has_query_cache 1; '; + $result .= "}\n"; - $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; - $result .= 'set $rocket_has_query_cache 1; '; - $result .= "}\n"; - - $iteration++; + $iteration++; } $result .= "\n"; @@ -296,7 +294,7 @@ protected function checkConfigurationFile() { if (file_exists($this->configFile) === false) { die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); - } + } } /** From 99b0d619d910b55cf80c4de2cb1c0470ddfda4ee Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 31 May 2023 12:00:04 -0400 Subject: [PATCH 82/94] Bump version --- rocket-parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-parser.php b/rocket-parser.php index 5ffb92f..d99067d 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -11,7 +11,7 @@ * Original author: Maxime Jobin * URL: https://www.maximejobin.com * -* Version 3.0.0 +* Version 3.0.1 * **************************************************************************************************/ From da1ffcb16550cc1135d31e9dca9c17d926a7cc89 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 25 Aug 2023 11:28:19 -0400 Subject: [PATCH 83/94] Update project URL --- rocket-nginx.ini.disabled | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 3cc40e6..04702d1 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -1,6 +1,6 @@ ; Rocket-Nginx configuration file ; Maintainer: SatelliteWP -; URL: https://github.com/maximejobin/rocket-nginx +; URL: https://github.com/satellitewp/rocket-nginx ; ; Original author: Maxime Jobin ; URL: https://www.maximejobin.com From 0714f97441714e8bb94ecba184460425914ff50d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 25 Aug 2023 11:33:56 -0400 Subject: [PATCH 84/94] Update ignored query strings parameters --- rocket-nginx.ini.disabled | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 04702d1..63f09a5 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -4,7 +4,7 @@ ; ; Original author: Maxime Jobin ; URL: https://www.maximejobin.com -; Version: 3.0.1 +; Version: 3.0.2 ; Default configuration [default] @@ -49,6 +49,10 @@ query_string_ignore[] = "utm_medium" query_string_ignore[] = "utm_expid" query_string_ignore[] = "utm_term" query_string_ignore[] = "utm_content" +query_string_ignore[] = "utm_id" +query_string_ignore[] = "utm_source_platform" +query_string_ignore[] = "utm_creative_format" +query_string_ignore[] = "utm_marketing_tactic" query_string_ignore[] = "_ga" query_string_ignore[] = "gclid" query_string_ignore[] = "campaignid" @@ -68,6 +72,13 @@ query_string_ignore[] = "fbclid" ; Mailchimp query_string_ignore[] = "mc_cid" query_string_ignore[] = "mc_eid" +; Matomo +query_string_ignore[] = "mtm_source" +query_string_ignore[] = "mtm_medium" +query_string_ignore[] = "mtm_campaign" +query_string_ignore[] = "mtm_keyword" +query_string_ignore[] = "mtm_cid" +query_string_ignore[] = "mtm_content" ; Klavyo query_string_ignore[] = "_ke" ; Other From bdd71b8e8f922427b36fa8973889aca99f50116a Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 25 Aug 2023 11:35:45 -0400 Subject: [PATCH 85/94] Update tested versions --- rocket-nginx.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 4e5fe92..516d568 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,10 +8,10 @@ # Maintainer: SatelliteWP.com # URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.13.3 -# Tested with NGINX: 1.25.0 (mainline) +# Tested with WP-Rocket version: 3.14.4.2 +# Tested with NGINX: 1.25.2 (mainline) # -# Version 3.0.1 +# Version 3.0.2 # ################################################################################################### From 2018dfe12b6711aa35310824916dcf158a6175c1 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 25 Aug 2023 11:36:14 -0400 Subject: [PATCH 86/94] Update version --- rocket-parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket-parser.php b/rocket-parser.php index d99067d..37d7b07 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -11,7 +11,7 @@ * Original author: Maxime Jobin * URL: https://www.maximejobin.com * -* Version 3.0.1 +* Version 3.0.2 * **************************************************************************************************/ From 9fb1b0bebf70385fe39db2883590c181afc0ce9c Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Fri, 25 Aug 2023 12:13:13 -0400 Subject: [PATCH 87/94] Add BF Cache question in FAQ and fix broken link to FAQ --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b84608..fe86b28 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This project is sponsored by SatelliteWP, a [WordPress maintenance service](http 1. [Installation](#installation) 1. [Configuration](#configuration) 1. [Debug](#debug) - 1. [FAQ](#css) + 1. [FAQ](#faq) 1. [License](#license) ## Contributors @@ -173,6 +173,10 @@ Reasons for not serving a cached file: ## FAQ +**Is Rocket-Nginx compatible with BF Cache (Back/forward cache)?** + +Yes! If your website does not display sensitive data and is a good match for Back/forward caching, you must must edit your Rocket-Nginx configuration by following the [BF Cache discussion](https://github.com/SatelliteWP/rocket-nginx/issues/197#issuecomment-1693584964) in the issues. + **How do I upgrade from version 1 or 2 to version 3?** We suggest that you save your previous configuration and start over. Take this opportunity to review everything as many things have changed. Officially, version 3.x is not backward-compatible with previous versions. Starging from scratch should not take more than 15 minutes. From d145cb8859cfb04f264a245b83432b6451ac5557 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 20 Sep 2023 10:47:47 -0400 Subject: [PATCH 88/94] Remove hyphen in WP Rocket and add a mention they are using Rocket-Nginx --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fe86b28..b844ccf 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,17 @@ Rocket-Nginx ============ -Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](https://wordpress.org) cache plugin [WP-Rocket](https://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. +Rocket-Nginx is a [Nginx](https://nginx.org) configuration for the [WordPress](https://wordpress.org) cache plugin [WP Rocket](https://wp-rocket.me). It enables Nginx to serve directly previously cached files without calling WordPress or any PHP. It also adds headers to cache CSS, JS and medias in order to leverage browser's cache by reducing request to your web server. + +**You might ask yourself: "How good is this configuration?".** + +Let's just say that WP Rocket themselves use it on their website to make it even faster! This project is sponsored by SatelliteWP, a [WordPress maintenance service](https://www.satellitewp.com/en) located near Montreal, Canada. Our service is offered in both English and French. SatelliteWP fait de l'[entretien de sites WordPress](https://www.satellitewp.com/?utm_source=rocket-nginx). [![SatelliteWP - WordPress Maintenance](https://www.satellitewp.com/wp-content/signature/logo.png "SatelliteWP - WordPress Maintenance")](https://www.satellitewp.com/en?utm_source=rocket-nginx) + ## Table of Contents 1. [Contributors](#contributors) @@ -168,7 +173,7 @@ Reasons for not serving a cached file: * **Arguments found**: One or more argument was found in the request (e.g. ?page=2). * **Maintenance mode**: The .maintenance file was found. Therefore, let's WordPress handle what should be displayed. * **Cookie**: A specific cookie was found and tells to not serve the cached page (e.g. user is logged in, post with password). - * **Specific mobile cache activated**: If you activated specific cache (one for mobile and one for desktop) in WP-Rocket, HTML files (pages, posts, ...) won't be served directly because Rocket-Nginx cannot know if the request was made by mobile or desktop device. + * **Specific mobile cache activated**: If you activated specific cache (one for mobile and one for desktop) in WP Rocket, HTML files (pages, posts, ...) won't be served directly because Rocket-Nginx cannot know if the request was made by mobile or desktop device. * **File not cached**: No cached file was found for that request. ## FAQ @@ -196,7 +201,7 @@ Many things! **Do you have any benchmark about the project ?** -No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP-Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → PHP → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP-Rocket... at least) before serving the static file. +No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → PHP → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP Rocket... at least) before serving the static file. **Will Rocket-Nginx work if my website uses a SSL certificate (https) ?** From 0215f4e187cbc9313ab8d5e339497b2fc54d4047 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 11 Oct 2023 13:02:14 -0400 Subject: [PATCH 89/94] Add more context to benchmark FAQ --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b844ccf..ed25533 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ Many things! **Do you have any benchmark about the project ?** -No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → PHP → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP Rocket... at least) before serving the static file. +No. People love benchmark as much as they hate them. All benchmarks have people claiming that X or Y or Z could have been done to improve the outcome. In this project, the benchmark would depend on how many plugins you have that are affecting the page even if the output is in cache (e.g. WP Rocket executes PHP even when a file is in cache). What we can say though is that you will go from **NGINX → PHP-FPM → WordPress (PHP and Database) → Static file** to **NGINX → Static file**. In other words, you are serving the static file directly from NGINX instead of passing the request to FPM then to PHP (for WP Rocket... at least) before serving the static file. **Will Rocket-Nginx work if my website uses a SSL certificate (https) ?** From 3abb04fc313ba9d0faf454ef28399ed81b885a1c Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 11 Oct 2023 13:03:23 -0400 Subject: [PATCH 90/94] Fix a typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed25533..0e1e2fa 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ Yes! If your website does not display sensitive data and is a good match for Bac **How do I upgrade from version 1 or 2 to version 3?** -We suggest that you save your previous configuration and start over. Take this opportunity to review everything as many things have changed. Officially, version 3.x is not backward-compatible with previous versions. Starging from scratch should not take more than 15 minutes. +We suggest that you save your previous configuration and start over. Take this opportunity to review everything as many things have changed. Officially, version 3.x is not backward-compatible with previous versions. Starting from scratch should not take more than 15 minutes. **What is new in version 3.x?** From 5d6b9a51b9114f851458051f7f8da02cfa9b4992 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Wed, 11 Oct 2023 13:05:41 -0400 Subject: [PATCH 91/94] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e1e2fa..2ac026d 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Make sure you test that your tasks still run after this change! ## Installation -In order to use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/satellitewp/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. +To use the script, you must include it in your actual configuration. If your WordPress website is not yet configured to run with Nginx, you can check the [Nginx configuration for WordPress](https://github.com/satellitewp/rocket-nginx/wiki/Nginx-configuration-for-WordPress) documentation. Only one instance of Rocket-Nginx is needed for all your WordPress websites using WP Rocket. You can generate as many configuration files as needed. From 70d4687c70852f432a65fdfde1e92be2f2b62cc8 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Mon, 29 Apr 2024 12:01:43 -0400 Subject: [PATCH 92/94] Add limitations to readme file --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 2ac026d..35bcda5 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ This project is sponsored by SatelliteWP, a [WordPress maintenance service](http 1. [Installation](#installation) 1. [Configuration](#configuration) 1. [Debug](#debug) + 1. [Limitations](#limitations) 1. [FAQ](#faq) 1. [License](#license) @@ -176,6 +177,21 @@ Reasons for not serving a cached file: * **Specific mobile cache activated**: If you activated specific cache (one for mobile and one for desktop) in WP Rocket, HTML files (pages, posts, ...) won't be served directly because Rocket-Nginx cannot know if the request was made by mobile or desktop device. * **File not cached**: No cached file was found for that request. +## Limitations +Is Rocket-Nginx perfect? No, it's not! We tried to make it as perfect as possible, but Nginx's scripting language does not offer all possibilities offered by a language like PHP, for instance. Therefore, there are some limitations. + +❌**Cache for mobile devices** + +Short answer: Rocket-Nginx won't manage any request if you activate the [Mobile cache feature](https://docs.wp-rocket.me/article/708-mobile-cache). + +In today's world, we usually use a responsive theme that will adapt to your visitor's screen. Therefore, activating the 'cache for mobile device' to create a separate cache for mobile is not needed. It's useless as the same code will be cached twice. That being said, there are cases where the code is different and you might want to use that option. Unfortunately, knowing if the device is a mobile or not is as simple as you might think. There are so many devices and browsers to handle! The Nginx's scripting language is not powerful enough to achieve the validation correctly. Therefore, if you activate this feature, Rocket-Nginx will let WP Rocket handle the device validation and serve the cached page. + +❌**WEBP Compatibility** + +Short answer: Rocket-Nginx won't serve the WebP caching files generated by WP Rocket if you activate the [WebP Compatibility feature](https://docs.wp-rocket.me/article/1282-webp). + +WP Rocket is able to create a specific cache if you used a tool to convert your images (JPG, PNG, ...) to WebP. Unfortunately, the Nginx's scripting language is not powerful enough to achieve the validation correctly. Therefore, if you activate this feature, Rocket-Nginx will let WP Rocket handle the request and serve the right cached page, depending on the context. + ## FAQ **Is Rocket-Nginx compatible with BF Cache (Back/forward cache)?** From 745903bfd5c38b88e16985b9f92b7bc2708404e8 Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 13 Jul 2024 14:33:59 -0400 Subject: [PATCH 93/94] Add mobile detection, add preprocess include, code factoring, add device header --- README.md | 7 +-- rocket-nginx.ini.disabled | 8 +++- rocket-nginx.tmpl | 93 +++++++++++++++++++++++++++------------ rocket-parser.php | 88 +++++++++++++++++++----------------- 4 files changed, 123 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 35bcda5..013f6a7 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ The following header is present no matter if debug is set to true or false: This will add the following headers to your response request: * **X-Rocket-Nginx-Reason**: If serving static is not set to "HIT", what is the reason for calling WordPress. If "HIT", what is the file used (URL). * **X-Rocket-Nginx-File**: If "HIT", what is the file used (path on disk). + * **X-Rocket-Nginx-Device**: What device was used to call the server ("desktop" or "mobile"). Reasons for not serving a cached file: @@ -180,11 +181,11 @@ Reasons for not serving a cached file: ## Limitations Is Rocket-Nginx perfect? No, it's not! We tried to make it as perfect as possible, but Nginx's scripting language does not offer all possibilities offered by a language like PHP, for instance. Therefore, there are some limitations. -❌**Cache for mobile devices** +❌**Encoded slugs** -Short answer: Rocket-Nginx won't manage any request if you activate the [Mobile cache feature](https://docs.wp-rocket.me/article/708-mobile-cache). +Short answer: An encoded slug cannot be served by Rocket-Nginx. -In today's world, we usually use a responsive theme that will adapt to your visitor's screen. Therefore, activating the 'cache for mobile device' to create a separate cache for mobile is not needed. It's useless as the same code will be cached twice. That being said, there are cases where the code is different and you might want to use that option. Unfortunately, knowing if the device is a mobile or not is as simple as you might think. There are so many devices and browsers to handle! The Nginx's scripting language is not powerful enough to achieve the validation correctly. Therefore, if you activate this feature, Rocket-Nginx will let WP Rocket handle the device validation and serve the cached page. +Due to Nginx's scripting limitations, slugs like 'جزازة العشب' are encoded and the file is stored by WP Rocket as '%d8%ac%d8%b2%d8%a7%d8%b2%d8%a9%20%d8%a7%d9%84%d8%b9%d8%b4%d8%a8' (lowercase). Some browsers, like Google Chrome, will send the request as '%D8%AC%D8%B2%D8%A7%D8%B2%D8%A9%20%D8%A7%D9%84%D8%B9%D8%B4%D8%A8' (uppercase). Using a language like PHP, it would be easy to compare these strings as equal. With Nginx, this is not possible unless a third party module (like Perl or Lua) is needed. To make Rocket-Nginx as generic as possible, it was decided to not add a module dependency. If you want to support encoded slugs and add the missing code, note that version 3.1.0 (and later) offers a new configuration include named "preprocess" to modify the `$rocket_uri_path` variable case (force lowercase). ❌**WEBP Compatibility** diff --git a/rocket-nginx.ini.disabled b/rocket-nginx.ini.disabled index 63f09a5..ad82931 100644 --- a/rocket-nginx.ini.disabled +++ b/rocket-nginx.ini.disabled @@ -4,7 +4,7 @@ ; ; Original author: Maxime Jobin ; URL: https://www.maximejobin.com -; Version: 3.0.2 +; Version: 3.1.0 ; Default configuration [default] @@ -108,6 +108,12 @@ css_expiration = "30d" js_expiration = "30d" media_expiration = "30d" +; Mobile user agents +; Mobile user agents regex pattern. By design, a tablet is not a mobile. +; Default value: (?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera mini|avantgo|mobilesafari|docomo|kaios) +mobile_ua_devices = "(?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera mini|avantgo|mobilesafari|docomo|kaios)" + + ; Multiple sections can be created for multiple websites with different configurations. ; It is recommended to use the default values as a base for custom validations. A new configuration ; file will be created for each new section created. diff --git a/rocket-nginx.tmpl b/rocket-nginx.tmpl index 516d568..e2e978d 100644 --- a/rocket-nginx.tmpl +++ b/rocket-nginx.tmpl @@ -8,10 +8,10 @@ # Maintainer: SatelliteWP.com # URL: https://github.com/satellitewp/rocket-nginx # -# Tested with WP-Rocket version: 3.14.4.2 -# Tested with NGINX: 1.25.2 (mainline) +# Tested with WP-Rocket version: 3.16.2.1 +# Tested with NGINX: 1.26.1 (mainline) # -# Version 3.0.2 +# Version 3.1.0 # ################################################################################################### @@ -22,29 +22,31 @@ set $rocket_debug #!# DEBUG #!#; ################################################################################################### # 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 +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_mobile_prefix ""; # Mobile prefix to use when mobile device is detected and mobile cache is activated +set $rocket_is_https 0; # Checks if the request is HTTPS +set $rocket_dynamic ""; # Dynamic value to add to cached filename +set $rocket_device "desktop"; # Device type (desktop or mobile) ################################################################################################### # PAGE CACHE # +# Start includes #!# INCLUDE_START #!# # Define Rocket-Nginx $is_args set $rocket_is_args $is_args; -set $rocket_uri_path ""; -if ($request_uri ~ "^([^?]*)(\?.*)?$") { - set $rocket_uri_path $1; +# Get query string without the parameters (before the '?') +set $rocket_uri_path $request_uri; +if ($request_uri ~* "^([^?]*)(\?.+)$") { + set $rocket_uri_path $1; } # Is GZIP accepted by client ? @@ -70,17 +72,15 @@ 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 +# This variable contains a file to look for. If it exists, WP Rocket is set to +# generate both Desktop and Mobile cache. set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active"; # Query strings to ignore #!# QUERY_STRING_IGNORE #!# +# Adjust $rocket_is_args after processing query strings to ignore if ($rocket_args = "") { set $rocket_is_args ""; } @@ -88,13 +88,29 @@ if ($rocket_args = "") { # Query string to cache #!# QUERY_STRING_CACHE #!# +# Check if device is Mobile +if ($http_user_agent ~* "#!# MOBILE_USER_AGENT #!#") { + set $rocket_device "mobile"; +} + +# Set mobile prefix if mobile mode is activated +if (-f "$rocket_mobile_detection") { + set $rocket_mobile_prefix "-mobile"; +} + +if ($rocket_device != "mobile") { + set $rocket_mobile_prefix ""; +} + # 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_mobile_prefix$rocket_https_prefix"; -set $rocket_file_start "index$rocket_https_prefix"; +# Pre-process includes +#!# INCLUDE_PREPROCESS #!# set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/"; set $rocket_pre_file "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/"; @@ -146,12 +162,6 @@ if ($http_cookie ~* "(#!# COOKIE_INVALIDATE #!#)") { 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"; @@ -162,6 +172,7 @@ if ($rocket_bypass = 1) { if ($rocket_debug = 0) { set $rocket_reason ""; set $rocket_file ""; + set $rocket_device ""; } # If the bypass token is still on, rewrite according to the file linked to the request @@ -177,7 +188,12 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*html$ { 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; + add_header X-Rocket-Nginx-Device $rocket_device; + + # Global includes #!# INCLUDE_GLOBAL #!# + + # HTTP includes #!# INCLUDE_HTTP #!# } @@ -193,7 +209,12 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { 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; + add_header X-Rocket-Nginx-Device $rocket_device; + + # Global includes #!# INCLUDE_GLOBAL #!# + + # HTTP includes #!# INCLUDE_HTTP #!# } @@ -201,9 +222,13 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ { 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; +add_header X-Rocket-Nginx-Device $rocket_device; + +# Global includes #!# INCLUDE_GLOBAL #!# + ################################################################################################### # BROWSER CSS CACHE # @@ -211,7 +236,11 @@ location ~* \.css$ { etag on; gzip_vary on; expires #!# CSS_EXPIRATION #!#; + + # Global includes #!# INCLUDE_GLOBAL #!# + + # CSS includes #!# INCLUDE_CSS #!# } @@ -223,7 +252,11 @@ location ~* \.js$ { etag on; gzip_vary on; expires #!# JS_EXPIRATION #!#; + + # Global includes #!# INCLUDE_GLOBAL #!# + + # JS includes #!# INCLUDE_JS #!# } @@ -234,6 +267,10 @@ location ~* \.js$ { location ~* \.(#!# MEDIA_EXTENSIONS #!#)$ { etag on; expires #!# MEDIA_EXPIRATION #!#; + + # Global includes #!# INCLUDE_GLOBAL #!# + + # Media includes #!# INCLUDE_MEDIA #!# } diff --git a/rocket-parser.php b/rocket-parser.php index 37d7b07..d0e0f87 100644 --- a/rocket-parser.php +++ b/rocket-parser.php @@ -11,7 +11,7 @@ * Original author: Maxime Jobin * URL: https://www.maximejobin.com * -* Version 3.0.2 +* Version 3.1.0 * **************************************************************************************************/ @@ -23,7 +23,7 @@ class RocketParser { /** * Parse the ini configuration file */ - protected function parseIniFile() { + protected function parseIniFile() : array { $data = parse_ini_file($this->configFile, true); $config = array(); @@ -33,12 +33,12 @@ protected function parseIniFile() { $extends = isset($parts[1]) ? trim($parts[1]) : null; // create namespace if necessary - if(!isset($config[$name])) { + if (!isset($config[$name])) { $config[$name] = array(); } // inherit base namespace - if(isset($data[$extends])) { + if (isset($data[$extends])) { foreach($data[$extends] as $prop => $val) { $config[$name][$prop] = $val; } @@ -54,8 +54,10 @@ protected function parseIniFile() { /** * Generate all configuration files + * + * @param array $config Configuration data */ - protected function generateConfigurationFiles($config) { + protected function generateConfigurationFiles($config) : void { // Load template $template = $this->getTemplate(); @@ -92,14 +94,14 @@ protected function generateConfigurationFiles($config) { $output = str_replace('#!# COOKIE_INVALIDATE #!#', $cookies, $output); // Query strings to ignore - $query_strings_ignore = ''; + $query_strings_ignore = '# None'; if (isset($section['query_string_ignore']) && is_array($section['query_string_ignore'])) { $query_strings_ignore = $this->getGeneratedQueryStringsToIgnore($section['query_string_ignore']); } $output = str_replace('#!# QUERY_STRING_IGNORE #!#', $query_strings_ignore, $output); // Query strings to cache - $query_strings_cache = ''; + $query_strings_cache = '# None'; if (isset($section['query_string_cache']) && is_array($section['query_string_cache'])) { $query_strings_cache = $this->getGeneratedQueryStringsToCache($section['query_string_cache']); } @@ -130,6 +132,11 @@ protected function generateConfigurationFiles($config) { $include_start = $this->getGeneratedInclude($name, 'start'); $output = str_replace('#!# INCLUDE_START #!#', $include_start, $output); + // Preprocess include + + $preprocess_start = $this->getGeneratedInclude($name, 'preprocess'); + $output = str_replace('#!# INCLUDE_PREPROCESS #!#', $preprocess_start, $output); + // Global include $include_global = $this->getGeneratedInclude($name, 'global'); $output = str_replace('#!# INCLUDE_GLOBAL #!#', $include_global, $output); @@ -156,6 +163,13 @@ protected function generateConfigurationFiles($config) { $media_extensions = $section['media_extensions']; } $output = str_replace('#!# MEDIA_EXTENSIONS #!#', $media_extensions, $output); + + // Mobile devices + $mobile_ua_devices = 'not_v_a_l_i_d'; + if (isset($section['mobile_ua_devices']) && !empty($section['mobile_ua_devices'])) { + $mobile_ua_devices = $section['mobile_ua_devices']; + } + $output = str_replace('#!# MOBILE_USER_AGENT #!#', $mobile_ua_devices, $output); // Create main configuration folder if it doesn't exist @@ -189,11 +203,12 @@ protected function generateConfigurationFiles($config) { /** * Returns generated query strings statement to ignore - * @param $queryStrings array Query strings to ignore + * + * @param array $queryStrings Query strings to ignore * * @return string Nginx "if" statements */ - protected function getGeneratedQueryStringsToIgnore($queryStrings) { + protected function getGeneratedQueryStringsToIgnore(array $queryStrings) : string { $result = ''; if (isset($queryStrings) && is_array($queryStrings)) { @@ -210,15 +225,12 @@ protected function getGeneratedQueryStringsToIgnore($queryStrings) { $result .= "\n"; $result .= '# Remove & at the beginning (if needed)' . "\n"; - $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n\n"; + $result .= 'if ($rocket_args ~ ^&(.*)) { set $rocket_args $1; }' . "\n"; $result .= "\n"; $result .= '# Do not count arguments if part of caching arguments' . "\n"; $result .= 'if ($rocket_args ~ ^\?$) {' . "\n"; $result .= "\t" . 'set $rocket_is_args "";' . "\n"; - $result .= '}' . "\n"; - } - else { - $result = "# None.\n"; + $result .= '}'; } return $result; @@ -227,47 +239,41 @@ protected function getGeneratedQueryStringsToIgnore($queryStrings) { /** * Returns generated query strings statement to cache * - * @param $queryStrings array Query strings to cache + * @param array $queryStrings Query strings to cache * * @return string Nginx "if" statements */ - protected function getGeneratedQueryStringsToCache($queryStrings) { + protected function getGeneratedQueryStringsToCache(array $queryStrings) : string { $result = ''; + $iteration = 1; - if (isset($queryStrings) && is_array($queryStrings)) { - $iteration = 1; + $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; + foreach ($queryStrings as $name => $value) { + $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; + $result .= 'set $rocket_has_query_cache 1; '; + $result .= "}\n"; - $result .= 'set $rocket_args_tmp $rocket_args;' . "\n"; - foreach ($queryStrings as $name => $value) { - $result .= 'if ($rocket_args_tmp ~ (.*)(?:&|^)' . $value . '=[^&]*(.*)) { '; - $result .= 'set $rocket_has_query_cache 1; '; - $result .= "}\n"; - - $iteration++; - } - - $result .= "\n"; - $result .= 'if ($rocket_has_query_cache = 1) {' . "\n"; - $result .= "\t" . 'set $rocket_args "#$rocket_args";' . "\n"; - $result .= "\t" . 'set $rocket_is_args "";' . "\n"; - $result .= '}' . "\n"; - } - else { - $result = "# None.\n"; + $iteration++; } + $result .= "\n"; + $result .= 'if ($rocket_has_query_cache = 1) {' . "\n"; + $result .= "\t" . 'set $rocket_args "#$rocket_args";' . "\n"; + $result .= "\t" . 'set $rocket_is_args "";' . "\n"; + $result .= '}'; + return $result; } /** * Returns generated include for a section headers * - * @param $config string Configuration name - * @param $section string Section name + * @param string $config Configuration name + * @param string $section Section name * * @return string Include statement */ - protected function getGeneratedInclude($config, $section) { + protected function getGeneratedInclude(string $config, string $section) : string { $dir = dirname(__FILE__); $result = "include {$dir}/conf.d/{$config}/{$section}.*.conf;"; @@ -277,7 +283,7 @@ protected function getGeneratedInclude($config, $section) { /** * Get the template file if it exists */ - protected function getTemplate() { + protected function getTemplate() : string { if (file_exists($this->templateFile) === false) { die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . @@ -290,7 +296,7 @@ protected function getTemplate() { /** * Check if configuration file exists */ - protected function checkConfigurationFile() { + protected function checkConfigurationFile() : void { if (file_exists($this->configFile) === false) { die("Error: the file 'rocket-nginx.ini' could not be found to generate the configuration. " . "You must rename the orginal 'rocket-nginx.ini.disabled' file to 'rocket-nginx.ini' and run this script again."); @@ -300,7 +306,7 @@ protected function checkConfigurationFile() { /** * Generate configuration files */ - public function go() { + public function go() : void { $this->checkConfigurationFile(); $data = $this->parseIniFile(); From d4cad6a78c21feff6dd80b275e726bf2049f4e7d Mon Sep 17 00:00:00 2001 From: Maxime Jobin Date: Sat, 13 Jul 2024 14:43:51 -0400 Subject: [PATCH 94/94] Add preprocess documentation --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 013f6a7..e32c33b 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,10 @@ In the default profile, create a file in the `conf.d/default/` having the follow In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `http.*.conf`. +### Preprocess (after all variables to use are set) + +In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `preprocess.*.conf`. + ### In the CSS section In the default profile, create a file in the `conf.d/default/` having the following filename pattern : `css.*.conf`.