The reverse proxy can be divided into two types in transmission:
1: Synchronous mode (apache-mod_proxy and squid)
2: asynchronous mode (Lighttpd and nginx)
In the nginx documentation, I mentioned the asynchronous transmission mode and mentioned that it can reduce the number of backend connections and pressure. Why?
Next, let's take a look at the differences between synchronous transmission of traditional proxies (Apache/squid) and asynchronous transmission of Lighttpd and nginx.
Figure:
Synchronous transmission: the browser initiates a request, and the request is immediately forwarded to the background, so a channel is established between the browser and the background. This channel always exists when the request is initiated until the request is complete.
Asynchronous transmission: When a browser initiates a request, the request is not immediately forwarded to the background. Instead, the request data (header) is first received on nginx and then sent to the backend by nginx, after the backend completes processing, it returns the data to nginx. nginx sends the data stream to the browser, which is a little different from Lighttpd. Lighttpd sends the data to the browser only after the backend data is fully received.
Conclusion: The reverse direction of Apache and squid will increase the burden on the backend web, because each user request will establish a persistent connection with the backend server on the proxy, knowing that the connection will not disappear before the data is obtained. Because the WAN speed is different from the LAN speed, although the speed between the LAN is extremely fast, the user's WAN connection determines this time. The asynchronous mode of Lighttpd and nginx is to collect and contact the backend, regardless of the size of the data you require. This is very fast, therefore, the proxy and the backend connection time will be very short, and dozens of m items will also be within a few seconds. The backend does not need to maintain so many connections. Lighttpd is different from nginx in asynchronous mode. Lighttpd is switched to the customer's browser after receiving the data, while nginx is switched to the user's browser while receiving the data.
So what are the benefits?
1. Assume that the user uploads a file, because the user's network speed is slow, it takes half an hour to upload the file to the server. The squid synchronization proxy establishes a connection with the background after the user starts the upload. After half an hour, the file upload is complete. As a result, the connection to the background server remains for half an hour; the nginx asynchronous proxy first receives the file from nginx. Therefore, only nginx and the user have been connected for half an hour. The backend server did not enable the connection for this request within this half hour, after half an hour, nginx only sends the uploaded content to the background. The bandwidth between nginx and the background is abundant, so it takes only one second to send the request to the background, the connection to the backend server is retained for one second. Synchronous transmission takes half an hour on the backend server, while asynchronous transmission takes only one second. It can be seen that the optimization is very high.
2. in the above example, if the background server is restarted for various reasons, the file upload will be interrupted, which is a very annoying thing for users, I think you may also experience interrupted upload of half of files. After the nginx proxy is used, the restart of the background server has reduced the impact on user uploads to the extreme, while nginx is very stable and does not need to be restarted frequently, even if it needs to be restarted, you can use kill-HUP to continuously restart nginx.
3. asynchronous transmission can make the Load balancer more secure. Why? In other balancers (LVS, haproxy, and Apache), each request has only one chance. If a user initiates a request, the backend server to which the request is sent just crashes, therefore, this request fails. nginx is asynchronous, so this request can be sent back to the next backend, And the next backend returns normal data. Therefore, this request is successful. In this example, we still use the user to upload files. If we use not only the nginx proxy but also the Server Load balancer, nginx will send the uploaded files to one of the backend servers, but the server suddenly restarts, after nginx receives an error, it will send the upload file to another background, so the user does not need to upload it again in half an hour.
4. If the user uploads a 10 Gb file and the backend server does not consider this situation, the backend server will not crash. With nginx, you can block all these things on nginx and use the nginx Upload File size restriction function. In addition, nginx performance is very guaranteed, let's rest assured that the alternative users on the internet can confront nginx.
Asynchronous transmission may cause problems:
If the backend server provides the upload progress function, it cannot get the progress with the nginx proxy. This requires a third-party module of nginx.
You are welcome to share your experience.
The reverse proxy can be divided into two types in transmission:
1: Synchronous mode (apache-mod_proxy and squid)
2: asynchronous mode (Lighttpd and nginx)
In the nginx documentation, I mentioned the asynchronous transmission mode and mentioned that it can reduce the number of backend connections and pressure. Why?
Next, let's take a look at the differences between synchronous transmission of traditional proxies (Apache/squid) and asynchronous transmission of Lighttpd and nginx.
Figure:
Synchronous transmission: the browser initiates a request, and the request is immediately forwarded to the background, so a channel is established between the browser and the background. This channel always exists when the request is initiated until the request is complete.
Asynchronous transmission: When a browser initiates a request, the request is not immediately forwarded to the background. Instead, the request data (header) is first received on nginx and then sent to the backend by nginx, after the backend completes processing, it returns the data to nginx. nginx sends the data stream to the browser, which is a little different from Lighttpd. Lighttpd sends the data to the browser only after the backend data is fully received.
Conclusion: The reverse direction of Apache and squid will increase the burden on the backend web, because each user request will establish a persistent connection with the backend server on the proxy, knowing that the connection will not disappear before the data is obtained. Because the WAN speed is different from the LAN speed, although the speed between the LAN is extremely fast, the user's WAN connection determines this time. The asynchronous mode of Lighttpd and nginx is to collect and contact the backend, regardless of the size of the data you require. This is very fast, therefore, the proxy and the backend connection time will be very short, and dozens of m items will also be within a few seconds. The backend does not need to maintain so many connections. Lighttpd is different from nginx in asynchronous mode. Lighttpd is switched to the customer's browser after receiving the data, while nginx is switched to the user's browser while receiving the data.
So what are the benefits?
1. Assume that the user uploads a file, because the user's network speed is slow, it takes half an hour to upload the file to the server. The squid synchronization proxy establishes a connection with the background after the user starts the upload. After half an hour, the file upload is complete. As a result, the connection to the background server remains for half an hour; the nginx asynchronous proxy first receives the file from nginx. Therefore, only nginx and the user have been connected for half an hour. The backend server did not enable the connection for this request within this half hour, after half an hour, nginx only sends the uploaded content to the background. The bandwidth between nginx and the background is abundant, so it takes only one second to send the request to the background, the connection to the backend server is retained for one second. Synchronous transmission takes half an hour on the backend server, while asynchronous transmission takes only one second. It can be seen that the optimization is very high.
2. in the above example, if the background server is restarted for various reasons, the file upload will be interrupted, which is a very annoying thing for users, I think you may also experience interrupted upload of half of files. After the nginx proxy is used, the restart of the background server has reduced the impact on user uploads to the extreme, while nginx is very stable and does not need to be restarted frequently, even if it needs to be restarted, you can use kill-HUP to continuously restart nginx.
3. asynchronous transmission can make the Load balancer more secure. Why? In other balancers (LVS, haproxy, and Apache), each request has only one chance. If a user initiates a request, the backend server to which the request is sent just crashes, therefore, this request fails. nginx is asynchronous, so this request can be sent back to the next backend, And the next backend returns normal data. Therefore, this request is successful. In this example, we still use the user to upload files. If we use not only the nginx proxy but also the Server Load balancer, nginx will send the uploaded files to one of the backend servers, but the server suddenly restarts, after nginx receives an error, it will send the upload file to another background, so the user does not need to upload it again in half an hour.
4. If the user uploads a 10 Gb file and the backend server does not consider this situation, the backend server will not crash. With nginx, you can block all these things on nginx and use the nginx Upload File size restriction function. In addition, nginx performance is very guaranteed, let's rest assured that the alternative users on the internet can confront nginx.
Asynchronous transmission may cause problems:
If the backend server provides the upload progress function, it cannot get the progress with the nginx proxy. This requires a third-party module of nginx.