nginx总结第四章----nginx模块与指令详解

四 nginx模块

官方提供了5个类型的模块:核心模块、配置模块、事件模块、http模块、mail模块。
    配置模块主要负责解析nginx.conf文件,是其他模块的基础,该类模块中只有一个ngx_conf_module模块;
    核心模块主要负责定义除配置模块之外的其他模块,该类模块中有6个核心模块。
        ngx_mail_module负责定义mail模块;
        ngx_http_module负责定义http模块;
        ngx_events_module负责定义事件模块;
        ngx_core_module则是nginx启动加载的第一个模块,它主要用来保存全局配置项。
        ngx_openssl_module只有当加载了之后,nginx才支持https请求
        ngx_errlog_module
    事件模块即负责事件的注册、分发处理、销毁等,该类模块中主要有这几个模块:
        ngx_event_core_module负责加载其他事件模块,是其他事件模块的基础
        ngx_epoll_module,该模块则是我们后面需要重点分析的模块,它负责事件的注册、集成、处理等
        其他的模块比如ngx_kqueue_module这些我们后面基本不会涉及,就不解释了,毕竟是另外一种I/O多路复用机制,大致的思想是一样的
    http模块和mail模块也无需太多解释,等到后面再介绍。

image

4.1 核心模块

  1. ngx_core_module
  2. ngx_http_module
  3. ngx_event_module
  4. ngx_stream_module
  5. ngx_mail_module

4.2 http模块

4.2.1 ngx_http_core_module

  • 指令1: root和alias
配置root和alias,测试访问的资源路径。
root:
(1) 前缀匹配
    location /download1/ {
        root /usr/local/mywork/test/html;
    }
    或者:
    location /download1/ {
        root /usr/local/mywork/test/html/;
    }
    或者:
    location /download1 {
        root /usr/local/mywork/test/html;
    }
    或者:
    location /download1 {
        root /usr/local/mywork/test/html/;
    }
(2) 正则匹配
    location ~ ... 重复上述四种情况,
以上八种情况:
curl -x 127.0.0.1:80 static.test.com/download1/1.html
    ==> /usr/local/mywork/test/html/download1/1.html
curl -x 127.0.0.1:80 static.test.com/download1/zhangbao/1.html
    ==> /usr/local/mywork/test/html/download1/zhangbao/1.html

总结: root不论哪种匹配方式,不论root最后加没加/,访问的都是root_path+uri


alias:
    浏览器访问:http://test.com/download2/zhangbao/1.html
(1) 前缀匹配:
    location  /download2/ {
        alias /usr/local/mywork/test/html;
    }
    location  /download2/ {
        alias /usr/local/mywork/test/html/;
    }
    location  /download2 {
        alias /usr/local/mywork/test/html;
    }
    location  /download2 {
        alias /usr/local/mywork/test/html/;
    }
===> 以上四种情况: 
/usr/local/mywork/test/htmlzhangbao/1.html
/usr/local/mywork/test/html/zhangbao/1.html
/usr/local/mywork/test/html/zhangbao/1.html
/usr/local/mywork/test/html//zhangbao/1.html

(2) 正则匹配: 
    location ~ /download2/ {
        alias /usr/local/mywork/test/html;
    }
    location ~ /download2/ {
        alias /usr/local/mywork/test/html/;
    }
    location ~ /download2 {
        alias /usr/local/mywork/test/html;
    }
    location ~ /download2 {
        alias /usr/local/mywork/test/html/;
    }
===> 以上四种情况:
1和3: 
    403错误:日志显示 directory index of "/usr/local/mywork/test/html" is forbidden,request: "GET /download/zhangba/1.html/ HTTP/1.1"
2和4: 
    301无限重定向:浏览器地址栏目是:http://test.com/download2/web/1.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/
    
解释:
    1和3: 当配置的server或者location没有指定首页时,找不到文件,会报403错误。
    2和4static模块实现了root/alias功能时,发现访问目标是目录,但URL末尾未加/时,会返回301重定向。   
    absolute_redirect,port_in_redirect,server_name_in_redirect 可以控制
    
总结:
看来对于alias来说,用不带修饰符的前缀匹配方式比较合适,而且处理逻辑和proxy_pass的一样,将匹配中的部分去掉后再拼接
而alias不适合用正则匹配,可能有意想不到的错误。
  • 指令2: client_*
client_body_buffer_size  : 请求存在包体时,接收包体时所分配的内存
	Syntax: client_body_buffer_size size;
   Default: client_body_buffer_size 8k|16k;	
   Context: http, server, location
   如果接收头部时就已经接收到了完整的包体,则这段内存不会被分配
   如果剩余未接收的包体的长度小于client_body_buffer_size的大小,则仅分配所需的大小
   如果剩余未接收的包体的长度大于client_body_buffer_size的大小,则分配client_body_buffer_size大的空间接收body
   	当关闭body缓存时,则该内存上的内容立即会被发往上游,一段一段的收,一段一段的发。
   	当开启body缓存时,该段内存用完时,写入临时文件,并释放这段内存。
   	
client_body_in_single_buffer		
   	Syntax: client_body_in_single_buffer on | off;
   	Default: client_body_in_single_buffer off;
   	Context: http, server, location

client_max_body_size: 包体最大长度限制
   	Syntax: client_max_body_size size;
   	Default: client_max_body_size 1m;
   	Context: http, server, location
       仅对请求头部中含有Content-Length生效
       超出最大长度限制,返回413错误。
   	
client_body_temp_path :临时文件路径
   	Syntax: client_body_temp_path path [level1 [level2 [level3]]];
   	Default: client_body_temp_path client_body_temp;
   	Context: http, server, location
   	默认值是client_body_temp,即将超出client_body_buffer_size 的body存在这个目录下。
   	设置多级目录是为了防止一个目录下文件过多,文件的存取速度太慢。
   	
client_body_in_file_only on	:决定body是否写入文件,一般为了定位问题而配置的	
   	Syntax: client_body_in_file_only on | clean | off;
   	Default: client_body_in_file_only off;
   	Context: http, server, location
   	on: 用户上传的body会一直保留在文件中,请求处理完成后也会一直保留,文件不会被删除。
   	clean:只要有body,用户上传的body必须写入文件,但是请求完成后文件就会被删除。
   	off:如果body较小,用client_body_buffer_size大小的内存就能存下,则此body不会被写入文件中
   	
client_body_timeout time : 两次读取body之间的最大迟延,类似于tcp的keepalive_timeout的超时时间
   	Syntax: client_body_timeout time;
   	Default: client_body_timeout 60s;
   	Context: http, server, location
   	读取包体超时,则返回408错误。

4.2.2 autoindex模块

展示目录结构
    server {
        server_name autoindex.test.com;
        location / {
            alias /usr/local/mywork/test/;
            autoindex on;    
            autoindex_exact_size off;   
            autoindex_format html;
            autoindex_localtime on;
        }
    }

4.2.3 ngx_http_proxy_module

  • 作用:实现反向代理及缓存功能
  • 配置范例
  location / {
      proxy_pass http://github.com;
      proxy_redirect off;
      proxy_set_header HOST $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  • 指令1 :proxy_*
1 proxy_redirect : 修改返回的location的头部
        语法:proxy_redirect [ default|off|redirect replacement ] 
        默认值:proxy_redirect default 
        使用字段:http, server, location 
        如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置。
        假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
        这个指令: 
        proxy_redirect http://localhost:8000/two/  http://frontend/one/;
        将Location字段重写为http://frontend/one/some/uri/。
        参考连接:https://blog.csdn.net/u010391029/article/details/50395680

2 proxy_set_header :
        上下文: http、server、location
        默认:  proxy_set_header Host $proxy_host;
               proxy_set_header Connection close;
        注意:
        (1)http段,server段,location段同时配置了proxy_set_header时,只有location中会生效,例如:
		        http{
		            proxy_set_header xxx1 A
		            proxy_set_header xxx2 B
		            location / {
		                proxy_set_header xxx3 C
		            }
		        }
		        A和B都不会生效,除非去除C!
		(2) 若value的值为空(‘ ’),则整个header都不会向上游发送

3  proxy_method 
 	Syntax: proxy_method method;
	Default: —
	Context: http, server, location

4  proxy_http_version
	Syntax: proxy_http_version 1.0 | 1.1;
	Default: proxy_http_version 1.0;
	Context: http, server, location

5  proxy_pass_request_headers
		Syntax: proxy_pass_request_headers on | off;
		Default: proxy_pass_request_headers on;
		Context: http, server, location
		
6 proxy_pass_request_body 和 proxy_set_body
		生成发往上游的包体
		Syntax: proxy_pass_request_body on | off;
		Default: proxy_pass_request_body on;
		Context: http, server, location

		Syntax: proxy_set_body value;
		Default: —
		Context: http, server, location

7 proxy_request_buffering  :控制接收完完整包体再转发或者是边收边发。
		Syntax: proxy_request_buffering on | off;
		Default: proxy_request_buffering on;
		Context: http, server, location
		默认情况下是接收完完整包体再转发。

8 proxy_connect_timeout :建立tcp连接的时间 
		Syntax: proxy_connect_timeout time;
		Default: proxy_connect_timeout 60s;
		Context: http, server, location
		如果规定时间内三次握手不成功,未能建立连接,则会返回给客户端502
	
9    proxy_next_upstream : 发生错误时尝试与下一个上游服务器转发请求。 
		Syntax: proxy_next_upstream http_502 | ..;
		Default: proxy_next_upstream error timeout;
		Context: http, server, location

10 	proxy_socket_keepalive 
		Syntax: proxy_socket_keepalive on | off;
		Default: proxy_socket_keepalive off;
		Context: http, server, location
		on时会使用操作系统默认的tcp keepalive相关的配置进行探测,及时关掉不再使用的的tcp连接。

11 proxy_buffering  
		Syntax: proxy_buffering on | off;
		Default: proxy_buffering on;
		Context: http, server, location
	    默认开启,先接收完完整的包体&#
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值