How to implement interception of URI in nginx location

How to implement interception of URI in nginx location

illustrate:

Root and alias in location

  • The root directive simply sets the search root to the directory set by root, that is, it does not truncate the URI, but uses the original URI to jump to the directory to find the file
  • The aias directive will truncate the matching URI and then use the path set by alias plus the remaining URI as a subpath for searching

The uri of the proxy_pass in location

If the proxy_pass url does not contain uri

  • If the trailing character is "/", the matching URI will be truncated.
  • If the trailing character is not "/", the matching URI will not be truncated.

If the proxy_pass url has a uri, the matching uri will be truncated

Examples

Root in location

root@pts/1 $ ls -ld /data/web/lctest*|awk '{print $NF}'
/data/web/lctest
/data/web/lctest2
/data/web/lctest3
/data/web/lctest4


location /lctest {
  root /data/web/;
}

location /lctest2/ {
  root /data/web/;
}
location /lctest3 {
  root /data/web;
}
location /lctest4/ {
  root /data/web;
}

The curl test results are as follows

Note: If you don't add a / at the end when you enter it in the browser, it will be automatically added, but curl will not

root@pts/1 $ curl http://tapi.xxxx.com/lctest/
hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest2/
hello world
2

root@pts/1 $ curl http://tapi.xxxx.com/lctest3/
3
hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest4/
hello world
4

location alias

location /lctest5 {
  alias /data/web/;
}
location /lctest6/ {
  alias /data/web/;
}

location /lctest7 {
  alias /data/web;
}

## 403 /data/web forbidden
location /lctest8/ {
  alias /data/web;
}

The curl test results are as follows

curl 'http://tapi.kaishustory.com/lctest5/'
curl 'http://tapi.kaishustory.com/lctest6/'
curl 'http://tapi.kaishustory.com/lctest7/'
The results are all /data/web/index.html output root@pts/1 $ curl 'http://tapi.kaishustory.com/lctest8/'
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

location proxy_pass

#--------proxy_pass configuration---------------------
location /t1/ { proxy_pass http://servers; } #Normal, not truncated location /t2/ { proxy_pass http://servers/; } #Normal, truncation location /t3 { proxy_pass http://servers; } #Normal, not truncated location /t4 { proxy_pass http://servers/; } #Normal, truncation location /t5/ { proxy_pass http://servers/test/; } #Normal, truncation location /t6/ { proxy_pass http://servers/test; } #Missing "/", truncation location /t7 { proxy_pass http://servers/test/; } #Contains "//", truncation location /t8 { proxy_pass http://servers/test; } #Normal, truncation

Test Scripts

for i in $(seq 8)
do
  url=http://tapi.xxxx.com/t$i/doc/index.html
  echo "----------$url-----------"
  curl url
done

Test Results

----------http://tapi.xxxx.com/t1/doc/index.html------------
/t1/doc/index.html

----------http://tapi.xxxx.com/t2/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t3/doc/index.html------------
/t3/doc/index.html

----------http://tapi.xxxx.com/t4/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t5/doc/index.html------------
/test/doc/index.html

----------http://tapi.xxxx.com/t6/doc/index.html------------
/testdoc/index.html

----------http://tapi.xxxx.com/t7/doc/index.html------------
/test//doc/index.html

----------http://tapi.xxxx.com/t8/doc/index.html------------
/test/doc/index.html

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Nginx configuration directive location matcher priority and security issues
  • Detailed explanation of location configuration in Nginx server
  • Detailed explanation of Nginx location matching rules
  • Analysis of some basic points of location configuration in Nginx server
  • Detailed explanation of the location directive matching rules of the Nginx server
  • Detailed explanation of Nginx Location configuration
  • A brief guide to the Nginx Location directive
  • Introduction to location matching rules in Nginx
  • nginx configuration location summary location regular writing and rewrite rule writing
  • Nginx location matching rule example

<<:  mysql workbench installation and configuration tutorial under centOS

>>:  JavaScript type detection method example tutorial

Recommend

5 MySQL GUI tools recommended to help you with database management

There are many database management tools for MySQ...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

MySQL uses aggregate functions to query a single table

Aggregate functions Acts on a set of data and ret...

How to use node scaffolding to build a server to implement token verification

content Use scaffolding to quickly build a node p...

Complete steps to install MySQL 8.0.x on Linux

MySQL Introduction to MySQL MySQL was originally ...

Detailed explanation of the principle of Vue monitoring data

Table of contents 1. Introduction II. Monitoring ...

Vue implements a simple shopping cart example

This article shares the specific code of Vue to i...

Floating menu, can achieve up and down scrolling effect

The code can be further streamlined, but due to t...

How to deploy springcloud project with Docker

Table of contents Docker image download Start mys...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

How to use regular expression query in MySql

Regular expressions are often used to search and ...

8 commands to effectively manage processes in Linux

Preface The role of process management: Determine...