Nginx 架构篇 - 详解Http模块执行的11各阶段②

接着上回的博客,回顾一下http模块执行的11个阶段,我们继续下面的内容。

precontent阶段 : 按序访问资源的try_files模块

功能:依次试图访问多个url对应的文件(由root或者alias指定),当文件存在时直接返回文件内容,如果所有文件夹都不存在,按照最后一个url或者code返回。

location / {
       try_files $uri $uri/ /index.php?$query_string;
       ...
}

try_files功能在实际应用中,隐藏index.php会用的比较多。

precontent阶段 : 实时拷贝流量mirror模块

功能:处理请求时,生成请求访问其他服务,对子请求的返回值不做处理

server {
        listen       80;
        server_name  web1.www.com;
        # 源站配置
        location / {
                access_log  /data/nginx/1.14.1/logs/web1/access.log  accesslog;
                mirror /mirror;
                mirror_request_body on;# Indicates whether the client request body is mirrored. default value is on.
                proxy_pass http://web1.upstream.name;
        }
        # 镜像站点配置
        location /mirror {
                internal; # 内部配置
                proxy_pass http://mirror.web1.upstream.name$request_uri;
                proxy_pass_request_body on; # Indicates whether the original request body is passed to the proxied server. default value is on
                proxy_set_header X-Original-URI $request_uri; # reset uri
        }
}

content阶段 : 详解root和alias指令

功能:将url映射文件路径,以返回静态文件的内容
差别:root会将完整的url映射进文件的路径中,alias只会将location后的url映射到文件路径

content阶段 : static模块提供的三个变量

request_filename : 待访问文件的完整路径
document_root : 由URL和root/alias规则生成的文件夹路径
replace_root : 将document_root中的软连接等换成真是路径

location /realpath{
                alias html/realpath;
                return 200 '$request_filename:$document_root:$realpath_root\n';
        }

content阶段 : static模块对url不以斜杠结尾却访问目录的做法

absolute_redirect : 当用户访问资源时没有在末尾加反斜杠的时候 NGINX会返回一个301的重定向。

 server {
        listen       80;
        server_name  localhost;

        absolute_redirect  off; #关闭的时候 
        port_in_redirect on;
}

当关闭absolute_redirect的时候,当访问后,看到头部是不带域名的。

Server: nginx/1.14.2
Date: Fri, 15 Feb 2019 08:26:21 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: /alias/  #我们看到没有完整域名

指令 server_name_in_redirect 主要控制 是否返回的 host域名 还是server_name主域名

content阶段 : index和autoindex的用法

index模块:该指令用来指定用来做默认文档的文件名,可以在文件名处使用变量。 如果您指定了多个文件,那么将按照您指定的顺序逐个查找。 可以在列表末尾加上一个绝对路径名的文件。

语法: index file [file...]
默认值: index index.html
作用域: http, server, location

index  index.$geo.html  index.0.html  /index.html;

autoindex : 打开目录浏览功能

    location / {      
         autoindex on;
    }
    
    location /down/ {
	    alias /home/wwwroot/lnmp/test/;
	    autoindex on;
	}

content阶段 : 提升多个小文件性能的Concat模块

功能:当页面需要访问多个小文件时,把它们的内容合并到一次http响应中返回,提升性能。
concat是第三方模块,需要下载编译。

location /css/ {
	concat on;
	concat_max_files 20;
	concat_unique off;
}

log阶段 : access的详细大小

功能: http请求记录到日志中

access_log : access_log 指派路径、格式和缓存大小。参数 “off” 将清除当前级别的所有 access_log 指令。如果未指定格式,则使用预置的 “combined” 格式。缓存不能大于能写入磁盘的文件的最大大小。在 FreeBSD 3.0-6.0 ,缓存大小无此限制。

语法: access_log path [format [buffer=size | off ] 默认值: access_log log/access.log combined

作用域: http, server, location

log_format: 指令日志格式描述日志项的格式。除了格式中的常规变量外,还可以使用仅在记录到日志中时存在的变量:

语法: log_format name format [format ...]
默认值: log_format combined "..."
作用域: http server
发布了98 篇原创文章 · 获赞 185 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/xuezhiwu001/article/details/100088955