In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use listen instructions in nginx". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Listen instruction
Nginx as a high-performance http server, network processing is its core, understanding the initialization of the network will help to deepen the understanding of nginx network processing. There are two main network-related configuration commands: listen and sever_name. The listen command sets the nginx listening address. For ip protocol, this address is address and port. For unix domain socket protocol, this address is path. A listen instruction can specify only one address or port,address or hostname.
Starting with this article, we analyze the parsing process of the listen directive. The configuration of the listen instruction is as follows: we can get how to use listen from the nginx.org manual:
Listen address [: port] [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on | off] [ssl] [so_keepalive=on | off | [keepidle]: [keepintvl]: [keepcnt]]
The parameters carried by a listen instruction are complex. However, we generally pay little attention to parameters that are less commonly used. Here are some common configuration methods:
Listen 127.0.0.1 listen 8000 Portal 127.0.0.1 does not add port, default listener port 80; listen 8000listen *: 8000listen localhost:8000
Parsing uri and Port in listen instruction
As we know from the above, listen can be used in many ways. We need to get the port number and uri part of the listen instruction when parsing. Nginx provides the ngx_parse_url () method to parse uri and port, which is called when parsing the listen instruction.
Ngx_int_tngx_parse_url (ngx_pool_t * pool, ngx_url_t * u) {u_char * p; size_t len; p = u-> url.data; len = u-> url.len; / / here is the protocol if for parsing unix domain (len > = 5 & & ngx_strncasecmp (p, (u_char *) "unix:", 5) = 0) {return ngx_parse_unix_domain_url (pool, u) } / / parsing ipv6 protocol if (len & & p [0] = ='[') {return ngx_parse_inet6_url (pool, u);} / parsing ipv4 protocol return ngx_parse_inet_url (pool, u);}
We use the ipv4 protocol, and here we analyze the ngx_parse_inet_url () function
/ / u.url = "80"; / / u.listen = 1 port port / u.default_port = 80 position static ngx_int_tngx_parse_inet_url (ngx_pool_t * pool, ngx_url_t * u) {u_char * p, * host, * port, * last, * uri, * args; size_t len; ngx_int_tn; struct sockaddr_in * sin;#if (ngx_have_inet6) struct sockaddr_in6 * sin6 # endif u-> socklen = sizeof (struct sockaddr_in); sin = (struct sockaddr_in *) & u-> sockaddr; sin- > sin_family = af_inet;// ipv4 type u-> family = af_inet; host = u-> url.data; / / "80" last = host + u-> url.len; / / host position of the last character port = ngx_strlchr (host, last,':') / / find port, here is null uri = ngx_strlchr (host, last,'/'); / / find uri, here is null args = ngx_strlchr (host, last,'?); / / find parameter args, here is null if (args) {if (uri = = null | | args
< uri) { uri = args; } } if (uri) { if (u->Listen | |! U-> uri_part) {u-> err = "invalid host"; return ngx_error;} u-> uri.len = last-uri; u-> uri.data = uri; last = uri; if (uri)
< port) { port = null; } } if (port) { port++; len = last - port; n = ngx_atoi(port, len); if (n < 1 || n >65535) {u-> err = "invalid port"; return ngx_error;} u-> port = (in_port_t) n; sin- > sin_port = htons ((in_port_t) n); u-> port_text.len = len; u-> port_text.data = port; last = port-1;} else {if (uri = = null) {if (u-> listen) {/ * test value as port only * / n = ngx_atoi (host, last-host) If (n! = ngx_error) {if (n)
< 1 || n >65535) {u-> err = "invalid port"; return ngx_error;} u-> port = (in_port_t) n; sin- > sin_port = htons ((in_port_t) n); u-> port_text.len = last-host; u-> port_text.data = host; u-> wildcard = 1; return ngx_ok;} u-> no_port = 1; u-> port = u-> default_port; sin- > sin_port = htons (u-> default_port);} len = last-host If (len = = 0) {u-> err = "no host"; return ngx_error;} u-> host.len = len; u-> host.data = host; if (u > listen & & len = = 1 & & * host = ='*') {sin- > sin_addr.s_addr = inaddr_any; u-> wildcard = 1; return ngx_ok;} sin- > sin_addr.s_addr = ngx_inet_addr (host, len) If (sin- > sin_addr.s_addr! = inaddr_none) {if (sin- > sin_addr.s_addr = = inaddr_any) {u-> wildcard = 1;} u-> naddrs = 1; u-> addrs = ngx_pcalloc (pool, sizeof (ngx_addr_t)); if (u-> addrs = = null) {return ngx_error;} sin = ngx_pcalloc (pool, sizeof (struct sockaddr_in)); if (sin = = null) {return ngx_error } ngx_memcpy (sin, & u-> sockaddr, sizeof (struct sockaddr_in)); u-> addrs [0] .sockaddr = (struct sockaddr *) sin; u-> addrs [0] .socklen = sizeof (struct sockaddr_in); p = ngx_pnalloc (pool, u-> host.len + sizeof (": 65535")-1); if (p = = null) {return ngx_error } u-> addrs [0] .name.len = ngx_sprintf (p, "% vhost% d", & u-> host, u-> port)-p; u-> addrs [0] .name.data = p; return ngx_ok;} if (u-> no_resolve) {return ngx_ok;} if (ngx_inet_resolve_host (pool, u)! = ngx_ok) {return ngx_error;} u-> family = u-> addrs [0] .sockaddr-> sa_family U-> socklen = u-> addrs [0] .socklen; ngx_memcpy (& u-> sockaddr, u-> addrs [0] .sockaddr, u-> addrs [0] .socklen); switch (u-> family) {# if (ngx_have_inet6) case af_inet6: sin6 = (struct sockaddr_in6 *) & u-> sockaddr; if (& sin6- > sin6_addr) {u-> wildcard = 1;} break # endif default: / * af_inet * / sin = (struct sockaddr_in *) & u-> sockaddr; if (sin- > sin_addr.s_addr = = inaddr_any) {u-> wildcard = 1;} break;} return ngx_ok;}
This function parses the address and port number of our listen. In our configuration file, the port number is 80, and no listener address is configured, so u-> wildcard = 1, indicating that this is a wildcard. You want to listen on the port number of all ip addresses of the server.
Parsing listen instruction
Let's take a look at the listen configuration from the source code:
{ngx_string ("listen"), ngx_http_srv_conf | ngx_conf_1more, ngx_http_core_listen, ngx_http_srv_conf_offset, 0, null}
We can see from the configuration file that listen can only appear in the server module and can take multiple parameters.
The corresponding handler function is ngx_http_core_listen. Let's analyze this function and delete some code that makes misjudgments.
Static char * ngx_http_core_listen (ngx_conf_t * cf, ngx_command_t * cmd, void * conf) {ngx_http_core_srv_conf_t * cscf = conf; ngx_str_t * value, size; ngx_url_t u; ngx_uint_t n; ngx_http_listen_opt_t lsopt; cscf- > listen = 1; value = cf- > args- > elts; ngx_memzero (& u, sizeof (ngx_url_t)); u.url = value [1]; u.listen = 1 U.default_port = 80; if (ngx_parse_url (cf- > pool, & u)! = ngx_ok) {return ngx_conf_error;} ngx_memzero (& lsopt, sizeof (ngx_http_listen_opt_t)); ngx_memcpy (& lsopt.sockaddr.sockaddr, & u.sockaddr, u.socklen); lsopt.socklen = u.socklen; lsopt.backlog = ngx_listen_backlog; lsopt.rcvbuf =-1; lsopt.sndbuf =-1 # if (ngx_have_setfib) lsopt.setfib =-1 * dif * if (ngx_have_tcp_fastopen) lsopt.fastopen =-1 *
< cf->Args- > nelts; nasty +) {if (ngx_strcmp (value [n] .data, "default_server") = = 0 | | ngx_strcmp (value [n] .data, "default") = = 0) {lsopt.default_server = 1; continue } / / the rest of the code deals with various parameters of listen, which is of no use to our analysis here} if (ngx_http_add_listen (cf, cscf, & lsopt) = = ngx_ok) {return ngx_conf_ok;} return ngx_conf_error;}
The overall flow of this function is to parse the parameters of the listen instruction to generate a ngx_http_listen_opt_t, which, as the name implies, is the listening port option that holds some listening ports. Here is a function called ngx_parse_url (), which we have analyzed above, and its function is to parse address and port in url.
Then the most important part comes when the ngx_http_core_listen () function calls the ngx_http_add_listen () function at the end, which stores the port information of the listen into the ports dynamic array of the ngx_http_core_main_conf_t structure.
Ngx_http_add_listen () function
/ / cf: configuration structure / / cscf: configuration structure of the server where the listen instruction is located / / lsopt: listen optionngx_int_tngx_http_add_listen generated by ngx_http_core_listen () (ngx_conf_t * cf, ngx_http_core_srv_conf_t * cscf, ngx_http_listen_opt_t * lsopt) {in_port_t p; ngx_uint_t i; struct sockaddr * sa; ngx_http_conf_port_t * port Ngx_http_core_main_conf_t * cmcf; / / get the main_conf structure of the ngx_http_core_module module ngx_http_core_main_conf_t cmcf = ngx_http_conf_get_module_main_conf (cf, ngx_http_core_module) The / / ports field is an array of if (cmcf- > ports = = null) {cmcf- > ports = ngx_array_create (cf- > temp_pool, 2, sizeof (ngx_http_conf_port_t)); if (cmcf- > ports = = null) {return ngx_error;} sa = & lsopt- > sockaddr.sockaddr; p = ngx_inet_get_port (sa); port = cmcf- > ports- > elts; for (I = 0; I
< cmcf->Ports- > nelts; iTunes +) {if (p! = port [I] .port | | sa- > sa_family! = port [I] .family) {continue;} / * a port is already in the port list * / return ngx_http_add_addresses (cf, cscf, & port [I], lsopt);} / * add a port to the port list * / port = ngx_array_push (cmcf- > ports); if (port = null) {return ngx_error;} port- > family = sa- > sa_family Port- > port = p; port- > addrs.elts = null; return ngx_http_add_address (cf, cscf, port, lsopt);}
This function saves the information about the port number in the port field of the ngx_http_core_main_conf_t structure.
This is the end of the content of "how to use the listen instruction in nginx". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.