Try_files on nginx to determine whether the file exists

Source: Internet
Author: User

Try_files is a new feature added after nginx0.6.36. It is used to search for N files in a specified directory. If fileN cannot be found, it calls the specified position in fallback to process the request. I personally think that, as the core content of nginx, it can partially replace the cumbersome rewrite function. I have used it in rewrite rewriting of wp super cache and it has also achieved good results.


Try_files
Syntax: try_files file1 [file2... filen] fallback

Default value: None

Scope: location

This command can receive multiple paths as parameters. If the resources in the current path cannot be found, the next path is automatically searched. If the requested static resource does not exist, the fallback request is routed to the backend server for dynamic processing.

Currently, there is a need for static files under the root directory of the website, and static files under the static Directory. static files under the static directory are generated in batches by programs, I want nginx to prioritize the use of files in the static directory without changing the address. If the static files in the root directory do not exist, for example, access the homepage http://example.com/index.html, then nginxwill return to /static/index.html.

After some research, we can use the if command. The key configuration is as follows. This configuration needs to be placed in the front.

The code is as follows: Copy code

If (-e $ document_root/static $ request_uri ){
Rewrite ^/(. *) $/static/$1 break;
Break;
}

Note the following two points:

1. nginx variables should start with a backslash and end with No.
2. nginx strings and variables can be directly connected. If there is any ambiguity, variable names can be enclosed in brackets. The entire string must be enclosed in double quotation marks.
"$ {Document_root}/static $ {request_uri}" is a disadvantage in this method. The file specified by the index command does not work. For example, when you access http://example.com/example.com/index.html. You can use rewrite to fix the issue, but it is uncomfortable to find a targeted command try_files on the nginx trap page.

The code is as follows: Copy code

Set $ static "/static ";
Try_files $ static $ uri/index.html/index. php;

Later, I found that the signed Uri variable automatically added the index.html suffix. This is also possible after experiments.

The code is as follows: Copy code

If (-e "$ {document_root}/static $ {uri }"){
Rewrite ^/(. *) $/static/$ uri break;
}

If (-e $ request_filename ){
Break;
}

Because the last non-existing files are written to index. php, you need to judge whether the file exists again after rewrite.

Example

The author's blog has just been established, wp super cache is an indispensable plug-in, but the default supercache is for apache. htaccess, under nginx

The code is as follows: Copy code


Server {
Set $ cache/wp-content/cache/supercache/$ host;
# Wp-super-cache path
Listen 80;
Server_name _;
Location /{
Root/home/html/s0001/domains/$ host;
Index. php index.html;
# Directly call the html static file after gzip compression
Add_header Content-Type "text/html; charsets = UTF-8 ";
Add_header Content-Encoding "gzip ";
Try_files $ cache/$ uri/index.html.gz @ proxy;
 
    }
# All static files are processed by nginx and compressed with gzip.
Location ~ *. (Jpg | jpeg | png | gif | css | js | swf | mp3 | avi | flv | xml | zip | rar) $ {
Expires 30d;
Gzip on;
Gzip_types text/plain application/x-javascript text/css application/xml;
Root/home/html/s0001/domains/$ host;
    }
# Files that cannot be found are handed over to the backend apache for processing.
Location @ proxy {
Index. php index.htm index.html;
Root/home/html/$ user/domains/$ host;
Proxy_pass http: // 127.0.0.1: 8080;
Proxy_redirect off;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
    }
}

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.