Skip to content

Commit 7805d75

Browse files
author
Maxime Jobin
committed
Add HSTS support
1 parent d1005ab commit 7805d75

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

rocket-nginx.conf

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
###################################################################################################
1616

1717
set $rocket_debug 0; # Add debug information into header
18+
set $rocket_hsts_value ""; # HTTP Strict Transport Security (if you want overwrite default)
1819

1920
###################################################################################################
2021
# Do not alter theses values
2122
#
2223
set $rocket_bypass 1; # Should NGINX bypass WordPress and call cache file directly ?
2324
set $rocket_encryption ""; # Is GZIP accepted by client ?
2425
set $rocket_file ""; # Filename to use
25-
set $rocket_is_bypassed "No"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Bypass
26+
set $rocket_is_bypassed "No"; # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Bypass
2627
set $rocket_reason ""; # Reason why cache file was not used. If cache file is used, what file was used
27-
set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are using HTTPS
28+
set $rocket_https_prefix ""; # HTTPS prefix to use when cached files are using HTTPS
29+
set $rocket_hsts 0; # Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS
30+
set $rocket_hsts_value_default "max-age=31536000; includeSubDomains";
2831

2932

3033
###################################################################################################
@@ -39,6 +42,17 @@ if ($http_accept_encoding ~ gzip) {
3942
# Is SSL request ?
4043
if ($https = "on") {
4144
set $rocket_https_prefix "-https";
45+
set $rocket_hsts 1;
46+
}
47+
48+
# If HSTS value is not set, use default value
49+
if ($rocket_hsts_value = "") {
50+
set $rocket_hsts_value "$rocket_hsts_value_default";
51+
}
52+
53+
# If HSTS is disabled, unset HSTS set for Rocket-Nginx configuration
54+
if ($rocket_hsts = "0") {
55+
set $rocket_hsts_value "";
4256
}
4357

4458
# File/URL to return IF we must bypass WordPress
@@ -101,6 +115,7 @@ location ~ /wp-content/cache/wp-rocket/.*html$ {
101115
add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
102116
add_header X-Rocket-Nginx-Reason $rocket_reason;
103117
add_header X-Rocket-Nginx-File $rocket_file;
118+
add_header Strict-Transport-Security "$rocket_hsts_value";
104119
expires 30d;
105120
}
106121

@@ -114,6 +129,7 @@ location ~ /wp-content/cache/wp-rocket/.*_gzip$ {
114129
add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
115130
add_header X-Rocket-Nginx-Reason $rocket_reason;
116131
add_header X-Rocket-Nginx-File $rocket_file;
132+
add_header Strict-Transport-Security "$rocket_hsts_value";
117133
expires 30d;
118134
}
119135

@@ -122,6 +138,8 @@ add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
122138
add_header X-Rocket-Nginx-Reason $rocket_reason;
123139
add_header X-Rocket-Nginx-File $rocket_file;
124140

141+
# No HSTS header added here. We suppose it's correctly added in the site configuration
142+
125143

126144
###################################################################################################
127145
# BROWSER CSS CACHE

0 commit comments

Comments
 (0)