Nginx & 详细举例 location -> index、return、rewrite、try_files、alias 各个属性的含义和注意事项

1、准备工作

1.1 本案例以 centos7 系统作为演示,配置文件在 /etc/nginx/conf.d/test.conf 中,内容如下

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';
  }
}

请务必记住好这几个配置,后面的案例都是以上面作为铺垫。

1.2 /home/cookcyq/web/ 目录如下:

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

1.3 在 /etc/nginx/nginx.conf 配置文件中将上面的文件引入进来(下面这段下载时就自动加上的,如果没有看到手动加即可)

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

1.4 重启 nginx ,访问 http://127.0.0.1/:8000/ 即可看到输出 Hello,world
在这里插入图片描述
接下来正式进入本文

2、location -> return

2.1 返回状态码 + 字符串

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

重启 nginx,访问 http://127.0.0.1:8000/a 输出:
在这里插入图片描述

2.2 重定向

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

重启 nginx,访问 http://127.0.0.1:8000/a 重定向到 https://www.baidu.com
在这里插入图片描述

3、location -> index

3.1 准备工作

/home/cookcyq/web/ 目录下新建 a 目录,并创建 index.html 和 test.html,结构如下

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

3.2 使用

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

现在访问 http://127.0.0.1:8000/a 时就会访问 /home/cookcyq/web/a/ 下的 index.html 文件
在这里插入图片描述

index 的作用就是当没有访问任何文件时,则默认访问 index.html

现在我们来指定一个,如 http://127.0.0.1:8000/a/test.html 就会访问 /home/cookcyq/web/a/ 下的 test.html 文件
在这里插入图片描述
如果我们指定一个不存在的文件比如 http://127.0.0.1:8000/a/ffff.html 则存在两种情况:

  1. 如果存在 , location / {} 则先访问
location / { 
	return "Hello,wolrd" 
}

在这里插入图片描述

  1. 如果不存在 location / { … } ,则访问前面所配置好的 error_page 404 /40x.html; 即 /home/cookcyq/web/40.html。
    在这里插入图片描述

4、location -> try_files

try_files 的细节蛮多的,稍不小心就掉进坑里,大家可要细心点哦。

4.1 准备工作

/home/cookcyq/web/ 目录下新建 b 目录结构如下

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

请务必记住好这些结构,下面将会使用以上结构作为示例。

4.2 使用

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

参数解释:

  • 参数一 $uri 表示访问 /b/path/anyfile
  • 参数二 $uri/ 表示访问 /b/path/ 时,访问该目录下的 index.xxx 索引文件
    注意:如果该目录下不存在任何 index.xxx 索引文件则返回 403,如果访问不存在的目录则返回 500。
  • 参数三 表示如果前面两个都找不到则访问 /b/index.html
    注意:如果 /b/index.html 也不存在,则返回 500

接下来我会一一演示如何触发这三个参数:
4.2.1 访问 http://127.0.0.1:8000/b/foo.html 则对应 $uri
4.2.2 访问 http://127.0.0.1:8000/b/b2/bird.html 则对应 $uri
在这里插入图片描述

4.2.3 访问 http://127.0.0.1:8000/b/ 则对应 $uri/
4.2.4 访问 http://127.0.0.1:8000/b/b2/ 则对应 $uri/
在这里插入图片描述

4.2.5 访问 http://127.0.0.1:8000/b/oooo.html 由于 oooo.html 不存在,则对应第三个参数 /b/index.html。
4.2.6 访问 http://127.0.0.1:8000/b/b2/oooo.html 由于 oooo.html 不存在,则对应第三个参数 /b/index.html。
在这里插入图片描述

4.2.7 访问 http://127.0.0.1:8000/b/oooo.htmlhttp://127.0.0.1:8000/b/b2/oooo.html 由于 oooo.html 不存在,且第三个参数 /b/index.html 也不存在的话(手动把index.html删掉或重命名即可实现效果),则返回 500
在这里插入图片描述

4.3 注意事项

  • 第三个参数推荐使用绝对路径的形式 /path/index.html ,请不要采用 $uri/index.html ,因为 $uri/ 是动态的目录,会随着 url 发生变化而变,这样很容易产生找不到目录而报 500 的问题
  • 第三个参数的 / 代表 root 根目录,但它并不会累加 location /b {} ,比如第三个参数为 /index.html 它对应的是 root/index.html 而不是 root/b/index.html,这就是为什么上面的第三个参数为 /b/index.html,而如果是 location /b { index /index.html } 则会累加 root/b/index.html 的形式,这点要注意。

5、location -> rewrite

rewrite 的细节也挺多的,不过理解起来很容易。

5.1 准备工作

/home/cookcyq/web/ 目录下新建 c 目录结构如下

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.

请务必记住好这些结构,下面将会使用以上结构作为示例。

5.2 使用

语法:rewrite [匹配模式] [重定向目标] [指令(可选)]

5.2.1 简单使用

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

当访问
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
都会重定向访问 root/r/1.html
在这里插入图片描述

记得把前面 location / { return 200 ‘Hello,world’ } 语句去掉,因为 return 指令会跳过下面的 location

5.2.2 带有 last 指令

location /c {
    rewrite ^(.*)$ /r/1.html last;
    # 不会触发这个,因为 last 会跳过下面的执行语句
    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
都会重定向访问 root/r/1.html
在这里插入图片描述

5.2.3 带有 last 指令

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;
}

当访问
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
都将重定向访问 root/r/3.html
在这里插入图片描述
5.2.4 带有 break 指令

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
都会重定向访问 root/r/1.html
在这里插入图片描述
5.2.5 带有 break 指令

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
都会重定向访问 root/r/1.html
在这里插入图片描述
5.2.6 容易犯错的重定向案例

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

思考一下,最终会重定向到哪里?

其实如果你知道 index 的作用,答案就简单多了,这里只会重定向到 root/r/1.html 而不会访问 root/r/2.html,因为 index 只是未访问任何文件时作为默认访问索引文件的指令。
在这里插入图片描述

5.3 注意事项

  • rewrite 重定向目标如果不存在则走进 error_page 404 /40x.html
  • 如果 root 是 /home/cookcyq/web 没有加 /,那么重定向目标时得加 /,反之 root 有加 / 则重定向目标可加可不加 /

6、location -> root 与 alias

假如我们不想让每个 location 都继承 root,而是拥有自己的 root,那么 location 内的 root 与 alias 指令就是很好的选择。

6.1 准备工作

目录结构

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;
}

当我们访问 http://127.0.0.1:8000/d/foo.html 就会访问 /home/cookcyq/my/d/foo.html
在这里插入图片描述

6.3 alias

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

当我们访问 http://127.0.0.1:8000/d/foo.html 就会访问 /home/cookcyq/my/d/foo.html
在这里插入图片描述

6.4 root 与 alias 的区别

root 指令最终形成的路径为:root+location xxx/*
alias 指令最终形成的路径为:alias/*

猜你喜欢

转载自blog.csdn.net/cookcyq__/article/details/122629541