English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Introduction
The proxy function and load balancing function of Nginx are the most commonly used. The basic grammar and configuration of nginx have been explained in the previous article. This article will cut to the chase and describe some configuration related to the proxy function first, and then explain the detailed load balancing.
Configuration instructions for Nginx proxy service
1In the previous article, we had the following configuration in the http module, when the proxy encounters a status code of404When404Page redirection to Baidu.
error_page 404 https://www.baidu.com; #Error page
However, this configuration, as careful friends may have noticed, does not take effect.
If we want it to take effect, we must use the following configuration together
proxy_intercept_errors on; #If the status code returned by the proxy server is400 or greater400, the error_page configuration set takes effect. The default is off.
2If our proxy only allows the acceptance of get, post request methods
proxy_method get; #Supports the request methods of the client. post/get;
3Set the supported http protocol version
proxy_http_version 1.0 ; #The http protocol version provided by the nginx server for proxy services1.0,1.1is set to1.0 version
4If your nginx server gives2web server as a proxy, and the load balancing algorithm uses round-robin, then when one of your machine's web program iis is closed, that is, the web cannot be accessed, then 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 the response, which will greatly reduce the user experience. How can we avoid such a situation? Here I will illustrate the problem with a picture.
If one of the web servers in the load balancing2In such cases, nginx will first go to web1request, but nginx will continue to distribute requests to web if the configuration is incorrect2Then wait for web2Only after our response time exceeds the timeout will the request be redistributed to web1If 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 for the nginx server to establish a connection with the proxy server, the default60 seconds proxy_read_timeout 1; #The timeout for the nginx server to wait for a response after sending a read request to the group of proxy servers, the default is60 seconds. proxy_send_timeout 1; #The timeout for the nginx server to wait for a response after sending a write request to the group of proxy servers, the default is60 seconds. proxy_ignore_client_abort on; #Whether the nginx server terminates the request to the proxy server when the client is disconnected. The default is off.
5、If you use the upstream instruction to configure a group of servers as the proxy server, the access algorithm in the server follows the configured load balancing rules, and you can also use this instruction to configure which exceptions will cause the request to be sequentially processed by the next group of servers.
proxy_next_upstream timeout; #The status value returned by the proxy server when the server group set with the upstream instruction fails. error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
6、If you want to get the real IP of the customer through http instead of getting the IP address of the proxy server, then you need to make the following settings.
proxy_set_header Host $host; #If the domain accessed by the user in the browser is bound to VIP VIP, and there is RS below, then use $host;The host is the domain 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 HTTP connection header】to X-Real-IP;So in the code, $X-Real-IP to obtain the source IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#When nginx is used as a proxy server, the IP list set will record the IP of the machine passing through and the IP of the proxy machine, separated by 【,】;In the code, use echo $x-forwarded-for |awk -F, '{print $1}' as the source IP
About X-Forwarded-For and X-Real-I recommend a blogger's related articles about IP: X in the HTTP request header-Forwarded-For ,this blogger has a series of articles explaining the http protocol, and I recommend everyone to pay attention to them.
7Here is a part of my configuration file about proxy settings for reference only.
include mime.types; # File extension to file type mapping table default_type application/octet-stream; #Default file type, the default is text/plain #access_log off; #Disable service logging log_format myFormat ' $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #Custom format access_log log/access.log myFormat; #combined is the default log format sendfile on; #Allow file transfer using sendfile method, the default is off, which can be set in the http block, server block, and location block. sendfile_max_chunk 100k; #The number of bytes that each process can transfer in each call cannot exceed the set value, the default is 0, which means no limit is set. keepalive_timeout 65; #The default connection timeout is75seconds, which can be set in the http, server, and location blocks. proxy_connect_timeout 1; #The timeout for the nginx server to establish a connection with the proxy server, the default60 seconds proxy_read_timeout 1; #The timeout for the nginx server to wait for a response after sending a read request to the group of proxy servers, the default is60 seconds. proxy_send_timeout 1; #The timeout for the nginx server to wait for a response after sending a write request to the group of proxy servers, the default is60 seconds. proxy_http_version 1.0 ; #The http protocol version provided by the nginx server for proxy services1.0,1.1is set to1.0 version. #proxy_method get; #Supports the request methods of the client. post/get; proxy_ignore_client_abort on; #Whether the nginx server terminates the request to the proxy server when the client is disconnected. The default is off. proxy_ignore_headers "Expires" "Set-Cookie"; #Nginx server does not process the header fields set in the http response, multiple can be set separated by spaces. proxy_intercept_errors on; #If the status code returned by the proxy server is400 or greater400, the error_page configuration set takes effect. The default is off. proxy_headers_hash_max_size 1024; # The maximum capacity of the hash table used to store HTTP headers, the default is512characters. proxy_headers_hash_bucket_size 128; # The capacity of the hash table used by the Nginx server to store HTTP headers. The default is64characters. proxy_next_upstream timeout; # The status value returned by the proxied server when a fault occurs in the upstream server group set for reverse proxy, error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off #proxy_ssl_session_reuse on; The default is on, if we find 'SSL3When the situation of '_GET_FINSHED: digest check failed' occurs, this instruction can be set to off.
Detailed Explanation of Nginx Load Balancing
In the previous article, I mentioned what load balancing algorithms Nginx has. In this section, I will give a detailed explanation of the operation configuration.
First, let me explain the upstream configuration to everyone, which is to write a group of server addresses to be proxied, and then configure the load balancing algorithm. The server addresses to be proxied here include2writing method.
upstream mysvr { server 192.168.10.121:3333 server 192.168.10.122:3333 server { .... location ~*^.+$ { proxy_pass http://mysvr; # Redirect requests to the server list defined by mysvr
upstream mysvr { server http://192.168.10.121:3333 server http://192.168.10.122:3333 server { .... location ~*^.+$ { proxy_pass mysvr; # Redirect requests to the server list defined by mysvr
Then, let's get into some practical stuff.
1、Hot standby: If you have2server, when one server fails, the second server is only enabled to provide services. The order in which the server processes requests: AAAAA suddenly A is down, BBBBBBBBBBBBBB.....
upstream mysvr { server 127.0.0.1:7878 server 192.168.10.121:3333 backup; # Hot standby
2、Round-robin: Nginx defaults to round-robin, with all weights set to1The order in which the server processes requests: ABABABABAB....
upstream mysvr { server 127.0.0.1:7878 server 192.168.10.121:3333
3、Weighted round-robin: Distributes different numbers of requests to different servers according to the size of the weights configured. If not set, the default is1The request order of the following servers is: ABBABBABBABBABB....
upstream mysvr { server 127.0.0.1:7878 weight=1 server 192.168.10.121:3333 weight=2
4、ip_hash: Nginx makes the same client IP request the same server.
upstream mysvr { server 127.0.0.1:7878 server 192.168.10.121:3333 ip_hash;
5、If you are referring to the above4I'm not very clear about the load balancing algorithm, so please take a look at the picture I configured in my last article, which may be easier to understand.
Do you feel that nginx's load balancing configuration is especially simple and powerful at this point? Then it's not over yet, let's continue, and here we go, here's the egg.
Several status parameters for explaining nginx load balancing configuration.
upstream mysvr { server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2 server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1
It should be said that nginx's built-in load balancing algorithm is out of stock here. If you want to learn more about nginx's load balancing algorithm, you can check out some plugins provided by the nginx official website.
That's all for this article. I hope it will be helpful to everyone's learning and I also hope everyone will support the Yelling Tutorial more.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.