Fix 404s by moving skip_cache conditions to server block #692
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Fixes a version of #436 where a system default 404 page is returned instead of the WordPress 404 (via index.html) . To reproduce, provision with cache
enabled: true
and do any of the following:example.com/missing.php?p=1
(triggers the$query_string
condition in code block below)example.com/xmlrpc.php
(triggersnginx_skip_cache_uri
condition in code block below)wp-admin
and then visitexample.com/missing.php
(triggersnginx_skip_cache_cookie
in code block below)Solution
The PR diff display overcomplicates the changes. This PR simply moves the following portion of the caching section up out of the location block context and into the server block context.
Explanation
Why does it fix the problem to move these
if
conditions out of thelocation ~ \.php$
block? Because If Is Evil, particularly when inside a location block. Here is a summary of the affected Trellis location block:The excerpt above appears to exhibit the problem demonstrated in the example titled
# try_files wont work due to if
. If one of theif
conditions is met, thetry_files
directive won't even be run. In our case, this means the WPindex.php
does not load and the system 404 is used instead. I am guessing this is related to the various descriptions here about how 'Nginx traps into the "if" inner block because its condition ... was met.'Justification
Here are two examples recommending placing the
if
condition cache exceptions in the server block instead of the location block: easyengine.io and digitalocean.com, the latter even suggesting that these exceptions "must be used in the server{ } context."