wenqiang li

Results 130 issues of wenqiang li

nginx框架通过upstream这个机制可以和上游服务器建立4层的连接。 http模块通过一个扩展的模块ngx_http_proxy_module.c提供了一个proxy_pass的命令支持7层的反向代理的功能。 下面就通过代码来看下nginx是如何实现的。 ## 配置解析与初始化 ### upstream 模块 ```ini upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com backup; } ``` upstream实现在ngx_http_upstream_module中,通过定义两个配置指令来配置上游服务器地址。 upstream的模块也是从定义了ngx_module_t。ctx定义了3个函数,ngx_http_upstream_add_variables添加了变量,ngx_http_upstream_create_main_conf创建配置文件所需要的内存结构。ngx_http_upstream_init_main_conf初始化配置结果。通过upstream指令可以配置一组上游服务器,该指令在nginx框架由ngx_http_upstream函数处理,把upstream的srv挂到upstream模块main配置的数组上(`uscfp = ngx_array_push(&umcf->upstreams);`)。在upstream...

### 概述 proxy模块是通过ups机制实现了反向代理功能的。该模块非常复杂。 在这里不会说ups机制,除非引入的非常深入。其余的均会带过,了解ups机制请看上篇。 proxy模块通过proxy_cache[_xxx]指令控制proxy缓存。 + 指令: proxy_hide_header field; 隐藏上游resp的header。 proxy_ignore_headers field ...; 忽略上游resp的header,不起作用。一般是x-accel-xxx的头。 和缓存相关的: proxy_cache_path 配置缓存文件的磁盘路径和存放缓存元数据的共享内存。 proxy_cache_valid 配置缓存的状态码和对应有效时常。 proxy_cache 是否开启缓存,和开启缓存要存放的路径和元数据共享内存,和proxy_cache_path对应,可以为变量。 proxy_cache_convert_head 是否把head请求转成get请求缓存。 proxy_cache_bypass 有缓存但是不从缓存获取。bypass。 ### 代码 + proxy_cache对应的处理函数ngx_http_proxy_cache。 其作用是是否开启缓存,可以配置变量和常量,如果是变量则该变量的值如果等于proxy_cache_path指令的keys_zone的name,则会走缓存。...

nginx

### 概述 ### 错误 kubectl create -f dapi-volume-resources.yaml `Error from server (BadRequest): error when creating "dapi-volume-resources.yaml": Pod in version "v1" cannot be handled as a Pod: unable to parse quantity's...

### 概述 k8s通过`ConfigMap`支持了pods统一的配置方案。 ### 使用 #### 创建ConfigMap资源 先通过yaml文件创建ConfigMap资源。 [root@master k8s]# cat cm-appvars-err.yaml ```yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-appvars-err data: apploglevel: debug appdatadir: /var/app/data 00app-log-dir: /var/app/log ``` 通过命令`kubectl create...

### 概述 ### 代码 main.c文件`start_kernel`函数会调用`sock_init`函数初始化网络文件系统, 该函数又会调用注册的协议族函数表对应的初始化函数。`AF_INET`对应的回调处理函数表是`inet_proto_ops`中的ip_proto_init函数。 该函数除了注册传输层支持的协议,还初始化了支持的链路层,还有定时回调函数。 ```c /* Hardware should be inited here. */ // 网络协议初始化 static int ip_proto_init(void) { int i; struct device *dev; struct ip_protocol *p; seq_offset...

### 概述 程序中使用网络首先会调用`socket`函数创建一个socket套接字的文件fd。 然后进行`bind` `listen`或者`connect`. 我们先来看socket tcp套件字的创建`int fd = socket(AF_INET, SOCK_STREAM, 0);`。 在glibc代码中,通过汇编调用80中断,中断号是`sys_socketcall`在系统调用表`sys_call_table`中的下标。 潜入到内核态调用`sys_socketcall`函数。 ### 代码 ```c int sys_socketcall(int call, unsigned long *args) { switch (call) { case SYS_SOCKET: verify_area(args,...

### 概述 应用程序通过`socket`创建套件字句柄,通过`bind`绑定地址, 通过`listen`监听端口,通过`connect`发起连接,通过`accept`接收连接, 通过`read`、`recv`读取数据,通过`write`、`send`发送数据。 我们就来看下这些函数的内核实现。 ### 代码分析 linux一切兼文件,网络子系统也是建立在文件系统之上的, 因此先回顾一下进程打开文件结构体,也就是说每个socket也会对应一个file结构体,通过`fd`索引。 ```c struct file { unsigned short f_mode; unsigned short f_flags; unsigned short f_count; unsigned short f_reada; unsigned short f_rdev; /*...

### 概述 过滤模块大量的被用于结束请求前向客户端发送resp被调用。 也有被用到保存请求body。 例如: + static模块的回调函数ngx_http_static_handler调用ngx_http_send_header发送响应的header,调用ngx_http_output_filter发送响应的body。 + rewrite模块的return指令调用的ngx_http_send_response函数,最终也调用ngx_http_send_header和ngx_http_output_filter。 + 处理request body的函数调用的ngx_http_top_request_body_filter。 发送响应的这两个函数的定义,最终调用的是ngx_http_top_header_filter 和 ngx_http_top_body_filter。如下函数定义。 保存请求包调用的是ngx_http_top_request_body_filter。 ```c typedef ngx_int_t (*ngx_http_output_header_filter_pt)(ngx_http_request_t *r); typedef ngx_int_t (*ngx_http_output_body_filter_pt) (ngx_http_request_t *r, ngx_chain_t *chain); typedef ngx_int_t...

nginx

## 概述 nginx通过proxy_pass url; 来指定一组上游服务器,来实现7层http的反向代理功能。 通过URL指定一组上游服务器,URL可以是变量、域名、upstream的配置名称。 ```ini server { ...... set $ups "127.0.0.1:8990"; location /test1/ { proxy_pass http://$ups/; } location /test2/ { proxy_pass http://$ups; } location /test3/ { proxy_pass http://127.0.0.1:8991/;...

nginx

### 概述 [gzip 模块](http://nginx.org/en/docs/http/ngx_http_gzip_module.html) 是压缩resp的body的。 通过配置gzip on;来开启压缩。 Accept-Encoding 和 Content-Encoding 是使用压缩的一对头部字段。Accept-Encoding表示客户端支持的压缩格式,Content-Encoding表示服务端选择的压缩格式。 + gzip 相关内容 gzip是用[zlib](https://www.zlib.net/manual.html)压缩的,[zlib的一个例子](http://blog.163.com/yuan_zhch/blog/static/193790046201182102746293/) 压缩过程:deflateInit2() ->deflate() ->deflateEnd(); 对应的解压过程 inflateInit2() -> inflate() -> inflateEnd(); deflateInit2 ----------- `int deflateInit2 ((z_streamp strm,...

nginx