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 inserts data successfully but reports [Err] 1055 error solution

1. Question: I have been doing insert operations ...

MySQL query learning basic query operations

Preface MySQL is the most popular relational data...

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

Nodejs converts JSON string into JSON object error solution

How to convert a JSON string into a JSON object? ...

Detailed explanation of nodejs built-in modules

Table of contents Overview 1. Path module 2. Unti...

WeChat applet canvas implements signature function

In the WeChat applet project, the development mod...

SQL to implement time series dislocation restoration case

Table of contents 1. Requirements description 2. ...

Using an image as a label, the for attribute does not work in IE

For example: Copy code The code is as follows: <...

Detailed explanation of common methods of JavaScript Array

Table of contents Methods that do not change the ...

jQuery realizes dynamic particle effect

This article shares the specific code of jQuery t...

JavaScript simulation calculator

This article shares the specific code of JavaScri...

How to hide the version number in Nginx

Nginx hides version number In a production enviro...