Nginx Notes and summaries (7) Location: regular match

Source: Internet
Author: User

In the default server segment of/usr/local/nginx/conf/nginx.conf, leave the default location information (previously tested location configuration removed):

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

 

Create the image directory under/var/www:

[Email protected] ~]# cd/var/www[[email protected] www]# mkdir image

Download or transfer a picture in this directory using wget or ftp: test.jpg

Modify/usr/local/nginx/html/index.html, plus

[Email protected] nginx]# vim html/index.html <! DOCTYPE html>

Smooth restart Nginx.

Because there is no image directory under the/usr/local/nginx/html/directory, when you access 192.168.254.100, the pictures on the page cannot be displayed:

  

Direct access to the image address:

At this point, a regular matching location configuration (2nd) is added under the location configuration, at which point the location is configured as:

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

Smooth restart Nginx.

To access the http://192.168.254.100/image/test.jpg, the regular match and the generic match are matched to the URI at the time of the precedence match:

The picture in the/var/www/image directory is displayed.

Direct access to Http://192.168.254.100/image/test.jpg:

The matching process is: the image in the regular match matches the/image/test.jpg in the URI, then the regular match works, and the real access is/var/www/images/test.jpg

Again, when more than one common match can be matched to a URI, which match can really work (compare 1th and 2nd)?

/usr/local/nginx/conf/nginx.conf:

        Location/{            root   html;            Index  index.html index.htm;        }        Location/foo {            root   /var/www;            Index  index.htm index.html;        }        Location ~ image {            root/var/www/;            Index index.html;        }

At this time the/var/www directory has foo directory, below the file Index.htm:i ' m/var/www/index.htm

For the URI "/foo", the two location of the Patt can match, that "/" and "/foo" can be left prefix matching "/foo",

Visit Http://192.168.254.100/foo:

  

That

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

Play a role because "/foo" is longer, so the location of "/foo" is used to match.

    

Nginx notes and summaries (7) Location: regular 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.