precontent阶段
static模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
root 和 alias
功能: 都是url映射为文件路径以返回静态文件内容
差异: 与location的配合, alias 只能用在location中,root可在个个地方出现, 且会将完整url映射进文件路径中, alias不会将location后的url映射到文件路径
root path;
default html;
可以出现在: http, server, location, if in location
alias path;
default
可以出现在: location
eg:
server {
listen 15636;
root /var/www/Photos;
error_log logs/root_alias_error.log info;
location /root {
root html;
}
location /alias {
alias html;
}
location ~/root/(\w+\.txt) {
root html/first/$1;
}
location ~/alias/(\w+\.txt) {
alias html/first/$1;
}
}
生成待访问文件的三个相关变量:
request_filename: 待访问文件的完整路径
document_root: 由 url 和 root/alias 规则生成的文件夹路径, 其中可能包含软连接,
realpath_root: 将 document_root 中的软连接等换成真实的路径
location /realpath/ {
alias html/realpath/
return 200 '${request_filename}|${document_root}|${realpath_root}\n';
}
realpath: 是一个软连接文件夹, realpath_root会返回realpath软连接指向的文件夹
问题: 访问目录时url最后没有带 "/" ?nginx是怎么处理的
static 模块实现root/alias 功能后,访问目标是目录,url末尾未加 / , 会返回301重定向, location会在末尾加 /.
重定向之后跳转域名的指定: 有三个指令
absolute_redirect on | off; # 返回301重定向是nginx自动发生的, 绝对路径会打开, 填写域名然后将url放到后面
Default: absolute_redirect on;
可以用出现在: http server location
server_name_in_redirect on | off; # absolute_redirect 默认开启域名, 但是返回的是server_name指定的域名, 还是访问的url的域名(地址), 默认是返回访问url时的域名.
Default: server_name_in_redirect off;
可以用出现在: http server location
port_in_redirect on | off; # 显示端口号
Default: port_in_redirect on;
可以用出现在: http server location
在 root/alias 文件访问配置基础之上加:
server {
listen 15636;
server_name www.fscloude.cn;
server_name_in_redirect on;
port_-n_redirect on;
absolute_redirect off;
}
1. 屏蔽 server_name_in_redirect on; absolute_redirect off;
默认不返回 server_name 设置的域名
访问: curl http://localhost:15636/html -I
返回: Location: http://localhost:15636/html/ # absolute_redirect 默认开启, 显示域名, server_name_in_redirect 默认 off, 不返回 server_name 指向域名
2. 开放 server_name_in_redirect on;
屏蔽 absolute_redirect off;
访问: curl http://localhost:15636/html -I
返回: Location: http://www.fscloude.cn:15636/html/ # 因为 server_name_in_redirect on; 域名指向 server_name
3. 屏蔽 server_name_in_redirect on;
开放 absolute_redirect off;
访问: curl http://localhost:15636/html -I
返回: Location: /html/ # 因为 absolute_redirect off; 不返回域名
4. 开放 server_name_in_redirect on;
开放 absolute_redirect off;
访问: curl http://localhost:15636/html -I
返回: Location: /html/ # 因为默认不返回域名
本文由作者按照
CC BY 4.0
进行授权