Nginx Location Configuration Tutorial from Scratch

Nginx Location Configuration Tutorial from Scratch

Basics

The matching order of location is "match regular expression first, then match common expression".

The location matching order is actually "match common first, then match regular". The reason for the misunderstanding is that regular matching will override ordinary matching

Nginx location configuration syntax

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

2. location @name { ... }

There are two ways to configure location configuration

1. prefix + uri (string/regular expression)

2.@ + name

Prefix meaning

= : Exact match (must all be equal)

~ : case sensitive

~* : Ignore case

^~ : only match the uri part

@: Internal service jump

Location Basics

1.location is configured in the server block.

2. You can use different configurations (configured in location) according to different URIs to handle different requests.

3. Locations are ordered and will be processed by the first matching location.

Location configuration demo

1.=, exact match

  location = / {
   #rule}
  # It matches requests like `http://www.example.com/`.

2.~, case sensitive

  location ~ /Example/ {
    #rule}
  #Request example#http://www.example.com/Example/ [Success]
  #http://www.example.com/example/ [FAIL]

3.~*, case-insensitive

 location ~* /Example/ {
    #rule}
 # The case of the uri part will be ignored #http://www.example.com/Example/ [Success]
 #http://www.example.com/example/ [Success]

4.^~, only matches the string starting with uri

 location ^~ /img/ {
   #rule}
 #Requests starting with /img/ will match #upload/2022/web/a.jpg [Success]
 #http://www.example.com/img/b.mp4 [Success]

5.@, nginx internal jump

 location /img/ {
  error_page 404 @img_err;
 }
 
 location @img_err {
  # rule}
 #For requests starting with /img/, if the link status is 404. It will match the @img_err rule.

Summarize

The location in Nginx is not as difficult to understand as you might think, so don't be afraid. Find more information and try more. You will gain something.

refer to

  • http://nginx.org/en/docs/http/ngx_http_core_module.html#location
  • Uniform Resource Identifier

Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Complete steps for deploying a front-end and back-end separated nginx configuration
  • Detailed tutorial on how to deploy Springboot project using Nginx on the server (jar package)
  • Detailed explanation of Nginx server setup and basic configuration
  • Detailed tutorial on how to start nginx configuration service
  • Even a novice can complete the deployment of Nginx service with zero foundation

<<:  Encapsulation implementation of the data format returned by nestjs to the front end

>>:  Explain MySQL's binlog log and how to use binlog log to recover data

Recommend

How to start and restart nginx in Linux

Nginx (engine x) is a high-performance HTTP and r...

Detailed explanation of SSH password-free login configuration under Linux

Assume there are two Linux servers A and B, and w...

DD DT DL tag usage examples

We usually use the <ul><li> tags, but ...

JavaScript built-in date and time formatting time example code

1. Basic knowledge (methods of date objects) 😜 ge...

H tags should be used reasonably in web page production

HTML tags have special tags to handle the title of...

Detailed explanation of Vue3 life cycle functions and methods

1. Overview The so-called life cycle function is ...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

CocosCreator implements skill cooling effect

CocosCreator realizes skill CD effect There are s...

jQuery Ajax chatbot implementation case study

Chatbots can save a lot of manual work and can be...

Let's talk about MySQL joint query in detail

Table of contents Union query 1. Query the ID and...

js to achieve the effect of light switch

This article example shares the specific code of ...

How to create a virtual environment using virtualenv under Windows (two ways)

Operating system: windowns10_x64 Python version: ...

MySQL cursor detailed introduction

Table of contents 1. What is a cursor? 2. How to ...

MySQL 8.0.13 installation and configuration method graphic tutorial

This article shares the installation and configur...