Yaf: nginx Rewrite Rules

Source: Internet
Author: User
try_files $uri $uri/ /index.php$is_args$args;



Put this line of code in the server {} block.

Explanations:

The first is the nginx try_files command. When a request occurs, such as "/ABC", it will check "/ABC" ($ URI) whether the file exists and whether the "/ABC/" ($ uri/) directory exists. If not, redirect to the last parameter "/index. PHP $ is_args $ ARGs ".

We know that index. php is the "entry file" of the framework ".

$ ARGs is a built-in nginx variable that represents the query string in the URL, that is, the get parameter, like "A = 1 & B = 2 & C = 3 ".

$ Is_args depends on $ ARGs, which is expressed in a single expression of three elements: $ is_args = empty ($ ARGs )? '':'? ';

In this way, let's assume several URLs to see how the redirection works:

/Index/Index =>/index. php

/Admin/index? Id = 1 =>/index. php? Id = 1

Is it strange that, after the redirection, the paths disappear, so how can we route the routes inside the framework?

The answer is $ _ server ['request _ URI '] (this is not the only way. The framework will also consider the special processing in path_info and IIS, but nginx and Apache only need to have request_uri.) You only need to know that, although nginx has been internally redirected, The request_uri parameter is not changed, which indicates the original URL, that is, the URL in the address bar of the browser.

After the above two redirection occurs, in fact:

$ _ Server ['request _ URI '] = "/index/Index ";

$ _ Server ['request _ URI '] = "/admin/index? Id = 1 ";

Then, the framework will parse the route according to $ _ server ['request _ URI.

I have attached my own Configuration:

Server {Listen 80; SERVER_NAME your own domain name; index index.html index.htm index. PHP; root/home/wwwroot/yaf; try_files $ URI // index. PHP $ is_args $ ARGs; location ~ . * \. (PhP | PhP5 )? $ {Fastcgi_pass Unix:/tmp/php-cgi.sock; fastcgi_index index. php; Include fcgi. conf;} location/status {stub_status on; access_log off;} location ~ . * \. (GIF | JPG | JPEG | PNG | BMP | SWF) $ {expires 30d;} location ~ . * \. (JS | CSS )? $ {Expires 12 h;} access_log/home/wwwlogs/access. Log Access ;}



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.