Nginx Notes and summaries (6) Location: Exact match

Source: Internet
Author: User
Tags nginx server

In the/usr/local/nginx/conf/nginx.conf server segment, location represents a different positioning based on the URI: the different parts of the site are targeted to different processing methods, for example, how to invoke the PHP interpreter in the. php file.

Location Syntax:

Location [=|~|~*|^~]/uri/{...}

The location type is divided into:

Location = Patt {}[exact match]

Location Patt {}[General match]

Location = ~patt {}[regular match]

Precision Matching

Match the first to see if there is no exact match, if any, then stop the matching process:

Location = Patt {

Config A

}

if $uri = = Patt, the match succeeds, using config A.

"Example Step 1"

Comment out the server information that was previously configured in/usr/local/nginx/conf/nginx.conf, edit it in the default server segment, and then access 192.168.254.100 to display

At this point the configuration of the location in the server segment is:

        Location/{            root   html;            Index  index.html index.htm;        }

At this point, access to 192.168.254.100 actually access the directory is:/usr/local/nginx/html/index.html

Note: The absolute path to HTML in root is/usr/local/nginx/html, and there is only one index.html file in the directory

"Example Step 2"

At this point, add a precise match (priority match, and immediately stop the matching process) at the top of the general match location/, at which point the location information is:

        Location =/{            root  /var/www;            Index  index.html index.htm;        }        Location/{            root   html;            Index  index.html index.htm;        }

To show the difference, the exact matching directory is set to/var/www, which has only one file Index.htm:i ' m/var/www/index.htm

Smooth restart Nginx

Access Http://192.168.254.100/, still showing

The reason: When the input 192.168.254.100 this IP, is actually access to a file, the exact match to find the file is index.html, that is, access to 192.168.254.100 is Access 192.168.254.100/ Index.html, you can only access the HTML (absolute path to/usr/local/nginx/html) index.html in this case, which explains why the Welcome to Nginx page appears.

"Example Step 3"

But once the exact match of the index.html and index.htm is swapped, the position:

        Location =/{            root  /var/www;            Index  index.htm index.html;        }        Location/{            root   html;            Index  index.html index.htm;        }

  

When you access http://192.168.254.100/, 404 Not Found appears:

View/usr/local/nginx/logs/error.log:

Tip/usr/local/nginx/html/index.htm can't find

"Example Step 4"

Modify the location of the exact match, at which point the location information is:

        Location =/index.htm {            root  /var/www/;            Index  index.htm index.html;        }        Location/{            root   html;            Index  index.html index.htm;        }

Smooth restart Nginx, Access http://192.168.254.100/index.htm, show I ' m/var/www/index.htm

The exact match was taken and matched to the file/var/www/index.htm

"Example Step 5"

Modify the location of the common match, at which point the location information is:

        Location =/index.htm {            root  /var/www/;            Index  index.htm index.html;        }        location/index.htm {            root   html;            Index  index.html index.htm;        }

Smooth restart Nginx, Access http://192.168.254.100/index.htm, show I ' m/var/www/index.htm:

Indicates that the exact match takes precedence over the normal match.

"Example Step 6"

At this point, direct access to http://192.168.254.100/, that is, neither exact match nor ordinary match, the actual page accessed is/usr/local/nginx/html/index.html:

"Example Step 7"

Add a location configuration information (2nd), at which point the location configuration information is:

        Location =/index.htm {            root  /var/www/;            Index  index.htm index.html;        }                Location =/{            root   /var/www/;            Index  index.htm index.html;        }                        location/index.htm {            root   html;            Index  index.htm index.html;        }

When the http://192.168.254.100/is accessed, the 2nd exact match is hit, showing: I ' m/var/www/index.htm

Reference:

"Linux nginx config location syntax regular expression"

"Troubleshooting 403 Forbidden in Nginx server"

Nginx Notes and summaries (6) Location: Exact match

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.