| 1 |
<?php |
| 2 |
/** |
| 3 |
* The template used to generate a sample nginx.conf configuration. |
| 4 |
* |
| 5 |
* This assumes the user has a correct "root "/app/wordpress"; definition in their server block, the cache path is relative to that. |
| 6 |
* |
| 7 |
* @var string $cache_path The relative path from WP_CONTENT_DIR without slashes, e.g. wp-content/cache/solid-performance/page |
| 8 |
* @var int $expiration The expiration time in seconds. |
| 9 |
* |
| 10 |
* @package SolidWP\Performance |
| 11 |
*/ |
| 12 |
|
| 13 |
declare( strict_types=1 ); |
| 14 |
|
| 15 |
$nginx = <<<RULES |
| 16 |
# BEGIN KadencePerformanceNginxCacheRules |
| 17 |
# This should be included in the server{} block of your vhost. |
| 18 |
# This file is automatically generated. Do no edit. Use the `solidwp/performance/nginx/rules` filter. |
| 19 |
|
| 20 |
# Deny direct access to the Kadence Performance swpsp-nginx.conf. |
| 21 |
location ~ swpsp-nginx\.conf$ { |
| 22 |
deny all; |
| 23 |
} |
| 24 |
|
| 25 |
# Headers to always send with each request. |
| 26 |
add_header Referrer-Policy "no-referrer-when-downgrade" always; |
| 27 |
|
| 28 |
# Copy the \$uri variable to ensure rewrite processing is always using the original. |
| 29 |
set \$swpsp_cache_uri \$uri; |
| 30 |
# Assume http for the default scheme. |
| 31 |
set \$swpsp_scheme http; |
| 32 |
# Don't bypass the cache by default. |
| 33 |
set \$swpsp_bypass_cache 0; |
| 34 |
# The default location block to use with try_files in the / location. |
| 35 |
set \$swpsp_cache_try "@html_cache"; |
| 36 |
# The relative cache directory to the configured Nginx root. |
| 37 |
# This supports sub directories automatically, e.g. root "/app/wordpress"; will look in /app/wordpress/wp-content/... |
| 38 |
set \$swpsp_cache_dir "/{$cache_path}"; |
| 39 |
|
| 40 |
# ------------------------------------------------------- |
| 41 |
# Device detection (phones only) |
| 42 |
# Desktop = default (empty suffix) |
| 43 |
# ------------------------------------------------------- |
| 44 |
|
| 45 |
set \$swpsp_device ""; |
| 46 |
|
| 47 |
if (\$http_user_agent ~* "(android.*mobile|iphone|ipod|windows phone|blackberry|bb10|opera mini|mobile.*safari)") { |
| 48 |
set \$swpsp_device "-mobile"; |
| 49 |
} |
| 50 |
|
| 51 |
set \$swpsp_cache_label "desktop"; |
| 52 |
|
| 53 |
if (\$swpsp_device = "-mobile") { |
| 54 |
set \$swpsp_cache_label "mobile"; |
| 55 |
} |
| 56 |
|
| 57 |
# ------------------------------------------------------- |
| 58 |
|
| 59 |
# Extra checks for scheme. |
| 60 |
if (\$https = on) { |
| 61 |
set \$swpsp_scheme https; |
| 62 |
} |
| 63 |
|
| 64 |
if (\$http_x_forwarded_proto = https) { |
| 65 |
set \$swpsp_scheme https; |
| 66 |
} |
| 67 |
|
| 68 |
# Bypass all post requests. |
| 69 |
if (\$request_method = POST) { |
| 70 |
set \$swpsp_bypass_cache 1; |
| 71 |
} |
| 72 |
|
| 73 |
# Bypass URLs with query strings. |
| 74 |
if (\$query_string != "") { |
| 75 |
set \$swpsp_bypass_cache 1; |
| 76 |
} |
| 77 |
|
| 78 |
# Bypass URLs with the following segments. |
| 79 |
if (\$request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|/cart/|/basket/|/checkout/|/my-account/") { |
| 80 |
set \$swpsp_bypass_cache 1; |
| 81 |
} |
| 82 |
|
| 83 |
# Bypass cache if the following cookies are set. |
| 84 |
if (\$http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|edd_items_in_cart|woocommerce_items_in_cart") { |
| 85 |
set \$swpsp_bypass_cache 1; |
| 86 |
} |
| 87 |
|
| 88 |
# If the request wants a gzip response, try that first. |
| 89 |
if (\$http_accept_encoding ~ gzip) { |
| 90 |
set \$swpsp_cache_try "@gz_cache"; |
| 91 |
} |
| 92 |
|
| 93 |
# If we need to bypass the cache, use named @wordpress location directly. |
| 94 |
if (\$swpsp_bypass_cache = 1) { |
| 95 |
set \$swpsp_cache_try "@wordpress"; |
| 96 |
} |
| 97 |
|
| 98 |
# All requests. If you have this in your configuration already, ensure to replace it with ours. |
| 99 |
location / { |
| 100 |
try_files \$uri \$swpsp_cache_try; |
| 101 |
} |
| 102 |
|
| 103 |
# ------------------------------------------------------- |
| 104 |
# GZIP CACHE |
| 105 |
# ------------------------------------------------------- |
| 106 |
|
| 107 |
# Attempt to serve our already compressed gzip file without having Nginx compress it again. |
| 108 |
# This will automatically try the @html_cache location if no file is found. |
| 109 |
location @gz_cache { |
| 110 |
internal; |
| 111 |
log_not_found off; |
| 112 |
recursive_error_pages on; |
| 113 |
error_page 404 = @html_cache; |
| 114 |
|
| 115 |
gzip off; |
| 116 |
types { |
| 117 |
text/html gz html; |
| 118 |
} |
| 119 |
default_type text/html; |
| 120 |
|
| 121 |
expires {$expiration}s; |
| 122 |
etag on; |
| 123 |
if_modified_since exact; |
| 124 |
|
| 125 |
add_header Content-Encoding gzip; |
| 126 |
add_header X-Cached-By "Kadence Performance (nginx)"; |
| 127 |
add_header X-Cache "HIT (\$swpsp_cache_label)"; |
| 128 |
add_header X-Cache-Encoding "GZIP"; |
| 129 |
add_header Cache-Control "public"; |
| 130 |
add_header Vary "Accept-Encoding"; |
| 131 |
|
| 132 |
rewrite ^ "\${swpsp_cache_dir}/\${host}\${swpsp_cache_uri}/index-\${swpsp_scheme}\${swpsp_device}.gz" break; |
| 133 |
} |
| 134 |
|
| 135 |
# ------------------------------------------------------- |
| 136 |
# HTML CACHE |
| 137 |
# ------------------------------------------------------- |
| 138 |
|
| 139 |
# Attempt to serve a fallback html cache file, if it exits. |
| 140 |
location @html_cache { |
| 141 |
internal; |
| 142 |
log_not_found off; |
| 143 |
recursive_error_pages on; |
| 144 |
error_page 404 = @wordpress; |
| 145 |
|
| 146 |
# Have Nginx compress this if the request allows it and our gz file is missing. |
| 147 |
gzip on; |
| 148 |
|
| 149 |
expires {$expiration}s; |
| 150 |
etag on; |
| 151 |
if_modified_since exact; |
| 152 |
|
| 153 |
add_header X-Cached-By "Kadence Performance (nginx)"; |
| 154 |
add_header X-Cache "HIT (\$swpsp_cache_label)"; |
| 155 |
add_header X-Cache-Encoding "HTML"; |
| 156 |
add_header Cache-Control "public"; |
| 157 |
add_header Vary "Accept-Encoding"; |
| 158 |
|
| 159 |
rewrite ^ "\${swpsp_cache_dir}/\${host}\${swpsp_cache_uri}/index-\${swpsp_scheme}\${swpsp_device}.html" break; |
| 160 |
} |
| 161 |
|
| 162 |
# The default WordPress location. |
| 163 |
location @wordpress { |
| 164 |
try_files \$uri \$uri/ /index.php?\$args; |
| 165 |
} |
| 166 |
# END KadencePerformanceNginxCacheRules |
| 167 |
|
| 168 |
RULES; |
| 169 |
|
| 170 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, StellarWP.XSS.EscapeOutput.OutputNotEscaped |
| 171 |
echo apply_filters( 'solidwp/performance/nginx/rules', $nginx, $cache_path ); |
| 172 |
|