Skip to content

Commit

Permalink
Add condition to add X-Cache-Enabled HTTP header only to loopback r…
Browse files Browse the repository at this point in the history
…equests.

Move `X-Cache-Enabled` HTTP header to PHP-specific `location` block.
  • Loading branch information
strarsis committed Aug 2, 2024
1 parent dbd5b6a commit a71c2de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 16 additions & 1 deletion roles/nginx/templates/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,22 @@ pid /run/nginx.pid;
{% endblock %}

http {
{% block http_begin %}{% endblock %}
{% block http_begin %}
map $upstream_cache_status $header_x_cache_enabled {
default true;
BYPASS "";
}

map $server_addr:$remote_addr $is_loopback_request {
"~^([^:]+):\1$" 1;
default 0;
}

map $is_loopback_request:$header_x_cache_enabled $loopback_header_x_cache_enabled {
default "";
1:true true;
}
{% endblock %}

{% block server_tokens -%}
# Hide nginx version information.
Expand Down
5 changes: 2 additions & 3 deletions roles/wordpress-setup/templates/wordpress-site.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ server {
index index.php index.htm index.html;
add_header Fastcgi-Cache $upstream_cache_status;

# Extra header for WordPress (notably its Site Health check)
add_header X-Cache-Enabled $header_x_cache_enabled;

# Specify a charset
charset utf-8;

Expand Down Expand Up @@ -247,6 +244,8 @@ server {
{% block location_php_basic -%}
try_files $uri /index.php;

# Additional proxy header for WordPress (notably its Site Health check)
add_header X-Cache-Enabled $loopback_header_x_cache_enabled;
{% endblock -%}

{% block cache_config -%}
Expand Down

0 comments on commit a71c2de

Please sign in to comment.