How to write the rollback target instruction in order when static files fall onto the Web server?
The instruction (try_files) that is responsible for finding files in order when static pseudo-URLs fall onto the web server should be written as 'the last parameter is an address', not to list a non-existent file. Its semantics are to check the existence of files in the given order, using the first found file to handle the request in the current context; if all are not found, then an internal redirection is performed to the address given by the last parameter.
What order does this instruction find files?
Three key points determine the writing style:
- Order-sensitive: judged one by one from left to right, the first existing one is adopted, and the others are no longer considered.
- Current context processing: After a hit, it will not go through the position matching again, so when rewriting the path in the position block, pay attention to the scope of action;
- The last parameter is a fallback: it does not participate in the existence judgment and is directly treated as an internal redirection target.
How to choose between two syntax forms
| Writing style | Meaning | Suitable for scenes |
|---|---|---|
| File list plus an address at the end | Internal redirection to this address when no hits | Static redirection is unifiedly handed over to the backend entry processing |
| File list plus a status code at the end | Directly return this status code when no hits | Static resources, thumbnails and such should not enter the backend requests |
The scenario of writing back as a status code, where the request will not enter the backend again, saving the cost of an unnecessary explanation; writing back as a backend address scenario, it is only necessary to ensure that the address can be processed in the same block.
Typical rollback chain in the pseudo-static scenario
The pseudo-static address generated by the content system does not correspond to the real existing file on the disk, so the search chain should be arranged in the order of 'let the real file take precedence first, and finally fall to the entry'.
- The request path itself - hits the existing static files (images, attachments, generated HTML);
- Paths ending with a slash - used for directory existence judgment;
- The backend entry address plus query string - the remaining address is handed over to the content system for interpretation.
The third item is the fallback target, which must be in address format and cannot be written as a status code, otherwise the entire site's pseudo-static pages will return an error directly.
The slash convention for directory judgment
To determine if a directory exists, a slash should be added at the end of the name. Without this slash, files and directories with the same name will be confused; if a slash is used on a file, it will cause the file that should exist to be permanently judged as non-existent.
The connection with internal redirection and deployment methods
This instruction handles the 'which file does the address fall into', and the background 301 redirect handles the 'moving the old address to the new address'. The two layers should not pretend to be each other: use a lookup chain to implement the jump, which will lose the explicit status code semantics, and the抓取方 cannot read the relocation relationship from the response.
The deployment method also affects the modifiability of this layer. When deploying with Docker images or panels, the server configuration is often included in the image or site template, and changing files directly in the container will disappear during the next rebuild. The correct approach is to write the pseudo-static rules and lookup chains in a versionable configuration mount, and then verify each address individually after making changes.
Common questions
Why are there still forty-four when the fallback target is written as the backend address?A common reason is that there is no corresponding processing block for the backend address in the same position block, and after internal redirection, it is intercepted by the same rule again, forming an unreturnable loop.
Should the static pages that have already been generated be placed in the progress directory for judgment?It should be placed in front. Only by making the generated static files take precedence can the benefits of pseudo-static 'generate on demand and reuse' be reflected.
Can it be used to replace the pseudo-static rules?No. This command only does file existence judgment and fallback, and the correspondence relationship between the address and parameters is still defined by the pseudo-static rules within the site.