Explanation of the try_files command in nginx

Source: Internet
Author: User
Official introduction to the try_files command is confusing. after some summary on the Internet, the core function of try_files is to replace rewrite. Try_files syntax: try_filesfile... uri or try_filesfile... default code: none... official introduction to the try_files command is confusing. after some summary on the Internet, the core function of try_files is to replace rewrite. Try_files syntax: try_files file... uri or try_files file... = code default value: No scope: server location Checks for the existence of files in order, and returns the first file that is found. A trailing slash indicates a directory-$ uri /. in the event that no file is found, an internal redirect to the last parameter is invoked. do note that only the last parameter causes an internal redirect, former ones jus T sets the internal URI pointer. the last parameter is the fallback URI and * must * exist, or else an internal error will be raised. named locations can be used. unlike with rewrite, $ args are not automatically preserved if the fallback is not a named location. if you need args preserved, you must do so explicitly: try_files $ uri // index. php? Q = $ uri & $ args; check whether the file exists in sequence and return the first file found. The slash at the end of the slash is the folder-$ uri /. If none of the files can be found, an internal redirection will be performed to the last parameter. Make sure that only the last parameter can cause an internal redirection. The previous parameter only sets the internal URI direction. The last parameter is a rollback URI and must exist. Otherwise, an internal 500 error will occur. The named location can also be used in the last parameter. Unlike the rewrite command, if the rollback URI is not the named location, $ args will not be automatically retained. if you want to retain $ args, it must be explicitly stated. Try_files $ uri // index. php? Q = $ uri & $ args; the instance analysis try_files will try the files you listed and set the internal file to point. For example: try_files/app/cache/$ uri @ fallback; and index. php index.html; it will detect $ document_root/app/cache/index. php, $ document_root/app/cache/index.html and $ document_root $ uri. if not, internal redirection to @ fallback. You can also use a file or status code (= 404) as the last parameter. if the last parameter is a file, the file must exist. Note that try_files outside the last parameter will not cause internal redirection for any reason. For example, nginx does not parse the php file and returns try_files $ uri/cache with text code. php @ fallback; because this command sets the internal file to direct to $ document_root/cache. php does not return, but internal redirection does not occur. therefore, no location segment is processed and the text is returned. (If the index command is added, PHP can be parsed because index triggers an internal redirection)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.