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

How to query date and time in mysql

Preface: In project development, some business ta...

Sample code for nginx to achieve dynamic and static separation

1. Simple configuration of nginx's dynamic an...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

How to modify the master-slave replication options in MySQL online

Preface: The most commonly used architecture of M...

XHTML Getting Started Tutorial: Form Tags

<br />Forms are an important channel for use...

Gearman + MySQL to achieve persistence operation example

This article uses the gearman+mysql method to imp...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

How to install jupyter in docker on centos and open ports

Table of contents Install jupyter Docker port map...

MySQL DML language operation example

Additional explanation, foreign keys: Do not use ...

WeChat applet implements a simple calculator

A simple calculator written in WeChat applet for ...

Three ways to check whether a port is open in a remote Linux system

This is a very important topic, not only for Linu...

MySql inserts data successfully but reports [Err] 1055 error solution

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

Pure CSS to achieve cool neon light effect (with demo)

I have recently been following the CSS Animation ...