Nginx Reverse Proxy and Load Balancing Detailed Explanation
Category Programming Technology
Preface
Nginx's proxy and load balancing features are the most commonly used. Basic syntax knowledge and configuration of nginx have been explained in Nginx Configuration Detailed Explanation. This article will dive straight in, first describing some configurations about proxy functions, and then explaining load balancing in detail.
Configuration Instructions for Nginx Proxy Service
Set the 404 page redirect address
error_page 404 https://www.runnob.com; #Error page proxy_intercept_errors on; #If the status code returned by the proxy server is 400 or greater, the configured error_page takes effect. Default is off.
If our proxy only accepts one of the get, post request methods
proxy_method get; #Support the client's request method. post/get;
Set the supported HTTP protocol version
proxy_http_version 1.0 ; #The HTTP protocol version 1.0, 1.1 provided by the Nginx server for proxy services, the default is set to version 1.0
If your Nginx server is acting as a proxy for two web servers, and the load balancing algorithm uses round-robin, then when one of your web applications iis is closed, that is, the web is not accessible, the Nginx server will still distribute requests to this inaccessible web server. If the response connection time here is too long, it will cause the client's page to keep waiting for a response, which greatly reduces the user experience. How do we avoid such a situation here? I will provide a diagram to illustrate the problem.
If a situation like this occurs in the load balancing with web2, Nginx will first request web1, but if Nginx is not properly configured, it will continue to distribute requests to web2, and then wait for web2 to respond, until our response time exceeds the timeout, and then the request will be redistributed to web1. If the response time here is too long, the longer the user will have to wait.
The following configuration is one of the solutions.
proxy_connect_timeout 1; #The timeout time for the Nginx server to establish a connection with the proxy server, the default is 60 seconds
proxy_read_timeout 1; #The timeout time for the Nginx server to send a read request to the proxy server group, waiting for the response, the default is 60 seconds.
proxy_send_timeout 1; #The timeout time for the Nginx server to send a write request to the proxy server group, waiting for the response, the default is 60 seconds.
proxy_ignore_client_abort on; #When the client's network is disconnected, whether the Nginx server ends the request to the proxy server. Default is off.
- If a group of servers is configured with the upstream directive as the proxy servers, the access algorithm of the servers follows the configured load balancing rules, and this directive can also be used to configure which exceptions will be handled by the next group of servers.
proxy_next_upstream timeout; #The status value returned by the proxy server when an error occurs in the server group set in the reverse proxy upstream.
The status values can be: error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
error: An error occurs when the server establishes a connection or sends a request or reads a response information to the proxy server.
timeout: An error occurs when the server establishes a connection, sends a request to the proxy server, or reads a response information.
invalid_header: The response header returned by the proxy server is abnormal.
off: Unable to distribute requests to the proxy server.
http_400, ....: The status code returned by the proxy server is 400, 500, 502, etc.
- If you want to obtain the real IP of the customer through http instead of obtaining the IP address of the proxy server, then the following settings need to be made.
``` proxy_set_header Host $host; #As long as the domain name visited by the user in the browser is bound to VIP VIP, there is RS below; then use $host; host is the domain name and port in the access URL www.taobao.com:80 proxy_set_header X-Real-IP $remote_addr; #Assign the source IP [$remote_addr, the information in the header when establishing HTTP connection] to X-Real-IP; in this way, $X-Real-IP is used in the code to obtain the source IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#When nginx Original link: https://www.cnblogs.com/knowledgesea/p/5199046.html
**Click to share my notes
-
-
-