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

New settings for text and fonts in CSS3

Text Shadow text-shadow: horizontal offset vertic...

JavaScript implements H5 gold coin function (example code)

Today I made a Spring Festival gold coin red enve...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

MySQL database implements MMM high availability cluster architecture

concept MMM (Master-Master replication manager fo...

How to set list style attributes in CSS (just read this article)

List style properties There are 2 types of lists ...

Docker mounts local directories and data volume container operations

1. Docker mounts the local directory Docker can s...

Tutorial on upgrading, installing and configuring supervisor on centos6.5

Supervisor Introduction Supervisor is a client/se...

A QQ chat room based on vue.js

Table of contents Introduction The following is a...

A brief discussion on the role of Vue3 defineComponent

Table of contents defineComponent overload functi...