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 > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what to do when nginx agents make 302 errors many times. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Use proxy_intercept_errors and recursive_error_pages to represent 302 multiple times
302 is a frequently used state code in the HTTP protocol, and it is one of many ways to redirect, and its semantics are often interpreted as "Moved Temporarily". By the way, most of the 302s used in reality are misused (mixed with 303307). In HTTP/1.1, its semantic is "Found".
302 sometimes it's obvious, sometimes it's covert. In the simplest case, when we type a URL An in the browser, and then the browser address bar automatically jumps to B, and then opens a web page, this situation is likely to be 302.
More covert situations often occur in players embedded in web pages. For example, when you open a Youku video playback page, grab the bag and observe it and you will often find the shadow of 302. However, since these url are not opened directly in the browser, there is no change in the browser's address bar. Of course, if you specifically pick out and copy these specific url into the browser's address bar, you can still observe them.
Youku was mentioned in the previous paragraph. In fact, most online video sites will use 302, the reason is very simple, video site traffic is generally large, will use CDN, the difference only lies in the use of self-built CDN or commercial CDN. Because 302's redirection semantics (again, 302's semantics are widely misused, we should probably use 303 or 307 when using 302, but don't worry about this later), it can be well combined with scheduling in CDN.
Let's take a look at an example. Open a NetEase video playback page, grab the bag, and find the url in status 302. For example:
Http://flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4
When we copy it to the browser address bar, we will find that the address bar quickly becomes another url. The Url is uncertain and may be:
Http://14.18.140.83/f6c00af500000000-1408987545-236096587/data6/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4
With the curl tool, you can see the whole process more clearly:
Curl-I "http://flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4"-LHTTP/1.1 302 Moved Temporarily Server: nginx Date: Mon 25 Aug 2014 14:49:43 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive NG: CCN-SW-1-5L2 X-Mod-Name: GSLB/3.1.0 Location: http://119.134.254.9/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 HTTP/1.1 302Moved Temporarily Server: nginx Date: Mon 25 Aug 200 14:49:41 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive X-Mod-Name: Mvod-Server/4.3.3 Location: http://119.134.254.7/cc89fdac00000000-1408983581-2095617481/data4/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 NG: CHN-SW-1-3Y1 HTTP/1.1 2014 OK Server: nginx Date: Mon 25 Aug 2014 14:49:41 GMT Content-Type: video/mp4 Content-Length: 3706468 Last-Modified: Mon, 25 Aug 2014 00:23:50 GMT Connection: keep-alive Cache-Control: no-cache ETag: "53fa8216-388e64" NG: CHN-SW-1-3g6 X-Mod-Name: Mvod-Server/4.3.3 Accept-Ranges: bytes
As you can see, I experienced 302 twice in the process.
Putting this example aside for a while, let's talk about another important term: proxy. We usually joke that some leaders are 302 type and some leaders are proxy type. 302 types of leaders, a thing through his hands, will be quickly transferred to others, while the proxy type of leaders will be involved in things, or even get things done.
Going back to the above example, if there are multiple 302s in the middle of visiting a url, what if you need to design a proxy with Nginx to hide all these 302s?
1. Original Proxy
We know that Nginx itself is an excellent proxy server. So first let's set up a Nginx forward proxy with a server IP of 192.168.109.128 (one of my test virtual machines).
The initial configuration is simplified as follows:
Server {listen 80; location / {rewrite_by_lua 'ngx.exec ("/ proxy-to".. Ngx.var.request_uri)';} location ~ / proxy-to/ ([^ /] +) (. *) {proxy_pass http://$1$2$is_args$query_string;}}
The function implemented is that when using the
Http://192.168.109.128/xxxxxx
When the agent is accessed, it proxy to the real server that xxxxxx represents.
The test results are as follows:
Curl-I "http://192.168.109.128/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4"-LHTTP/1.1 302 Moved Temporarily Server: nginx/1.4.6 Date: Mon 25 Aug 2014 14:50:54 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive NG: CCN-SW-1-5L2 X-Mod-Name: GSLB/3.1.0 Location: http://183.61.140.24/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 HTTP/1.1 302Moved Temporarily Server: nginx Date: Mon 25 Aug 200 14:50:55 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive X-Mod-Name: Mvod-Server/4.3.3 Location: http://183.61.140.20/540966e500000000-1408983655-236096587/data1/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 NG: CHN-ZJ-4-3M4 HTTP/1.1 2014 OK Server: nginx Date: Mon 25 Aug 2014 14:50:55 GMT Content-Type: video/mp4 Content-Length: 3706468 Last-Modified: Mon, 25 Aug 2014 00:31:03 GMT Connection: keep-alive Cache-Control: no-cache ETag: "53fa83c7-388e64" NG: CHN-ZJ-4-3M4 X-Mod-Name: Mvod-Server/4.3.3 Accept-Ranges: bytes
It can be seen that although proxy is used, the process is no different from the original access. The access process is, when you visit
Http://192.168.109.128/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4
Nginx will proxy the request to the
Http://flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4
The latter will immediately return a 302, so Nginx, as a proxy, sends the 302 back to the client, which re-initiates the request and repeats the previous 302. This shows that once the proxy backend of Nginx returns 302, the client is out of touch with the proxy of Nginx, and Nginx cannot play the role of a complete agent.
two。 The first revision
Modify the configuration file to:
Server {listen 80; location / {rewrite_by_lua 'ngx.exec ("/ proxy-to".. Ngx.var.request_uri)';} location ~ / proxy-to/ ([^ /] +) (. *) {proxy_pass http://$1$2$is_args$query_string; error_page 302 = @ error_page_302 } location @ error_page_302 {rewrite_by_lua 'local _, _, upstream_http_location = string.find (ngx.var.upstream_http_location, "^ http:/ (. *) $") ngx.header ["zzzz"] = "/ proxy-to".. Upstream_http_location ngx.exec ("/ proxy-to".. Upstream_http_location);';}}
The difference from the above is that an error_page is used so that when it is found that the back end of the proxy returns 302, the destination location of the 302 is used to continue the proxy instead of returning it directly to the client. And this logic contains the meaning of recursion, tracking 302 all the way to the address of 200. The test results are as follows:
Curl-I "http://192.168.109.128/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4"-LHTTP/1.1 302 Moved Temporarily Server: nginx/1.4.6 Date: Mon 25 Aug 2014 15:01:17 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive NG: CCN-SW-1-5L2 X-Mod-Name: GSLB/3.1.0 Location: http://183.61.140.24/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 HTTP/1.1 302Moved Temporarily Server: nginx Date: Mon 25 Aug 200 15:01:17 GMT Content-Type: text/html Content-Length: 154Connection: keep-alive X-Mod-Name: Mvod-Server/4.3.3 Location: http://183.61.140.20/a90a952900000000-1408984277-236096587/data1/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 NG: CHN-ZJ-4-3M4 HTTP/1.1 2014 OK Server: nginx Date: Mon 25 Aug 2014 15:01:17 GMT Content-Type: video/mp4 Content-Length: 3706468 Last-Modified: Mon, 25 Aug 2014 00:31:03 GMT Connection: keep-alive Cache-Control: no-cache ETag: "53fa83c7-388e64" NG: CHN-ZJ-4-3M4 X-Mod-Name: Mvod-Server/4.3.3 Accept-Ranges: bytes
It can be seen that this revision is still not successful!
Why? For analysis, we have added a header print statement to the @ error_page_302 location, but in the test, the header was not typed, which shows that the process did not enter the @ error_page_302 location.
The reason is that
Error_page 302 = @ error_page_302
Error_page is the error code for this processing by default. As a proxy, as long as the response from the upstream server is forwarded successfully, the status code should be 200. That is, what we really need to check is the status code returned by the proxy's back-end server, not the status code returned by the proxy itself. Check that this is what the Nginx wiki,proxy_intercept_errors instruction is for:
Syntax: proxy_intercept_errors on | off;Default: proxy_intercept_errors off;Context: http, server, locationDetermines whether proxied responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.
3. The second revision
Server {listen 80; proxy_intercept_errors on; location / {rewrite_by_lua 'ngx.exec ("/ proxy-to".. Ngx.var.request_uri)';} location ~ / proxy-to/ ([^ /] +) (. *) {proxy_pass http://$1$2$is_args$query_string; error_page 302 = @ error_page_302 } location @ error_page_302 {rewrite_by_lua 'local _, _, upstream_http_location = string.find (ngx.var.upstream_http_location, "^ http:/ (. *) $") ngx.header ["zzzz"] = "/ proxy-to".. Upstream_http_location ngx.exec ("/ proxy-to".. Upstream_http_location);';}}
Compared with the last modification, the only difference is the addition of a proxy_intercept_errors instruction. The test results are as follows:
Curl-I "http://192.168.109.128/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4"-L HTTP/1.1 302 Moved TemporarilyServer: nginx/1.4.6Date: Mon, 25 Aug 2014 15:05:54 GMTContent-Type: text/htmlContent-Length: 160Connection: keep-alivezzzz: / proxy-to/183.61.140.24/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4
It's even more amazing this time, just return to a 302 status and stop jumping.
The problem is that although the first 302 request successfully entered @ error_page_302, the subsequent error_page instruction did not work. That is, error_page only checks the status code returned by the back end for the first time, and does not continue to check the subsequent back end status code.
Check the data, and at this time, another instruction recursive_error_pages will come in handy.
4. The third revision
Server {listen 80; proxy_intercept_errors on; recursive_error_pages on; location / {rewrite_by_lua 'ngx.exec ("/ proxy-to".. Ngx.var.request_uri)';} location ~ / proxy-to/ ([^ /] +) (. *) {proxy_pass http://$1$2$is_args$query_string; error_page 302 = @ error_page_302 } location @ error_page_302 {rewrite_by_lua 'local _, _, upstream_http_location = string.find (ngx.var.upstream_http_location, "^ http:/ (. *) $") ngx.header ["zzzz"] = "/ proxy-to".. Upstream_http_location ngx.exec ("/ proxy-to".. Upstream_http_location);';}}
Compared with the last time, only the instruction recursive_error_pages on has been added. The test results are as follows:
Curl-I "http://192.168.109.128/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4"-L HTTP/1.1 200 OK Server: nginx/1.4.6 Date: Mon 25 Aug 2014 15:09:04 GMT Content-Type: video/mp4 Content-Length: 3706468 Connection: keep-alive zzzz: / proxy-to/14.18.140.83/f48bad0100000000-1408984745-236096587/data6/flv.bn.netease.com/tvmrepo/2014/8/5/P/EA3I1J05P/SD/EA3I1J05P-mobile.mp4 Last-Modified: Mon 25 Aug 2014 00:21:07 GMT Cache-Control: no-cache ETag: "53fa8173-388e64" NG: CHN-MM-4-3FE X-Mod-Name: Mvod-Server/4.3.3 Accept-Ranges: bytes
It can be seen that Nginx finally managed to return 200. At this point, Nginx really plays the function of a Proxy, hiding multiple 302 links of a request and returning only one final result on the client side.
This is the end of this article on "what to do when nginx agents make 302 errors many times". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.