Configuration method of location in Nginx

Source: Internet
Author: User
This article mainly introduces the location of Nginx configuration method, has a certain reference value, now share to everyone, have the need for friends can refer to

今天有一位同学问到 Nginx 的站点多路径匹配的问题?

1.www.domain.com/aNeed to return/var/www/domain.com/a/index.html
2. www.domain.com/b need to return/var/www/domain.com/b/index.html
How do I configure the Nginx to take effect?

To solve this problem, the first reflection is to directly use the Nginx location command to solve, but before giving the answer, we first to understand the Nginx location instruction base.

Nginx Block Configuration Concept

In Nginx's configuration file, it is usually set up with two commonly used blocks (block):

1.Server Block
2.Localtion Block

The block here refers to the block, which you can even understand as the configuration between the back of the pair {} .

Sever block is mainly the real host configuration, such as the configuration of the host domain name, IP, port and other content. Of course, in an Nginx configuration file, we can specify the configuration of multiple Sever chunks.

The location block, in the Sever block, is subdivided into configurations for different paths and requests. Because there are usually a lot of URIs in a site, you can also write multiple location configurations in the Location block Settings section.

Let's look at the basic syntax for location configuration first:

Location Optional_modifier Location_match {# This {} config content is a block block}

The above optional_modifier configuration items are available with regular expressions. Some of the following are commonly used:

    1. 留空。 Yes, leaving it blank is also a way to set it. In the case of leave blank, the configuration represents the request path location_match starting from.

    2. =, the equals sign is still very easy to understand: The request path is exactly equal to the value behind it, and the location_match first item is left blank or different.

    3. ~, float (note that the floating number entered in English) indicates a case-sensitive regular match.

    4. ~*Represents a regular match that is case insensitive.

    5. ^~Indicates that there is no need for a regular match to occur.

Nginx processing the order of location blocks

The basic concepts and common configuration of the location directive are understood above. Let's take a look at the order in which the location takes effect! This is also important:

Every request comes in Nginx, the Nginx will choose a location of the best match to respond, the process of the specific procedure is to compare with location configuration, this step can be divided into the following steps:

    1. The first 前缀式 match (that is, the location's optional_modifier is an empty configuration).

    2. Nginx will then look for the exact location configuration (that is, location's optional_modifier configuration) based on the URI = .

    3. If there is no match, the configuration is matched first, and ^~ if a configuration is found, the search process is stopped and the response content is returned directly.

    4. If no match is found, the case-sensitive regular match is first followed, and then the size-insensitive regular match.

Some examples of Nginx location configuration:

More useless, see so many theories, no concrete examples of support is useless, so let's look at specific configuration examples:

Location  =/{  # = equals sign, match only/this route}
location/data {   # empty configuration will match the route with/data start, and subsequent matches will match down. }
Location ^~/img/{  # Note ^~ configuration, where the match to the beginning of the/img/, will return directly. }
Location ~*. (png|gif|ico|jpg|jpeg) $ {  # matches requests ending with PNG, GIF, ICO, JPG or JPEG; this is typically used to set a request response for a picture. }

Very practical two examples:

1. Simple picture anti-theft chain

Location ~. (png|gif|jpe?g) $ {  Valid_referers none blocked yourwebsite.com *.yourwebsite.com;  # Note that it's good to write your domain name    if ($invalid _referer) {      return   403;  }}

2. For some writable paths, no php or js step execution

Location ~*/(Media|images|cache|tmp|logs)/.*. (Php|jsp|pl|py|asp|cgi|sh) $ {  return 403;}

The answer to the question

Finally, we look at the answer to the question, which can look something like this:

location/a {   root/var/www/domain.com/a;} location/b {   root/var/www/domain.com/b;}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.