Nginx & detailed example location -> index, return, rewrite, try_files, alias The meaning and precautions of each attribute

1. Preparation

1.1 In this case, the centos7 system is used as a demonstration, and the configuration file is /etc/nginx/conf.d/test.confin , the content is as follows

server {
  listen  8000;
  server_name           127.0.0.1;
  root                  /home/cookcyq/web/;
  error_page  404              /40x.html;
  error_page  500 502 503 504  /50x.html;
  
  location / {
    return 200 'Hello,world';
  }
}

Please be sure to remember these configurations, the following cases are based on the above as a foreshadowing.

1.2 /home/cookcyq/web/The directory is as follows:

home
	cookcyq
		web
			40x.html 内容为:40xxxxxxxx.html
			50x.html 内容为:50xxxxxxxx.html
			index.html 内容为: indexxxxxx.html

1.3 Introduce the above files in /etc/nginx/nginx.confthe configuration file (the following section will be automatically added when downloading, if you don’t see it, you can add it manually)

http{
	...
	include /etc/nginx/conf.d/*.conf;
}

1.4 Restart nginx, visit http://127.0.0.1/:8000/to see the output Hello, world
insert image description here
and then officially enter this article

2、location -> return

2.1 return status code + string

location /a {
	return 200 'Hi, I am a.';
}

Restart nginx, access http://127.0.0.1:8000/athe output :
insert image description here

2.2 Redirection

location /a {
  return https://www.baidu.com;
 }

Restart nginx, visit http://127.0.0.1:8000/aRedirect to https://www.baidu.com
insert image description here

3、location -> index

3.1 Preparations

Create a new directory under the /home/cookcyq/web/directory , and create index.html and test.html, the structure is as follows

home
	cookcyq
		web
			a
				index.html  内容为: Hello, index.html.
				test.html 	  内容为:Hello,test.html.

3.2 use

root                  /home/cookcyq/web/;
....
location /a {
    index index.html;
 }

http://127.0.0.1:8000/aWhen you visit now, you will visit the files/home/cookcyq/web/a/ underindex.html
insert image description here

The function of index is to access index.html by default when no files are accessed

Now let's specify one, such as http://127.0.0.1:8000/a/test.htmlwill access the file/home/cookcyq/web/a/ under If we specify a file that does not exist, such as , there are two situations:test.html
insert image description here
http://127.0.0.1:8000/a/ffff.html

  1. If present, location / {} is accessed first
location / { 
	return "Hello,wolrd" 
}

insert image description here

  1. If there is no location / { … }, visit the previously configured error_page 404 /40x.html; ie /home/cookcyq/web/40.html.
    insert image description here

4、location -> try_files

There are quite a lot of details in try_files, if you accidentally fall into the pit, please be careful.

4.1 Preparations

/home/cookcyq/web/Create a new b directory under the directory structure as follows

home
	cookcyq
		web
			b
				foo.html  		内容为: Hello, foo.html.
				bar.html	  	内容为:Hello,bar.html.
				index.html		内容为:Hello,b1, index.html.
				b2
					bird.html 		内容为:Hello,Bird.html
					index.html   	内容为:Hello,b2, index.html

Be sure to keep these structures in mind, the following will use the above structure as an example.

4.2 use

location /b {
	try_files $uri $uri/ /b/index.html;
}

Parameter explanation:

  • Parameter one $uri indicates access to /b/path/anyfile
  • The second parameter $uri/ indicates that when accessing /b/path/, access the index.xxx index file in this directory.
    Note: If there is no index.xxx index file in this directory, it will return 403, and if it accesses a directory that does not exist, then returns 500.
  • Parameter three means that if the previous two cannot be found, visit /b/index.html
    Note: If /b/index.html does not exist, return 500

Next, I will demonstrate how to trigger these three parameters one by one:
4.2.1 http://127.0.0.1:8000/b/foo.htmlAccess corresponds to $uri
4.2.2 http://127.0.0.1:8000/b/b2/bird.htmlAccess corresponds to $uri
insert image description here

4.2.3 http://127.0.0.1:8000/b/Access corresponds to $uri/
4.2.4 http://127.0.0.1:8000/b/b2/Access corresponds to $uri/
insert image description here

4.2.5 Access http://127.0.0.1:8000/b/oooo.htmlSince oooo.html does not exist, it corresponds to the third parameter /b/index.html.
4.2.6 Access http://127.0.0.1:8000/b/b2/oooo.htmlSince oooo.html does not exist, it corresponds to the third parameter /b/index.html.
insert image description here

4.2.7 Access http://127.0.0.1:8000/b/oooo.htmlor http://127.0.0.1:8000/b/b2/oooo.htmlSince oooo.html does not exist, and the third parameter /b/index.htmldoes not exist (manually delete or rename index.html to achieve the effect), return 500
insert image description here

4.3 Precautions

  • The third parameter is recommended to use the form of an absolute path /path/index.html, please do not use it $uri/index.html, because $uri/it is a dynamic directory, it will change with the change of the url, so it is easy to cause the problem that the directory cannot be found and report 500
  • The third parameter /represents the root root directory, but it does not add location /b {}, for example, the third parameter /index.htmlcorresponds root/index.html to not root/b/index.html, which is why the third parameter above is /b/index.html, and if it is location /b { index /index.html } will accumulate in the form root/b/index.htmlof , which should be noted.

5、location -> rewrite

The details of rewrite are quite a lot, but it is easy to understand.

5.1 Preparations

/home/cookcyq/web/Create a new c directory under the directory structure as follows

home
	cookcyq
		web
			c
				c.html  	内容为:Hello, c.html.
			r
				1.html  	内容为:Hello, 1.html.
				2.html	  	内容为:Hello, 2.html.
				3.html		内容为:Hello, 3.html.

Be sure to keep these structures in mind, the following will use the above structure as an example.

5.2 use

Syntax: rewrite [matching pattern] [redirect target] [command (optional)]

5.2.1 Easy to use

location /c {
	rewrite ^(.*)$ /r/1.html;
}

When you visit,
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
you will be redirected to visit root/r/1.html
insert image description here

Remember to remove the previous location / { return 200 'Hello, world' } statement, because the return command will skip the following location

5.2.2 With last directive

location /c {
    rewrite ^(.*)$ /r/1.html last;
    # 不会触发这个,因为 last 会跳过下面的执行语句
    rewrite ^(.*)$ /r/2.html last; 
}

When you visit,
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
you will be redirected to visit root/r/1.html
insert image description here

5.2.3 With last directive

location /c {
    rewrite ^(.*)$ /r/1.html last;
    # 不会触发这个,因为 last 会跳过下面的执行语句,但它会继续走进下一个 location 块
    rewrite ^(.*)$ /r/2.html last; 
}
# 这里监听 /r/1.html 再做一次重定向
location = /r/1.html {
	rewrite ^(.*)$ /r/3.html last;
}

When accessing
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
will be redirected to visit root/r/3.html
insert image description here
5.2.4 with break instruction

location /c {
  rewrite ^(.*)$ /r/1.html break;
  # 不会触发这个,因为 break 会跳过下面的执行语句
  rewrite ^(.*)$ /r/2.html last; 
}

http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
Will redirect access to root/r/1.html
insert image description here
5.2.5 with break instruction

location /c {
    rewrite ^(.*)$ /r/1.html break;
    # 不会触发这个,因为 break 会跳过下面的执行语句,同时也会跳过下面的 location 块
    rewrite ^(.*)$ /r/2.html last; 
  }
  # 由于 break 这里不会触发到
  location = /r/1.html {
    rewrite ^(.*)$ /r/3.html last;
  }

http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
Will redirect access to root/r/1.html
insert image description here
5.2.6 Error-prone redirection cases

location /c {
  rewrite ^(.*)$ /r/1.html last;
}
location = /r/1.html {
  index /r/2.html;
}

Think about it, where does it end up being redirected to?

In fact, if you know the function of index, the answer is much simpler. Here, only root/r/1.html will be redirected and root/r/2.html will not be accessed, because index is only used as the default access when no files are accessed. Directive for indexing files.
insert image description here

5.3 Precautions

  • If the rewrite redirect target does not exist, it will go to error_page 404 /40x.html
  • If the root is /home/cookcyq/web and it is not added /, then the redirection target must be added /, /otherwise the redirection target can be added or not/

6、location -> root 与 alias

If we don't want each location to inherit root, but have its own root, then the root and alias commands in location are a good choice.

6.1 Preparations

Directory Structure

home
	cookcyq
		web
			d
				1.html 			内容为 Hello, 1.html
				index.html 		内容为 Hello, web index.html
		my
			d
				foo.html 		内容为 Hello, foo.html.
				index.html 		内容为 Hello, my index.html.

6.2 root

root /home/cookcyq/web/;
location /d {
	root /home/cookcyq/my/;
	try_files $uri $uri/ /d/index.html;
}

when we visit http://127.0.0.1:8000/d/foo.htmlwe will visit/home/cookcyq/my/d/foo.html
insert image description here

6.3 alias

root /home/cookcyq/web/;
location /d {
	alias /home/cookcyq/my/d/;
	try_files $uri $uri/ /d/index.html;
}

when we visit http://127.0.0.1:8000/d/foo.htmlwe will visit/home/cookcyq/my/d/foo.html
insert image description here

6.4 The difference between root and alias

The final path formed by the root command is: root+location xxx/*
The final path formed by the alias command is:alias/*

Guess you like

Origin blog.csdn.net/cookcyq__/article/details/122629541