Explanation of the whole process of HTTP request processing when nginx is used as a reverse proxy

Source: Internet
Author: User
Tags nginx reverse proxy

I used nginx as the reverse proxy to proxy Baidu (www.baidu.com) and modified the host file locally during the test. Resolve www.baidu.com to the nginx Proxy Server (74.91.23.207 ).

# Vim/etc/hosts

Next let's take a look at the nginx configuration file segment (where: Server 115.239.210.27 is Baidu's server address)

Next, enter www.baidu.com in the browser and send an HTTP request. Ping it to see that it is a request sent to 74.91.23.207.

So how does nginx handle the request after it reaches 74.91.23.207?

Take a look at the wireshark packet (fliter: IP. src = 192.168.100.11 & HTTP. Host = "www.baidu.com" & HTTP. Request. Method = "get ")

Based on the above get package data explanation:

1. When an HTTP request is sent to nginx for processing, nginx will first retrieve the host (www.baidu.com) in the header, and thenAll configuration filesTo determine which server block to process the request. (Of course, sometimes a host may match SERVER_NAME in multiple server blocks. In this case, the server block actually processed will be selected based on the matching priority. The priority will not be detailed here .)

2. at this point, we can see that the HTTP request matches SERVER_NAME, so that nginx will match the location according to the request URI field in the header, as shown in the preceding configuration file. Location/is matched /.

3. Next, we can see the proxy _ pass http: // rocdn through nginx. This is reverse proxy processing. In this example, we use the nginx upstream module for reverse proxy implementation.

4. You can find the corresponding upstream block through the rocdn in the proxy _ pass http: // rocdn;, and then perform another proxy request based on the server 115.239.210.27: 80,

The content of the get packet sent: The Host field and request URI field are the same as those above,

5. after the current HTTP request arrives at Baidu's server 115.239.210.27, if Baidu is also nginx, the server will perform SERVER_NAME and location matching like the above nginx handles HTTP requests, return the requested resource to the proxy server.

6. The nginx reverse proxy server receives the resources returned by the Baidu server and then returns them to the client browser.

The above is the general process of HTTP request and processing when nginx acts as the response proxy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.