content阶段中一个常用模块:static模块,这个模块提供的root、alias是我们常用的两个指令。这个模块是nginx框架中的,我们不能进行移除。
一、root和alias指令
root和alias的作用都是用于将url映射为文件路径,返回文件内容。
1、案例
location /root {
root html;
}
location /alias {
alias html;
}
使用 curl http://localhost:8000/alias/1.txt,返回test,1.txt文件中的内容就是test。最终访问的路径是/html/1.txt。
使用 curl http://localhost:8000/root/1.txt,返回访问错误。最终访问的路径是/html/root/1.txt。
**2、**区别
root会将location后面的字段拼接到请求url中,alias不会。
3、注意
如果配置为:
location /aslias/ {
alias /usr/html;
}
访问/alias/web/index.html时,最终访问的路径是/usr/htmlweb/index.html。
如果配置为:
location /aslias/ {
alias /usr/html/;
}
访问/alias/web/index.html时,最终访问的路径是/usr/html/web/index.html。
二、static模块提供的变量
static模块提供了3个变量:
- request_filename:访问文件的完整目录。
- document_root:有URL和root/alias生成的文件夹目录。
- realpath_root:将document_root中的软连接换成真实的路径。
1、案例
进入nginx-demo项目中,使用:
ln -s ./html first
创建一个指向nginx-demo/html文件的软连接first,使用ls -il查看可以看到first和html的关系如下:
nginx-demo的nginx.conf文件中增加:
location /root { alias first; # return 200 '$request_filename \n $document_root \n $realpath_root'; }
使用http://localhost:8000/root/index.html,可以访问到,访问显示的页面是html/index.html文件中的内容,说明软连接创建成功。
打开注释,再次请求http://localhost:8000/root/index.html,返回的内容如下:
解释:
-
$request_filename值为../nginx-demo/first/index.html。
-
$document_root值为../nginx-demo/first。
-
$realpath_root值为/Users/...../Nginx/nginx-demo/html。
三、log_not_found指令
该指令的值为on或者off,存在的上下文为http、server、location,默认为on,当为on时,我们访问某个url,对应的文件没有找到时,这条请求会被添加到log日志中;如果为off,则不会被添加到日志中。
四、index和auto_index
1、index模块
index模块中的index指令是ngx_http_index_module模块中的,不可以禁用,但是可以修改它的值,它默认的值为index.html,当我们访问http://localhost:8000/时,会查找nginx中的html/index.html文件,我们可以使用:
index html/test.html
进行修改。该指令存在的上下文是http、location、server。
index模块的执行阶段是在auto_index之前的,所以当访问http://localhost:8000/时,如果找到了index.html文件,就会返回文件内容,后面的auto\_index得不到执行。
2、auto_index模块
auto_index模块的作用是当url以/结尾时,将尝试以html/xml/json/jsonp等格式返回root/aias中指向目录的目录结构。
该模块属于ngx_http_autoindex_module模块,默认是编译进nginx中的,可以使用--width-http_autoindex_module取消。
提供了4个指令:
- autoindex:值为on/off,默认为off,作用是开启或者关闭返回目录的功能。
- autoindex_exact_size:值为on/off,默认为on,当autoindex_format设置时才会起作用,作用是返回的目录是相对路径还是绝对路径。
- autoindex_format:值为html/xml/json/jsonp,默认为html,作用是确定返回的目录的格式。
- autoindex_localtime:值为on/off,默认为off,作用是时间是否使用本地时间格式。
3、案例
location / {
alias html/;
autoindex on;
}
显示的还是index.html的内容。
改成这样:
location / {
alias html/;
autoindex on;
index a.html;
}
显示如下:
再改成这样:
location / {
alias html/;
autoindex on;
index a.html;
autoindex_exact_size off;
}
显示的还是上图这样
再改成这样:
location / {
alias html/;
autoindex on;
index a.html;
autoindex_exact_size off;
autoindex_format json;}
显示这样:
再改成这样:
location / {
alias html/;
autoindex on;
index a.html;
autoindex_exact_size off;
autoindex_format json;
autoindex_localtime on;}
显示结果同上图。
五、concat模块
concat模块是由阿里巴巴提供的,这个模块是够再一次请求中返回多个文件的内容,提升性能。
使用git clone git://github.com/alibaba/nginx-http-concat.git进行下载,这里我安装到nginx-demo、nginx-1.20.1-2的同级。
nginx-demo为nginx-1.20.1-2编译后的项目,在nginx-1.20.1-2中执行./configure --prefix=../nginx-demo --add-module=../nginx-http-concat,再执行make,执行完之后用nginx-1.20.1-2/objs/nginx替换nginx-demo/sbin/nginx,最后重启nginx-demo项目即可。
1、指令
指令有:
-
concat:值为on/off,默认为off,启用还是禁用。
-
concat_delimiter:值为string,返回给前端的多个文件的内容的分割符,比如这里设置:::,请求的文件为1.txt和2.txt,那么返回的响应中两个文件的内容用:::分割。
-
concat_types:值为文件类型,默认值为text/css application/x-javascript,指明对哪些文件进行合并。
-
concat_unique:值为on/off,默认值为on,指明是对一种文件类型还是多种文件类型进行合并。
-
concat_ignore_file_error:值为on/off,默认为off,指明在请求多个文件时,有一个文件不存在或者错误时,继续返回其它文件。
-
concat_max_files:值为一个数值,默认为10,指明最多可以合并多少个文件。
2、案例
server {
concat on;
location /concat {
concat on;
concat_delimiter ':::';
concat_ignore_file_error on;
concat_max_files 10;
concat_types text/plain;
concat_unique on;
}
}
六、补充
1、请求的url为目录并且最后不带/时,即curl localhost:8000/root -I时,返回的Location中的内容是什么?
当我们访问的url为目录时,nginx会返回一个301的重定向。
相关指令:
-
absolute_redirect:Location中是否带有协议、域名、端口,默认为on,带有协议、域名、端口。
-
server_name_in_redirect:Location中带着的域名为Host域名还是server_name配置的主域名。默认为off。
-
port_in_redirect:端口是否显示在Location中。默认为on。
设置了absolute_redirect时:
- 当设置为off时,返回内容如下:
- 当设置为on时(默认),返回内容如下:
总结:absolute_redirect为off时,Location中不会带协议、域名、端口;为on时,会带,但带的是curl时带的域名。
设置了server_name_in_redirect时(absolute_redirect为on):
- 当设置为off时(默认),返回内容如下:
- 当设置为on时,返回内容如下:
test-fxt.threatbook-inc.cn为主域名。
总结:当server_name_in_redirect为off时,Location中的域名为curl时带的域名,如果为on,则为server_name中配置的主域名。
设置了port_in_redirect时(absolute_redirect为on,且server_name_in_redirect为on):
- 当设置为off时,返回内容如下:
- 当设置为on时(默认),返回内容如下:
总结:当port_in_redirect为off时,Location中不携带端口,当为on时,携带端口。