In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use proxy_pass in nginx? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
An online http service. The key configuration of the example nginx is as follows:
Server {listen 80; server_name ligang.gdemo.com; server_tokens off; keepalive_timeout 5; charset utf-8; include / home/ligang/devspace/gobox-demo/conf/http/general/gzip.conf; access_log logs/ligang.gdemo.com.log combinedio buffer=1k; error_log logs/ligang.gdemo.com.log.err; location / {include / home/ligang/devspace/gobox-demo/conf/http/general/http_proxy.conf; proxy_intercept_errors on Proxy_pass http://ligang.proxy.gdemo.com;}}
Here you can see that when requesting ligang.gdemo.com, nginx proxies the request back to ligang.proxy.gdemo.com for processing.
The ligang.proxy.gdemo.com service has been deployed online and parsed to the three computer rooms A, B and C. now I want to adjust the analysis and get rid of the C computer room, leaving only An and B computer rooms.
After adjusting the resolution, check that the new resolution has taken effect, but observe the number of requests in computer room C and find that there is no change as before.
So I looked at the log of nginx in computer room C and found that the source of the request was still ligang.gdemo.com 's machine. After the domain name resolution was adjusted, the nginx still used the previous IP.
So after I reload all the nginx on ligang.gdemo.com 's machine, the request for C computer room is finally gone.
Problem description
The above question shows that if the domain name is used in the proxy_pass of nginx, then nginx will cache the resolution result and does not seem to update it, because in the above example, after adjusting the resolution, I went to see the log of computer room C almost every other day and found that there was no change in traffic.
In this way, if you configure a reverse proxy server, if the upstream adjusts the domain name and you are not notified, then your proxy service is equivalent to unavailable.
Look at how nginx parses the host ip from the code
I'm a little curious about how nginx parses the host ip, so trace the code:
Where the proxy_pass directive is defined (http/modules/ngx_http_proxy_module.c):
{ngx_string ("proxy_pass"), NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_TAKE1, ngx_http_proxy_pass, / / processing method NGX_HTTP_LOC_CONF_OFFSET, 0, NULL}
Ngx_http_proxy_pass method (http/modules/ngx_http_proxy_module.c):
Static char * ngx_http_proxy_pass (ngx_conf_t * cf, ngx_command_t * cmd, void * conf) {ngx_http_proxy_loc_conf_t * plcf = conf; size_t add; u_short port; ngx_str_t * value, * url; ngx_url_t u; ngx_uint_t n; ngx_http_core_loc_conf_t * clcf Ngx_http_script_compile_t sc;. Url = & value [1];. Ngx_memzero (& u, sizeof (ngx_url_t)); u.url.len = url- > len-add; u.url.data = url- > data + add; u.default_port = port; u.uri_part = 1; u.no_resolve = 1; plcf- > upstream.upstream = ngx_http_upstream_add (cf, & u, 0);}
Continue to trace the ngx_http_upstream_add method (http/ngx_http_upstream.c):
Ngx_http_upstream_srv_conf_t * ngx_http_upstream_add (ngx_conf_t * cf, ngx_url_t * u, ngx_uint_t flags) {ngx_uint_t I; ngx_http_upstream_server_t * us; ngx_http_upstream_srv_conf_t * uscf, * * uscfp; ngx_http_upstream_main_conf_t * umcf If (! (flags & NGX_HTTP_UPSTREAM_CREATE)) {if (ngx_parse_url (cf- > pool, u)! = NGX_OK) {if (u-> err) {ngx_conf_log_error (NGX_LOG_EMERG, cf, 0, "% s in upstream\"% V\ ", u-> err, & u-> url);}
Continue to trace the ngx_parse_url method (core/ngx_inet.c):
Ngx_int_tngx_parse_url (ngx_pool_t * pool, ngx_url_t * u) {u_char * p; p = u-> url.data; if (ngx_strncasecmp (p, (u_char *) "unix:", 5) = = 0) {return ngx_parse_unix_domain_url (pool, u);} if (p [0] = ='[') {return ngx_parse_inet6_url (pool, u) } return ngx_parse_inet_url (pool, u);}
Then comes the ngx_parse_inet_url method (core/ngx_inet.c):
Static ngx_int_tngx_parse_inet_url (ngx_pool_t * pool, ngx_url_t * u) {. If (ngx_inet_resolve_host (pool, u)! = NGX_OK) {return NGX_ERROR;}.}
Then comes the ngx_inet_resolve_host method (core/ngx_inet.c):
# if (NGX_HAVE_GETADDRINFO & & NGX_HAVE_INET6) ngx_int_t ngx_inet_resolve_host (ngx_pool_t * pool, ngx_url_t * u) {. If (getaddrinfo ((char *) host, NULL, & hints, & res)! = 0) {u-> err = "host not found"; ngx_free (host); return NGX_ERROR;}.} # else / *! NGX_HAVE_GETADDRINFO |! NGX_HAVE_INET6 * / ngx_int_tngx_inet_resolve_host (ngx_pool_t * pool, ngx_url_t * u) {. H = gethostbyname ((char *) host);.}
Think about how to solve this problem.
The simplest solutions have come to my mind as follows:
Execute nginx reload
The advantages and disadvantages of this approach are obvious:
Advantages: easy to operate.
Disadvantages: belong to what we often call the afterhand, need to do a good job of monitoring.
Configure resolver
You can dynamically update parsing by configuring resolver in nginx, roughly as follows:
Server {listen 80; server_name ligang.gdemo.com; resolver 8.8.8.8 valid=60s; resolver_timeout 3s; set $gproxy "ligang.proxy.gdemo.com"; location / {proxy_pass http://$gproxy;}}
The advantages and disadvantages of this method are as follows:
Advantages: the parsing address is automatically updated at regular intervals, and there is no need to do nginx reload manually.
Disadvantages: need to specify the DNS server address, if the server is down, or the address has changed, you need to modify the nginx configuration after reload.
After reading the above, have you mastered how to use proxy_pass in nginx? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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
How can I post the WORD document?
© 2024 shulou.com SLNews company. All rights reserved.