A simple and sufficient explanation of nginx location configuration

Time:2022-5-23

preface

Location is a very core configuration in nginx. This article focuses on the configuration of location and some precautions.

grammar

For location, take a simple configuration example:

http { 
  server {
      listen 80;
        server_name www.yayujs.com;
        location / {
          root /home/www/ts/;
          index index.html;
        }
  }
}

Roughly, when you visitwww.yayujs.comof80Port, return/home/www/ts/index.htmlDocuments.

Let’s look at the specific syntax of location:

location [ = | ~ | ~* | ^~ ] uri { ... }

Focus on the in square brackets[ = | ~ | ~* | ^~ ], where|The separated content indicates the syntax you may use, where:

  • =Indicates an exact match, for example:
location = /test {
  return 200 "hello";
}

# /test ok
# /test/ not ok
# /test2 not ok
# /test/2 not ok
  • ~Represents a case sensitive regular match, such as:
location ~ ^/test$ {
  [ configuration ] 
}

# /test ok
# /Test not ok
# /test/ not ok
# /test2 not ok
  • ~*Represents a case insensitive regular match
location ~* ^/test$ {     
    [ configuration ] 
}

# /test ok
# /Test ok
# /test/ not ok
# /test2 not ok
  • ^~Indicates that the URI starts with a string
location ^~ /images/ {    
    [ configuration ] 
}

# /images/1.gif ok

When you don’t use these grammars and only write URI:

/Indicates a universal match:

location / {     
    [ configuration ] 
}

# /index.html ok
location /test {
    [ configuration ] 
}

# /test ok
# /test2 ok
# /test/ ok

Matching order

When there are multiple locations, their matching order is referencedlocation /i/ {
alias /data/w3/images/;
}

When requested/i/top.gif/data/w3/images/top.gifWill be returned.

At first glance, the two are very similar, but when you take a closer look, you can see the difference between the two. Root is direct splicingroot + locationAnd alias usesaliasreplacelocation, so there are in the last path in root/i/, but not in the last path in alias/i/

So if you use allias to define a path like this:

location /images/ {
    alias /data/w3/images/;
}

In fact, using root is better:

location /images/ {
    root /data/w3;
}

Root in server and location

Root can be used in both server and location. For example:

http { 
  server {
      listen 80;
        server_name www.yayujs.com;
        root /home/www/website/;
        location / {
          root /home/www/ts/;
          index index.html;
        }
  }
}

If both appear, what is the priority?

In short, it is the proximity principle. If the location can be matched, the root configuration in the location is used, and the root configuration in the server is ignored. When the location cannot be matched, the root configuration in the server is used.

Series articles

Blog building series is the only practical series of tutorials I have written so far. It explains how to use vuepress to build a blog and deploy it to GitHub, gitee, personal server and other platforms.

  1. One takes you to build a blog with vuepress + GitHub pages
  2. An article teaches you to synchronize GitHub and gitee
  3. Can’t use GitHub actions yet? Look at this one
  4. How does gitee automatically deploy pages? Still use GitHub actions!
  5. A Linux command with sufficient front end

Wechat: “mqyqingfeng”, add me to Yu Yu’s only reader group.

If there is any mistake or lack of preciseness, please be sure to correct it. Thank you very much. If you like it or have some inspiration, welcome star, which is also an encouragement to the author.