Skip to content

Commit 745903b

Browse files
committed
Add mobile detection, add preprocess include, code factoring, add device header
1 parent 70d4687 commit 745903b

File tree

4 files changed

+123
-73
lines changed

4 files changed

+123
-73
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ The following header is present no matter if debug is set to true or false:
167167
This will add the following headers to your response request:
168168
* **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).
169169
* **X-Rocket-Nginx-File**: If "HIT", what is the file used (path on disk).
170+
* **X-Rocket-Nginx-Device**: What device was used to call the server ("desktop" or "mobile").
170171

171172

172173
Reasons for not serving a cached file:
@@ -180,11 +181,11 @@ Reasons for not serving a cached file:
180181
## <a name='limitations'>Limitations</a>
181182
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.
182183

183-
**Cache for mobile devices**
184+
**Encoded slugs**
184185

185-
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).
186+
Short answer: An encoded slug cannot be served by Rocket-Nginx.
186187

187-
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.
188+
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).
188189

189190
**WEBP Compatibility**
190191

rocket-nginx.ini.disabled

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
;
55
; Original author: Maxime Jobin
66
; URL: https://www.maximejobin.com
7-
; Version: 3.0.2
7+
; Version: 3.1.0
88

99
; Default configuration
1010
[default]
@@ -108,6 +108,12 @@ css_expiration = "30d"
108108
js_expiration = "30d"
109109
media_expiration = "30d"
110110

111+
; Mobile user agents
112+
; Mobile user agents regex pattern. By design, a tablet is not a mobile.
113+
; 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)
114+
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)"
115+
116+
111117
; Multiple sections can be created for multiple websites with different configurations.
112118
; It is recommended to use the default values as a base for custom validations. A new configuration
113119
; file will be created for each new section created.

rocket-nginx.tmpl

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
# Maintainer: SatelliteWP.com
99
# URL: https://github.com/satellitewp/rocket-nginx
1010
#
11-
# Tested with WP-Rocket version: 3.14.4.2
12-
# Tested with NGINX: 1.25.2 (mainline)
11+
# Tested with WP-Rocket version: 3.16.2.1
12+
# Tested with NGINX: 1.26.1 (mainline)
1313
#
14-
# Version 3.0.2
14+
# Version 3.1.0
1515
#
1616
###################################################################################################
1717

@@ -22,29 +22,31 @@ set $rocket_debug #!# DEBUG #!#;
2222
###################################################################################################
2323
# Do not alter theses values
2424
#
25-
set $rocket_bypass 1; # Should NGINX bypass WordPress and call cache file directly ?
26-
set $rocket_encryption ""; # Is GZIP accepted by client ?
27-
set $rocket_file ""; # Filename to look for
28-
set $rocket_is_bypassed "MISS"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Serving-Static
29-
set $rocket_reason ""; # Reason why cache file was not used. If cache file is used, what file was used
30-
set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are using HTTPS
31-
set $rocket_has_query_cache 0; # Checks if a query string from URL is found from the cached query string
32-
set $rocket_is_https 0; # Checks if the request is HTTPS
33-
set $rocket_support_webp 0; # Checks if the request supports WebP
34-
set $rocket_dynamic ""; # Dynamic value to add to cached filename
25+
set $rocket_bypass 1; # Should NGINX bypass WordPress and call cache file directly ?
26+
set $rocket_encryption ""; # Is GZIP accepted by client ?
27+
set $rocket_file ""; # Filename to look for
28+
set $rocket_is_bypassed "MISS"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Serving-Static
29+
set $rocket_reason ""; # Reason why cache file was not used. If cache file is used, what file was used
30+
set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are using HTTPS
31+
set $rocket_mobile_prefix ""; # Mobile prefix to use when mobile device is detected and mobile cache is activated
32+
set $rocket_is_https 0; # Checks if the request is HTTPS
33+
set $rocket_dynamic ""; # Dynamic value to add to cached filename
34+
set $rocket_device "desktop"; # Device type (desktop or mobile)
3535

3636
###################################################################################################
3737
# PAGE CACHE
3838
#
3939

40+
# Start includes
4041
#!# INCLUDE_START #!#
4142

4243
# Define Rocket-Nginx $is_args
4344
set $rocket_is_args $is_args;
4445

45-
set $rocket_uri_path "";
46-
if ($request_uri ~ "^([^?]*)(\?.*)?$") {
47-
set $rocket_uri_path $1;
46+
# Get query string without the parameters (before the '?')
47+
set $rocket_uri_path $request_uri;
48+
if ($request_uri ~* "^([^?]*)(\?.+)$") {
49+
set $rocket_uri_path $1;
4850
}
4951

5052
# Is GZIP accepted by client ?
@@ -70,31 +72,45 @@ if ($rocket_is_https = "1") {
7072
set $rocket_https_prefix "-https";
7173
}
7274

73-
# Check if request supports WebP ?
74-
if ($http_accept ~* "webp") {
75-
set $rocket_support_webp "1";
76-
}
77-
7875
# Set mobile detection file path
76+
# This variable contains a file to look for. If it exists, WP Rocket is set to
77+
# generate both Desktop and Mobile cache.
7978
set $rocket_mobile_detection "$document_root/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$request_uri/.mobile-active";
8079

8180
# Query strings to ignore
8281
#!# QUERY_STRING_IGNORE #!#
8382

83+
# Adjust $rocket_is_args after processing query strings to ignore
8484
if ($rocket_args = "") {
8585
set $rocket_is_args "";
8686
}
8787

8888
# Query string to cache
8989
#!# QUERY_STRING_CACHE #!#
9090

91+
# Check if device is Mobile
92+
if ($http_user_agent ~* "#!# MOBILE_USER_AGENT #!#") {
93+
set $rocket_device "mobile";
94+
}
95+
96+
# Set mobile prefix if mobile mode is activated
97+
if (-f "$rocket_mobile_detection") {
98+
set $rocket_mobile_prefix "-mobile";
99+
}
100+
101+
if ($rocket_device != "mobile") {
102+
set $rocket_mobile_prefix "";
103+
}
104+
91105
# File/URL to return IF we must bypass WordPress
92106
# Desktop: index.html
93107
# Gzip: index.html_gzip
94108
# HTTPS: index-https.html
95109
# Mobile: index-mobile-https.html
110+
set $rocket_file_start "index$rocket_mobile_prefix$rocket_https_prefix";
96111

97-
set $rocket_file_start "index$rocket_https_prefix";
112+
# Pre-process includes
113+
#!# INCLUDE_PREPROCESS #!#
98114

99115
set $rocket_pre_url "/#!# WP_CONTENT_URI #!#/cache/wp-rocket/$http_host/$rocket_uri_path/$rocket_args/";
100116
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 #!#)") {
146162
set $rocket_reason "Cookie";
147163
}
148164

149-
if (-f "$rocket_mobile_detection") {
150-
set $rocket_bypass 0;
151-
set $rocket_is_bypassed "BYPASS";
152-
set $rocket_reason "Specific mobile cache activated";
153-
}
154-
155165
# If the bypass token is still on, let's bypass WordPress with the cached URL
156166
if ($rocket_bypass = 1) {
157167
set $rocket_is_bypassed "HIT";
@@ -162,6 +172,7 @@ if ($rocket_bypass = 1) {
162172
if ($rocket_debug = 0) {
163173
set $rocket_reason "";
164174
set $rocket_file "";
175+
set $rocket_device "";
165176
}
166177

167178
# 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$ {
177188
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
178189
add_header X-Rocket-Nginx-Reason $rocket_reason;
179190
add_header X-Rocket-Nginx-File $rocket_file;
191+
add_header X-Rocket-Nginx-Device $rocket_device;
192+
193+
# Global includes
180194
#!# INCLUDE_GLOBAL #!#
195+
196+
# HTTP includes
181197
#!# INCLUDE_HTTP #!#
182198
}
183199

@@ -193,25 +209,38 @@ location ~ /#!# WP_CONTENT_URI #!#/cache/wp-rocket/.*_gzip$ {
193209
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
194210
add_header X-Rocket-Nginx-Reason $rocket_reason;
195211
add_header X-Rocket-Nginx-File $rocket_file;
212+
add_header X-Rocket-Nginx-Device $rocket_device;
213+
214+
# Global includes
196215
#!# INCLUDE_GLOBAL #!#
216+
217+
# HTTP includes
197218
#!# INCLUDE_HTTP #!#
198219
}
199220

200221
# Debug header (when file is not cached)
201222
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
202223
add_header X-Rocket-Nginx-Reason $rocket_reason;
203224
add_header X-Rocket-Nginx-File $rocket_file;
225+
add_header X-Rocket-Nginx-Device $rocket_device;
226+
227+
# Global includes
204228
#!# INCLUDE_GLOBAL #!#
205229

206230

231+
207232
###################################################################################################
208233
# BROWSER CSS CACHE
209234
#
210235
location ~* \.css$ {
211236
etag on;
212237
gzip_vary on;
213238
expires #!# CSS_EXPIRATION #!#;
239+
240+
# Global includes
214241
#!# INCLUDE_GLOBAL #!#
242+
243+
# CSS includes
215244
#!# INCLUDE_CSS #!#
216245
}
217246

@@ -223,7 +252,11 @@ location ~* \.js$ {
223252
etag on;
224253
gzip_vary on;
225254
expires #!# JS_EXPIRATION #!#;
255+
256+
# Global includes
226257
#!# INCLUDE_GLOBAL #!#
258+
259+
# JS includes
227260
#!# INCLUDE_JS #!#
228261
}
229262

@@ -234,6 +267,10 @@ location ~* \.js$ {
234267
location ~* \.(#!# MEDIA_EXTENSIONS #!#)$ {
235268
etag on;
236269
expires #!# MEDIA_EXPIRATION #!#;
270+
271+
# Global includes
237272
#!# INCLUDE_GLOBAL #!#
273+
274+
# Media includes
238275
#!# INCLUDE_MEDIA #!#
239276
}

0 commit comments

Comments
 (0)