Deployment of Nginx reverse proxy and Server Load balancer in practice

Source: Internet
Author: User
Tags nslookup nginx reverse proxy rsyslog

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:


 
 
  1. [Root @ rhel6u3-4 ~] # Sed-I's/# NameVirtualHost/'/etc/httpd/conf/httpd. conf // remove # And start the VM Function

  2. [Root @ rhel6u3-4 ~] # Cat>/etc/httpd/conf/httpd. conf <endf // Add a VM

  3. > <VirtualHost *: 80>

  4. > DocumentRoot/var/local/www

  5. > ServerName www1.rsyslog.org

  6. > </VirtualHost>

  7. > Endf

  8. [Root @ rhel6u3-4 ~] # Mkdir/var/local/www1 // create a VM directory

  9. [Root @ rhel6u3-4 ~] # Echo "This is www1.rsyslog.org website">/var/local/www1/index.html // create a VM test page

  10. [Root @ rhel6u3-4 ~] # Cat/var/local/www1/index.html

  11. This is www1.rsyslog.org website

  12. [Root @ rhel6u3-4 ~] #/Etc/rc. d/init. d/httpd restart // restart the httpd service

  13. Stopping httpd: [OK]

  14. Starting httpd: [OK]

  15. [Root @ rhel6u3-4 ~] #

  16. [Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone // Add A record to the DNS server

  17. Www1 A 192.168.100.104

  18. [Root @ rhel6u3-2 ~] #/Etc/rc. d/init. d/named restart // restart the DNS server to make the record take effect

  19. Stopping named:. [OK]

  20. Starting named: [OK]

  21. [Root @ rhel6u3-4 ~] # Cat/etc/resolv. conf // Add the DNS address to the web server for testing

  22. Nameserver 192.168.100.102

  23. [Root @ rhel6u3-4 ~] # Nslookup www1.rsyslog.org // use nslookup to first resolve whether the domain name is OK

  24. Server: 192.168.100.102

  25. Address: 192.168.100.102 #53

  26. Name: www1.rsyslog.org

  27. Address: 192.168.100.104

  28. [Root @ rhel6u3-4 ~] # Links-dump http://www1.rsyslog.org // use the links command to test whether the website can be accessed normally

  29. This is www1.rsyslog.org website

  30. [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


 
 
  1. // Nginx setup process omitted

  2. [Root @ rhel6u3-5 ~] # Vim/usr/local/nginx/conf/nginx. conf // Add a VM to nginx. conf.

  3. Http

  4. .........

  5. {

  6. Server {

  7. Listen 80;

  8. Server_name www1.rsyslog.org;


  9. Location /{

  10. Root www1;

  11. Index index.html index.htm;

  12. }


  13. }

  14. .......

  15. }

  16. [Root @ rhel6u3-5 ~] # Mkdir/usr/local/nginx/www2 // create a VM directory

  17. [Root @ rhel6u3-5 ~] # Echo "This is www2.rsyslog.org website">/usr/local/nginx/www2/index.html // create a test page

  18. [Root @ rhel6u3-5 ~] #/Etc/rc. d/init. d/nginx restart // restart the nginx Server

  19. Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK

  20. Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful

  21. Stopping nginx: [OK]

  22. Starting nginx: [OK]


  23. [Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone // Add A record to the dns server

  24. Www2 A 192.168.100.105


  25. [Root @ rhel6u3-4 ~] # Nslookup www2.rsyslog.org // test whether the domain name can be correctly resolved

  26. Server: 192.168.100.102

  27. Address: 192.168.100.102 #53


  28. Name: www2.rsyslog.org

  29. Address: 192.168.100.105


  30. [Root @ rhel6u3-4 ~] # Links-dump http://www2.rsyslog.org // use the links command to test whether the website can be accessed normally

  31. 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:


 
 
  1. upstream bakend {

  2. server 192.168.100.104 weight=10;

  3. server 192.168.100.105 weight=10;

  4. }


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:


 
 
  1. upstream bakend {

  2. ip_hash;

  3. server 192.168.100.104:80;

  4. server 192.168.100.105:80;

  5. }


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:


 
 
  1. upstream bakend {

  2. server 192.168.100.104:80;

  3. server 192.168.100.105:80;

  4. fair;

  5. }


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:


 
 
  1. upstream backend {

  2. server 192.168.100.104:3128;

  3. server 192.168.100.105:3128;

  4. hash $request_uri;

  5. 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.


 
 
  1. Nginx Installation Process

  2. [Root @ rhel6u3 ~] # Vim/usr/local/nginx/conf/nginx. conf

  3. ......... // Content omitted

  4. Http {

  5. ........ // Content omitted

  6. 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)

  7. 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.

  8. Client_body_temp_path/dev/shm/client_body_temp; \ This command specifies the directory path in which connection requests attempt to write cache files.

  9. Proxy_connect_timeout 600; \ timeout for connecting to the backend server, initiating a handshake and waiting for response timeout

  10. 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:

  11. 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.

  12. 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

  13. The size of the punch area. Generally, this part of the response contains a small response header.

  14. 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

  15. Buffer, maximum space used

  16. Proxy_busy_buffers_size 64 k; \ if the system is busy, you can apply for a larger proxy_buffers. The official recommendation is * 2.

  17. 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.

  18. 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.


  19. Upstream rsyslog.org _ pool {

  20. 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

  21. Server 192.168.100.105: 80 weight = 4max_fails = 2fail_timeout = 30 s;

  22. } \ 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.


  23. Server {

  24. Listen 80;

  25. Server_name www.rsyslog.org; // set the common domain name for all web server Loads

  26. Index index.html index.htm;


  27. Location /{

  28. Proxy_pass http://rsyslog.org _ pool/; // determine the URL, port, or socket to proxy.

  29. Proxy_set_header Host $ host;

  30. 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.

  31. 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.

  32. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

  33. 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.

  34. }

  35. \\# Determine under which circumstances the request will be forwarded to the next server:

  36. # Error-an error occurs when you connect to a server, send a request, or read the response.

  37. # Timeout-timeout occurs when a server is connected, a request is forwarded, or a response is read.

  38. # Invalid_header-the server returns an empty or incorrect response.

  39. # Http_500-the server returns code 500.

  40. # Http_502-the server returns code 502.

  41. # Http_503-the server returns code 503.

  42. # Http_504-the server returns code 504.

  43. # Http_404-the server returns Code 404.

  44. # Off-Prohibit forwarding requests to the next server.

  45. }

  46. }


  47. [Root @ rhel6u3-2 ~] # Vim/var/named/view/zone/Others.rsyslog.org. zone

  48. 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


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.