Detailed Analysis of the Differences between break and last in Nginx

Detailed Analysis of the Differences between break and last in Nginx

Let's talk about the difference first

  • last, the rewritten rule will continue to use the rewritten value to match the following location.
  • break, the rewritten rule will not match the following location. Using the new rules, a http request is directly initiated.

Nginx configuration file

server {
  listen 88;
  server_name _;
  location /break { # location 1
   rewrite ^/break/(.*)$ /bak/$1 break;
  }
  location /last { # location 2
   rewrite ^/last/(.*)$ /bak/$1 last;
  }

  location /bak { # location 3
   default_type text/html;
   return 200 $uri;
  }

}

Visit http://rumenz.com:88/break/one

When location1 is hit, the browser address bar does not change. It goes directly to find the /nginx/html/bak/one file. Since there is no such file, 404 is returned.

Browser

Nginx error (error.log) log

/nginx/html/bak/one failed (2: No such file or directory)

break means stop after rewriting and no longer match the location block.

Visit http://rumenz.com:88/last/one

Hit location2, the browser address bar does not change, re-match to location3

last means to jump to the location block after rewriting and match it with the rewritten address again

Use cases for break and last

break

File download, hide and protect the real file server.

location /down {
 rewrite ^/down/(.*)$ https://rumenz.com/file/$1 break;
}

last

Rewrite the interface address, rewrite https://rumenz.com/api/list to https://rumenz.com/newapi/list

location /api {
 rewrite ^/api/(.*)$ /newapi/$1 last;
}

location /newapi {
 default_type Application/json;
 return 200 '{"code":200,"msg":"ok","data":["JSON.IM","json format"]}';
}

Summarize

This is the end of this article about the difference between break and last in Nginx. For more information about the difference between break and last in Nginx, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Analysis of the difference between last and break when writing rewrite in Nginx configuration

<<:  Vue's detailed code for implementing the shuttle box function

>>:  How to distinguish MySQL's innodb_flush_log_at_trx_commit and sync_binlog

Recommend

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

Component design specifications for WeChat mini-program development

WeChat Mini Program Component Design Specificatio...

Webpack builds scaffolding to package TypeScript code

Create a folder Directory structure: dabaots Init...

CSS code for arranging photos in Moments

First, you can open Moments and observe several l...

Detailed explanation of eight methods to achieve CSS page bottom fixed

When we are writing a page, we often encounter a ...

HTML design pattern daily study notes

HTML Design Pattern Study Notes This week I mainl...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

Initial settings after installing Ubuntu 16 in the development environment

The office needs Ubuntu system as the Linux devel...

Flex layout achieves fixed number of rows per line + adaptive layout

This article introduces the flex layout to achiev...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

Solution to incomplete text display in el-tree

Table of contents Method 1: The simplest way to s...

mysql-8.0.17-winx64 deployment method

1. Download mysql-8.0.17-winx64 from the official...