Skip to content

ngx_http_proxy_module ​


更新于 2024-12-05

代理请求流程 ​

接受客户端连接 ​

c
ngx_epoll_process_events    //事件入口
|_ngx_event_accept          //接受新连接
  |_register_handle_function
    //ngx_unix_recv(recv)
    //ngx_unix_send(send)       
    //ngx_readv_chain(readv)
    //ngx_linux_sendfile_chain(writev)

处理客户端请求 ​

c
ngx_epoll_process_events               //事件入口
|-ngx_http_wait_request_handler        //分配内存,接收数据,位置c->buffer
  |-ngx_http_create_request            //创建ngx_http_request_t,头部数据位置r->headers_in
  |_ngx_http_process_request_line      //解析请求行
    |-ngx_http_process_request_headers //解析请求头
      |-ngx_http_process_request       //*设置事件回调,Todo:具体作用
        |-ngx_http_handler             //进入多阶段处理
          |_ngx_http_core_run_phases   //多阶段处理

多阶段处理 ​

c
ngx_http_core_run_phases                      //多阶段处理
 |-ngx_http_core_content_phase                //content阶段
   |-ngx_http_proxy_handler                   //proxy_pass指令处理函数
     |-ngx_http_upstream_create               //创建ngx_http_upstream_t
     |_ngx_http_read_client_request_body      //消息体处理,消息位置r->request_body->buf
	   |-ngx_http_do_read_client_request_body //读取消息体
	   | |-ngx_http_request_body_filter       //消息体处理
	   | |_ngx_http_copy_pipelined_header     //分离不同请求头
       |-ngx_http_upstream_init               
         |_ngx_http_upstream_init_request      //u->request_bufs= r->request_body->bufs;
           |_ngx_http_proxy_create_request    //创建请求,更新数据
           | |-ngx_http_script_len_code       //变量替换
           | |_ngx_http_script_copy_code
           |_ngx_http_upstream_connect         //连接源站,*设置事件回调
             |_ngx_http_upstream_send_request  //若connect成功则发送;链接未成功则后续事件内发送

proxy转发请求 ​

c
ngx_epoll_process_events                           
|-ngx_http_upstream_handler                       //upstream写事件处理
  |_ngx_http_upstream_send_request_handler        //发送request事件处理
    |_ngx_http_upstream_send_request              //发送request
      |-ngx_http_upstream_send_request_body       //发送数据,位置u->request_bufs
	    |-ngx_output_chain
		  |-ngx_chain_writer
		    |-ngx_linux_sendfile_chain
			  |_writev                            //发送数据

proxy接受应答 ​

c
ngx_epoll_process_events
|-ngx_http_upstream_handler                        //upstream读事件处理
  |_ngx_http_upstream_process_header               //处理应答头, 数据位置u->buffer
    |-ngx_http_proxy_process_status_line           //处理状态行
    | |-ngx_http_proxy_process_header              //处理应答头
    |   |-ngx_http_proxy_process_content_length
    |   |-ngx_http_upstream_copy_header_line
    |-ngx_http_upstream_process_handlers
    | |-ngx_http_upstream_copy_content_type
    |-ngx_http_upstream_send_response              //更新事件回调,p->preread_bufs
      |-ngx_http_send_header
      | |-ngx_http_top_header_filter
      |-ngx_http_proxy_input_filter_init
      |-ngx_http_upstream_process_upstream
      | |-ngx_http_upstream_process_request
      | | |-ngx_http_upstream_finalize_request
      | | | |-ngx_http_upstream_process_trailers

应答客户端 ​

c
ngx_epoll_process_events
|-ngx_http_upstream_handler
  |-ngx_http_upstream_process_upstream
    |-ngx_event_pipe
      |-ngx_http_pipe_write_to_downstream
        |-ngx_http_upstream_output_filter
          |-ngx_http_output_filter
            |-ngx_http_top_body_filter     //body filter
              |-ngx_http_copy_filter       //将内容从上有传递到下游
              |-ngx_http_postpone_filter   //控制响应发送顺序
              |-ngx_http_write_filter      //数据写入客户端套接字
			    |-ngx_linux_sendfile_chain
				  |_writev                 //发送数据

指令 ​

proxy_bind ​

yaml
Syntax:    proxy_bind address [transparent] | off;
Default:    —
Context:    http, server, location

指定上行使用的源IP
transparent属性指定上行时使用非本地IP作为源IP

proxy_buffer_size ​

单次接收源站应答消息的缓存区大小

proxy_buffering ​

缓存源站应答

proxy_buffers ​

缓存单个链接的缓存大小与缓存个数

proxy_busy_buffers_size ​

proxy_cache ​

定义缓存区

proxy_cache_background_update ​

允许子请求更新超时缓存条目

proxy_cache_bypass ​

条件判断 是否使用缓存的应答消息

proxy_cache_convert_head ​

proxy_cache_key ​

定义缓存key格式

proxy_cache_lock ​

多个请求cache key相同时,仅有一个请求可以填充新的cache

proxy_cache_lock_age ​

proxy_cache_lock_timeout ​

proxy_cache_max_range_offset ​

proxy_cache_methods ​

客户端哪些方法需要缓存应答

proxy_cache_min_uses ​

设置缓存连续请求的中间求数间隔数

proxy_cache_path ​

缓存路径 文件名cache key的md5值

proxy_cache_purge ​

删除缓存条件

proxy_cache_revalidate ​

proxy_cache_use_stale ​

proxy_cache_valid ​

不同请求对应的缓存超时时间

proxy_connect_timeout ​

与源站建链超时时间

proxy_force_ranges ​

proxy_headers_hash_bucket_size ​

proxy_headers_hash_max_size ​

proxy_hide_header ​

源站应答UA时,指定需要隐藏的头

proxy_http_version ​

与源站交互使用的HTTP版本

proxy_ignore_client_abort ​

客户端不等应答断开时,代理与源站之间是否需要断开链接

proxy_ignore_headers ​

不处理源站应答里的消息头

proxy_intercept_errors ​

源站错误码大于等于300时,代理转发UA或拦截重定向

proxy_limit_rate ​

限制从源站读取应答的速率

proxy_max_temp_file_size ​

proxy_method ​

UA请求到源站时,指定请求方法

proxy_next_upstream ​

转发请求给下一源站的条件

proxy_next_upstream_timeout ​

proxy_next_upstream_tries ​

proxy_no_cache ​

不缓存源站的条件

proxy_pass ​

yaml
Syntax:    proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

示例

yaml
location / {
    proxy_pass       http://localhost:8000;  #上行出口
    proxy_set_header Host      $host;        #上行修改host头
    proxy_set_header X-Real-IP $remote_addr; #上行添加头
}
#------------
upstream realserver{                         #上行集群
    www.test1.com;
    www.test2.com;
}

location / {
    proxy_pass                 realserver; #上行代理到集群
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}

proxy_pass_header ​

源站-->UA,允许转发被其他模块禁掉的消息头

proxy_pass_request_body ​

UA-->源站,转发请求消息体

proxy_pass_request_headers ​

UA-->源站,转发请求消息头

proxy_read_timeout ​

proxy_redirect ​

proxy_request_buffering ​

proxy_send_lowat ​

proxy_send_timeout ​

proxy_set_body ​

转发UA请求给源站时,覆盖消息体

proxy_set_header ​

转发UA请求给源站时,可以重写或新增头部

proxy_socket_keepalive ​

设置与源站之间的socket链接属性SO_KEEPALIVE

proxy_ssl_certificate ​

指定TLS证书

proxy_ssl_certificate_key ​

私钥

proxy_ssl_ciphers ​

TLS套件

proxy_ssl_conf_command ​

通过ssl会话属性

proxy_ssl_crl ​

指定吊销证书文件

proxy_ssl_name ​

重写

proxy_ssl_password_file ​

密钥密码文件

proxy_ssl_protocols ​

启用指定TLS协议版本

proxy_ssl_server_name ​

与源站建立TLS会话时,是否携带SNI

proxy_ssl_session_reuse ​

重用SSL会话

proxy_ssl_trusted_certificate ​

CA证书

proxy_ssl_verify ​

验证源站服证书

proxy_ssl_verify_depth ​

源站证书链验证深度

proxy_store ​

proxy_store_access ​

proxy_temp_file_write_size ​

proxy_temp_path ​

存储从源站接受的零时文件数据