How to check the status code conditions and inheritance level when the security response header is added but not present in some responses?
Why is the security response header added, but not on some responses? Most cases are not due to non-effectiveness, but one of these two things: the status code of that response is not within the effective range of the header addition instruction, or the definition is written in another configuration level, and the upper group has not inherited. Troubleshooting should be based on the specific response missing the header to reproduce, rather than checking the normal page.
First confirm the type of response missing the header
The most common report is that the 'home page check is all green, but the error page is missing the header'. The reason is clearly written in the document: the header instruction is only added to the response when the response code is equal to 200, 201, 204, 206, 301, 302, 303, 304, 307, or 308. The returns in the four hundred and five hundred sections are not on this table, including common forbidden access, internal errors, and returns when static resources cannot be found. That is to say, the passing conclusion of the check tool on the normal page does not represent the abnormal path.
How to bypass the activation conditions
A solution is provided in the same document: add the always parameter, which will add this field regardless of the response code. This parameter has been provided since version 1.7.5. It is important to clarify the pros and cons - using always will add this set of headers to all returned data, including those generated by error pages or debugging entries. The benefit is complete coverage, but the cost is that if a header is incorrectly written, it will affect all responses. Security headers usually hope to take effect without exception, so this parameter is worth using in most scenarios.
| Phenomenon | Likely cause | How to verify | Common modifications |
|---|---|---|---|
| Present on normal pages, not on error pages | Status code is not in the effective list | Manually request a non-existent address to see the response | Add always parameter |
| The upper level wrote, and all in a certain sub-block disappeared | The same level defined the header command, and the upper group does not inherit | Compare the position of the command in the configuration layer layer by layer | Rewrite the group in the sub-block or use the inheritance parameter |
| All are local, and a part is missing online | The response was rewritten by the front-end cache or compression layer | Request with random parameters by bypassing the cache | At the layer generating the response |
| Missing header in redirect response | Only codes in the list are effective in the three hundred segments | Requesting an old address known to redirect | Change the redirect to internal rewrite or supplement on the target page |
Inheritance hierarchy is the easiest to step on
This rule in the document is worth reading word by word: these instructions are inherited from the upper level only when no header instructions are defined at the current level. It is not a merge, but a replacement of the entire group.A typical accident is: A set of security headers is written in the main configuration of the site, and later, in order to add a header instruction related to caching in a sub-path block, a line of header addition instruction is written, resulting in the entire group above disappearing in this sub-path.When checking, don't just ask 'What has this sub-block written,' but ask 'Has this level written?'——as soon as it has been written once, the entire group above becomes invalid. The newer versions provide instructions to redefine inheritance rules, named add_header_inherit, appearing in version 1.29.3, which allows the lower level to append rather than replace on the basis of the upper level.
Locating which layer generated the position
Another source of missing response headers is: this response is not returned by the site program at all. Reverse proxy, caching layer, and load balancing can independently produce returns before the site, such as cache hits and error pages generated by proxy timeouts. The method to distinguish is to see if the response contains the fields that the program itself writes, and then directly request the site service port to reproduce it after bypassing the front-end layer. AnQiCMS listens on port 8001 by default, and is usually reverse proxied to it by the front-end server during deployment. Both sides have their own configuration layer response header settings, and comparing layer by layer is faster than a full restart to find the differences.
Review actions after going online
After the modification, it is recommended to take a response from each of the four paths: normal page, redirect address, non-existent address, and authentication entry. These four cover both types of status codes inside and outside the effective list, as well as the positions where the inheritance rules will be triggered. It is much more informative to record the header fields of each response and compare them, as opposed to running the score repeatedly on the checking site - the checking site usually only tests the homepage.
Common questions
Q: Can it be considered that the configuration is fine if only the homepage passes the check? A: No. The homepage has two hundred successful responses, which fall exactly into the effective list, and the performance of abnormal paths cannot be seen.
What is the reason why only writing one header in the child configuration will take away a group of headers? Answer: Because once a header addition instruction is defined at the same level, the upper level definition no longer inherits, and the replacement occurs for the entire group.
Why is the configuration still missing headers online after the change? Answer: First, confirm whether this response is directly returned by the cache or proxy layer, and the configuration of that layer needs to be supplemented separately.