Skip to content

Commit

Permalink
Merge pull request #219 from SatelliteWP/version-3.1
Browse files Browse the repository at this point in the history
Version 3.1
  • Loading branch information
maximejobin authored Jul 25, 2024
2 parents 70d4687 + d4cad6a commit db6e266
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 73 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -167,6 +171,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:
Expand All @@ -180,11 +185,11 @@ Reasons for not serving a cached file:
## <a name='limitations'>Limitations</a>
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**

Expand Down
8 changes: 7 additions & 1 deletion rocket-nginx.ini.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
;
; Original author: Maxime Jobin
; URL: https://www.maximejobin.com
; Version: 3.0.2
; Version: 3.1.0

; Default configuration
[default]
Expand Down Expand Up @@ -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.
Expand Down
93 changes: 65 additions & 28 deletions rocket-nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
###################################################################################################

Expand All @@ -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 ?
Expand All @@ -70,31 +72,45 @@ 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 "";
}

# 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/";
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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 #!#
}

Expand All @@ -193,25 +209,38 @@ 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 #!#
}

# Debug header (when file is not cached)
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
add_header X-Rocket-Nginx-Reason $rocket_reason;
add_header X-Rocket-Nginx-File $rocket_file;
add_header X-Rocket-Nginx-Device $rocket_device;

# Global includes
#!# INCLUDE_GLOBAL #!#



###################################################################################################
# BROWSER CSS CACHE
#
location ~* \.css$ {
etag on;
gzip_vary on;
expires #!# CSS_EXPIRATION #!#;

# Global includes
#!# INCLUDE_GLOBAL #!#

# CSS includes
#!# INCLUDE_CSS #!#
}

Expand All @@ -223,7 +252,11 @@ location ~* \.js$ {
etag on;
gzip_vary on;
expires #!# JS_EXPIRATION #!#;

# Global includes
#!# INCLUDE_GLOBAL #!#

# JS includes
#!# INCLUDE_JS #!#
}

Expand All @@ -234,6 +267,10 @@ location ~* \.js$ {
location ~* \.(#!# MEDIA_EXTENSIONS #!#)$ {
etag on;
expires #!# MEDIA_EXPIRATION #!#;

# Global includes
#!# INCLUDE_GLOBAL #!#

# Media includes
#!# INCLUDE_MEDIA #!#
}
Loading

0 comments on commit db6e266

Please sign in to comment.