Ningx acts as a Load balancer and is also a reverse proxy server. The configuration syntax is quite simple. It can be used to balance the backend servers by round robin, IP_hash, url_hash, weight, and other methods, it also supports backend server health check. In addition, it has advantages over LVS because it is based on Layer 7 Server Load balancer and executes Server Load balancer tasks based on information in the header, therefore, the network dependency is relatively small. Theoretically, load balancing can be achieved by ping. In China, Nginx is not only an excellent Load balancer, it is also a WEB application software suitable for high-concurrency environments. It has been applied to large websites such as Sina, Jinshan, and thunder online. Its advantages as a server Load balancer are as follows:
1. The configuration file is very simple: the style is as easy to understand as the program.
2. Low Cost: Nginx is open-source software and can be used for free. The purchase of F5 BIG-IP, NetScaler and other hardware Server Load balancer switches requires over 100,000 to several hundred thousand RMB.
3. Support for Rewrite Rules: You can allocate HTTP requests to different backend server groups based on different domain names and URLs.
4. built-in health check function: If a Web server on the Nginx Proxy backend is down, front-end access will not be affected.
5. bandwidth saving: GZIP compression is supported. You can add the Header cached locally by the browser.
6. High Stability: used for reverse proxy, the probability of downtime is minimal. In particular, by tracking some projects that have been launched, it is found that in the case of high concurrency, the number of times Nginx acts as the negative server balancer/reverse proxy goes down is almost zero.
The disadvantage is that only http and mail Server Load balancer is supported currently, but we can take it for a short time, mainly using it to support Rewrite Rules and features of high stability, load balancing of its intermediate layer.
The above quotes reference fuqin cooking wine!
--------------------------------------------------------------
Purpose:
1. Use the Nginx reverse proxy server to quickly access the background web server.
2. Implement load balancing and redundant backup for the backend web server group through Nginx Reverse Proxy Server
Preparations:
1. Independently deploy the web (www1.rsyslog.org) server on Apache and implement access (Brief Introduction)
Domain Name virtual hosts under Apache implement process http://dreamfire.blog.51cto.com/418026/163612 on the Home Directory
Automatically create an Apache Virtual Host http://dreamfire.blog.51cto.com/418026/1152672 with an interactive script
Access domain name resolution http://dreamfire.blog.51cto.com/418026/1133159 in different regions through DNS View
DNS configuration explanation 1 DNS related concepts understanding and basic configuration http://dreamfire.blog.51cto.com/418026/1091943
Configure a simple Apache VM:
[Root @ rhel6u3-4 ~] # Sed-I's/# NameVirtualHost/'/etc/httpd/conf/httpd. conf // remove # And start the VM Function
[Root @ rhel6u3-4 ~] # Cat>/etc/httpd/conf/httpd. conf <endf // Add a VM
> <VirtualHost *: 80>
> DocumentRoot/var/local/www
> ServerName www1.rsyslog.org
> </VirtualHost>
> Endf
[Root @ rhel6u3-4 ~] # Mkdir/var/local/www1 // create a VM directory
[Root @ rhel6u3-4 ~] # Echo "This is www1.rsyslog.org website">/var/local/www1/index.html // create a VM test page
[Root @ rhel6u3-4 ~] # Cat/var/local/www1/index.html
This is www1.rsyslog.org website
[Root @ rhel6u3-4 ~] #/Etc/rc. d/init. d/httpd restart // restart the httpd service
Stopping httpd: [OK]
Starting httpd: [OK]
[Root @ rhel6u3-4 ~] #
[Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone // Add A record to the DNS server
Www1 A 192.168.100.104
[Root @ rhel6u3-2 ~] #/Etc/rc. d/init. d/named restart // restart the DNS server to make the record take effect
Stopping named:. [OK]
Starting named: [OK]
[Root @ rhel6u3-4 ~] # Cat/etc/resolv. conf // Add the DNS address to the web server for testing
Nameserver 192.168.100.102
[Root @ rhel6u3-4 ~] # Nslookup www1.rsyslog.org // use nslookup to first resolve whether the domain name is OK
Server: 192.168.100.102
Address: 192.168.100.102 #53
Name: www1.rsyslog.org
Address: 192.168.100.104
[Root @ rhel6u3-4 ~] # Links-dump http://www1.rsyslog.org // use the links command to test whether the website can be accessed normally
This is www1.rsyslog.org website
[Root @ rhel6u3-4 ~] #
2. Deploy the web (www2.rsyslog.org) server on Nginx separately and implement access (Brief Introduction)
Nginx practice basics a source package compilation installation deployment web Server http://dreamfire.blog.51cto.com/418026/1140965
Implementation Process http://dreamfire.blog.51cto.com/418026/1141018 of virtual host on Nginx
// Nginx setup process omitted
[Root @ rhel6u3-5 ~] # Vim/usr/local/nginx/conf/nginx. conf // Add a VM to nginx. conf.
Http
.........
{
Server {
Listen 80;
Server_name www1.rsyslog.org;
Location /{
Root www1;
Index index.html index.htm;
}
}
.......
}
[Root @ rhel6u3-5 ~] # Mkdir/usr/local/nginx/www2 // create a VM directory
[Root @ rhel6u3-5 ~] # Echo "This is www2.rsyslog.org website">/usr/local/nginx/www2/index.html // create a test page
[Root @ rhel6u3-5 ~] #/Etc/rc. d/init. d/nginx restart // restart the nginx Server
Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK
Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful
Stopping nginx: [OK]
Starting nginx: [OK]
[Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone // Add A record to the dns server
Www2 A 192.168.100.105
[Root @ rhel6u3-4 ~] # Nslookup www2.rsyslog.org // test whether the domain name can be correctly resolved
Server: 192.168.100.102
Address: 192.168.100.102 #53
Name: www2.rsyslog.org
Address: 192.168.100.105
[Root @ rhel6u3-4 ~] # Links-dump http://www2.rsyslog.org // use the links command to test whether the website can be accessed normally
This is www2.rsyslog.org website
1,DeploymentNginxReverse Proxy Server for proxy and load balancing
NginxOfUpstreamCurrently supported5Method of allocation
1 round robin (default)
Each request is distributed to different backend servers one by one in chronological order. If the backend servers are down, they can be removed automatically.
2 weight
Specify the round-robin probability. weight is proportional to the access ratio, which is used when the backend server performance is uneven.
For example:
upstream bakend {
server 192.168.100.104 weight=10;
server 192.168.100.105 weight=10;
}
3ip_hash
Each request is allocated according to the hash result of the access ip address, so that each visitor accesses a backend server at a fixed time.
Solve the session problem.
For example:
upstream bakend {
ip_hash;
server 192.168.100.104:80;
server 192.168.100.105:80;
}
4 fair (third party)
Requests are allocated based on the response time of the backend server. Requests with short response time are prioritized.
For example:
upstream bakend {
server 192.168.100.104:80;
server 192.168.100.105:80;
fair;
}
5url_hash (third-party)
Distribute requests according to the hash result of the access url so that each url is directed to the same backend server.
It is effective when the server is cached.
For example:
upstream backend {
server 192.168.100.104:3128;
server 192.168.100.105:3128;
hash $request_uri;
hash_method crc32;
Set the status of each device:
1. down indicates that the server before a ticket is not involved in the load
2. The default weight value is 1. The larger the weight value, the higher the load weight, depending on the server performance.
3. max_fails: the default number of failed requests is 1. If the maximum number of failed requests is exceeded, an error defined by the proxy_next_upstream module is returned.
4. fail_timeout: The pause time after max_fails fails.
5. backup: Requests the backup machine when all other non-backup machines are down or busy. Therefore, this machine is under the least pressure.
Nginx supports setting multiple groups of server Load balancer instances for unused servers.
Nginx Installation Process
[Root @ rhel6u3 ~] # Vim/usr/local/nginx/conf/nginx. conf
......... // Content omitted
Http {
........ // Content omitted
Client_max_body_size 300 m; // The maximum number of bytes allowed by the client to request a single file. It appears in the Content-Length Field in the request header. (You can change this parameter to limit the size of uploaded files)
Client_body_buffer_size 128 k; \ maximum number of bytes of buffer client requests by the buffer proxy. It can be understood that the request is saved locally and then transmitted to the user. This command can specify the buffer size used by the connection request. Default Value: 8 k/16 k. If the client requests a file larger than 128 kb, Nginx will try to create a temporary file on the hard disk. If the hard disk is full, an error is returned.
Client_body_temp_path/dev/shm/client_body_temp; \ This command specifies the directory path in which connection requests attempt to write cache files.
Proxy_connect_timeout 600; \ timeout for connecting to the backend server, initiating a handshake and waiting for response timeout
Proxy_read_timeout 600; \ connection successful _ Wait for the response time of the backend server _ in fact, it has entered the backend queue for processing. Default Value:
Proxy_send_timeout 600; \ backend server return time _ indicates that the backend server must transmit all data within the specified time. Set the timeout time for the proxy server to forward requests. It also refers to the time after the two handshakes are completed. If no data is forwarded to the backend server after this time, nginx will close the connection.
Proxy_buffer_size 16 k; \ proxy request cache _ this cache interval stores the user's header information to provide Nginx for rule processing. Generally, you only need to save the following information. Default Value: proxy_buffer_size 4 k/8 k. Set the slow response of the first part read from the backend server
The size of the punch area. Generally, this part of the response contains a small response header.
Proxy_buffers 4 32 k; \ sets the number and size of the buffer used to read the response (from the backend server), telling Nginx to save the number
Buffer, maximum space used
Proxy_busy_buffers_size 64 k; \ if the system is busy, you can apply for a larger proxy_buffers. The official recommendation is * 2.
Proxy_temp_file_write_size 64 k; \ sets the size of the temporary file data cached when the proxy_temp_path is written to prevent a working process from being blocked too long when passing files.
Proxy_temp_path/dev/shm/proxy_temp; \\similar to the client_body_temp_path command in the http core module, specify a directory to buffer large proxy requests.
Upstream rsyslog.org _ pool {
Server 192.168.100.104: 80 weight = 4max_fails = 2fail_timeout = 30 s; // The default performance of both servers is the same for testing, with the average load
Server 192.168.100.105: 80 weight = 4max_fails = 2fail_timeout = 30 s;
} \ HTTP Server Load balancer module. Upstream sets a group of servers. You can place this field in the proxy_pass and fastcgi_pass commands as a separate entity. They can be servers listening to different ports, it can also be a server that listens to both TCP and Unix sockets. The server can specify different weights. The default value is 1.
Server {
Listen 80;
Server_name www.rsyslog.org; // set the common domain name for all web server Loads
Index index.html index.htm;
Location /{
Proxy_pass http://rsyslog.org _ pool/; // determine the URL, port, or socket to proxy.
Proxy_set_header Host $ host;
Proxy_redirect off; \ if you need to modify the "Location" and "Refresh" fields in the response header sent from the backend server, you can set this command.
Proxy_set_header X-Real-IP $ remote_addr; \ This command allows you to redefine or add some fields to the request header sent to the backend server. This value can be a text, variable, or a combination of them.
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; \ when the backend server encounters a 500, 502, 503, 504, 404 error and timeout, requests are automatically forwarded to another server in the rsyslog.org _ pool group for failover.
}
\\# Determine under which circumstances the request will be forwarded to the next server:
# Error-an error occurs when you connect to a server, send a request, or read the response.
# Timeout-timeout occurs when a server is connected, a request is forwarded, or a response is read.
# Invalid_header-the server returns an empty or incorrect response.
# Http_500-the server returns code 500.
# Http_502-the server returns code 502.
# Http_503-the server returns code 503.
# Http_504-the server returns code 504.
# Http_404-the server returns Code 404.
# Off-Prohibit forwarding requests to the next server.
}
}
[Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone
Www A 192.168.100.106
Test Results
This article is from the blog of "the Linux open source technology blog", please be sure to keep this source http://dreamfire.blog.51cto.com/418026/1158301