Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the optimization method of bind ip for nginx origin-pull?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "what is the optimization method of nginx back-to-origin bind ip", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the optimization method of bind ip when nginx is back to origin?"

Brief introduction

Proxy_bind belongs to proxy_module. For local ip when establishing a connection to the backend, only one ip of bind is supported for origin-pull in the nginx source code. If you want to use multiple ip for origin-pull, you can modify the source code to support bind ip array. That's what I do in practice. Bind ip data polling selects ip for origin-pull to establish a connection with upstream to solve the problem of limiting the number of origin-pull connections in a single ip. The following proxy_bind section is the optimized code for proxy_bind, which supports bind multi-ip.

Check_bind is the source ip used to check the health of the origin server. In the health check of the upstream, the source ip and upstream Jianlian used to judge the health status according to the response, and distinguish the ip group used in the health check from the ip group used in the business back-to-origin service. Check_bind configuration is not a function that comes with nginx, which requires secondary development of nginx.

Proxy_bind

Nginx source code configuration:

{ngx_string ("proxy_bind")

NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12

Ngx_http_upstream_bind_set_slot

NGX_HTTP_LOC_CONF_OFFSET

Offsetof (ngx_http_proxy_loc_conf_t, upstream.local)

NULL}

Improved configuration:

Ngx_string ("proxy_bind")

NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE

Ngx_http_upstream_bind_set_slot_array

NGX_HTTP_LOC_CONF_OFFSET

Offsetof (ngx_http_proxy_loc_conf_t, upstream.local_array)

NULL}

Here is the related code optimization section:

Char * ngx_http_upstream_bind_set_slot_array (ngx_conf_t * cf, ngx_command_t * cmd

Void * conf)

{

...

Ngx_http_upstream_local_array_t * * plocal, * local

Plocal = (ngx_http_upstream_local_array_t * *) (p + cmd- > offset)

If (* plocal! = NGX_CONF_UNSET_PTR) {

Return "bind is duplicate"

}

Value = cf- > args- > elts

/ / establish local array

Local = ngx_pcalloc (cf- > pool, sizeof (ngx_http_upstream_local_array_t))

* plocal = local

/ / establish local peer addr

Local- > addr = ngx_pcalloc (cf- > pool, sizeof (ngx_peer_addrs_t))

/ / establish addr array

Local- > addr- > addrs = ngx_array_create (cf- > pool, 1, sizeof (ngx_addr_t))

/ / iterate through all local ip and put them in array

For (I = 1; I

< cf->

Args- > nelts; iTunes +) {

Addr = ngx_array_push (local- > addr- > addrs)

If (addr = = NULL) {

Return NGX_CONF_ERROR

}

Rc = ngx_parse_addr (cf- > pool, addr, value [I] .data, value [I] .len)

Switch (rc) {

Case NGX_OK:

Addr- > name = value [I]

Break

Case NGX_DECLINED:

Ngx_conf_log_error (NGX_LOG_EMERG, cf, 0

"invalid address\"% V\ ", & value [I])

/ * fall through * /

Default:

Return NGX_CONF_ERROR

}

}

...

}

Then assign u → peer.local_array to u → conf → local_array in init_request (that is, the one that has just been assigned by instruction)

Static void ngx_http_upstream_init_request (ngx_http_request_t * r)

{

...

U = r-> upstream

U-> peer.local_array = ngx_http_upstream_get_local_array (r, u-> conf- > local_array)

...

}

How did you get this u → conf? In fact, it is obtained through different handler. For example, proxy_pass, which is commonly used in our tengine configuration, can be assigned to this value.

Static ngx_int_t ngx_http_proxy_handler (ngx_http_request_t * r)

{

...

U = r-> upstream

/ / obtain the loc conf of proxy

Plcf = ngx_http_get_module_loc_conf (r, ngx_http_proxy_module)

/ / get the corresponding conf

U-> conf = & plcf- > upstream

...

}

The plcf- > upstream → upstream is the real upstream_srv_conf, which is obtained in proxy_pass

Static char * ngx_http_proxy_pass (ngx_conf_t * cf, ngx_command_t * cmd, void * conf)

{

...

Plcf- > upstream.upstream = ngx_http_upstream_add (cf, & u, 0)

...

}

Check_bind

Add chenk_bind module

Check_bind adds global_local to the main_conf of the health check.

Ngx_string ("check_bind")

NGX_HTTP_MAIN_CONF | NGX_CONF_1MORE

Ngx_http_upstream_bind_set_slot_array

NGX_HTTP_MAIN_CONF_OFFSET

Offsetof (ngx_http_upstream_check_main_conf_t, global_local)

NULL

Typedef struct {

Ngx_uint_t check_shm_size

Ngx_http_upstream_check_peers_t * peers

Ngx_http_upstream_local_array_t * global_local

} ngx_http_upstream_check_main_conf_t

The next operation is similar to proxy_bind

Char * ngx_http_upstream_bind_set_slot_array (ngx_conf_t * cf, ngx_command_t * cmd

Void * conf)

{

...

/ / the global here is upstream_check_main_conf

...

}

After storing the global_local in the ucmcf, the next step is to put the global in each upstream, that is, uscf

The init_main method of upstream_check is called

Static char * ngx_http_upstream_check_init_main_conf (ngx_conf_t * cf, void * conf)

{

...

/ / get the main conf of upstream module

Umcf = ngx_http_conf_get_module_main_conf (cf, ngx_http_upstream_module)

/ / get the pointer to the backend array

Uscfp = umcf- > upstreams.elts

For (I = 0; I)

< umcf->

Upstreams.nelts; iTunes +) {

/ / cyclic assignment

If (ngx_http_upstream_check_init_srv_conf (cf, uscfp [I], ucmcf- > global_local)! = NGX_OK) {

Return NGX_CONF_ERROR

}

}

...

}

Static char * ngx_http_upstream_check_init_srv_conf (ngx_conf_t * cf, void * conf

Ngx_http_upstream_local_array_t * global_local)

{

...

Ngx_http_upstream_srv_conf_t * us = conf

/ / get the check module conf under this upstream srv conf

Ucscf = ngx_http_conf_upstream_srv_conf (us, ngx_http_upstream_check_module)

/ / perform assignment

If (ucscf- > global_local = = NGX_CONF_UNSET_PTR) {

If (global_local! = NGX_CONF_UNSET_PTR & & global_local! = NULL) {

Ucscf- > global_local = global_local

}

}

...

}

If dyups is compatible, you need to ensure that the value is assigned in add peer and add peer at the same time.

Ngx_uint_t ngx_http_upstream_check_add_peer (ngx_conf_t * cf

Ngx_http_upstream_srv_conf_t * us, ngx_addr_t * peer_addr

# ifdef CONFIG_NGX_NS

, ngx_int_t vni, ngx_addr_t * hp_addr, ngx_addr_t * hp2_addr, _ _ U8 * mac

# endif

)

{

...

/ / add check_bind support for dyups modules.

If (ucscf- > global_local = = NGX_CONF_UNSET_PTR) {

If (ucmcf- > global_local! = NGX_CONF_UNSET_PTR & & ucmcf- > global_local! = NULL) {

Ucscf- > global_local = ucmcf- > global_local

}

}

/ / add peer

Peers = ucmcf- > peers

Peer = ngx_array_push (& peers- > peers)

Peer- > index = peers- > peers.nelts-1

/ / this part is very critical. The ucscf that has just been assigned global_local is assigned as peer- > conf.

Peer- > conf = ucscf

Peer- > upstream_name = & us- > host

Peer- > peer_addr = peer_addr

...

}

Assign all these handler values in add_timer, and data is specified as the peer generated above

Static ngx_int_t ngx_http_upstream_check_add_timer (ngx_http_upstream_check_peer_t * peer

Ngx_check_conf_t * check_conf, ngx_msec_t timer, ngx_log_t * log)

{

...

Peer- > check_ev.handler = ngx_http_upstream_check_begin_handler

Peer- > check_ev.log = log

Peer- > check_ev.data = peer

...

}

Ngx_http_upstream_check_connect_handler. This is the handler that is called during upstream check.

Static void ngx_http_upstream_check_begin_handler (ngx_event_t * event)

{

...

If (peer- > shm- > owner = = ngx_pid) {

Ngx_http_upstream_check_connect_handler (event)

}

...

}

Static void ngx_http_upstream_check_connect_handler (ngx_event_t * event)

{

...

Peer = event- > data

/ / the conf of peer is ucscf

Ucscf = peer- > conf

/ / assignment

If (peer- > conf- > global_local! = NGX_CONF_UNSET_PTR & & peer- > conf- > global_local! = NULL) {

Peer- > pc.local_array = peer- > conf- > global_local- > addr

} else {

Peer- > pc.local_array = NULL

}

Rc = ngx_event_connect_peer (& peer- > pc)

...

}

At this point, I believe that everyone on the "nginx back-to-origin bind ip optimization method is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report