Nginx Load Balancer Configuration

Source: Internet
Author: User
Tags sendfile

1. Yum Installation Nginx

Yum Install Nginx

2. Start Nginx

Chkconfig nginx on service nginx start
To put the test file into the Web server:

[HTML]View Plaincopy print?
  1. <html>
  2. <head>
  3. <title>welcome to nginx! </title>
  4. </head>
  5. <body bgcolor="white" text="Black">
  6. <Center><H1>welcome to nginx! 192.168.232.132</H1></ Center>
  7. </body>
  8. </html>
To configure the Load Balancer server:

Vi/etc/nginx/nginx.conf

The contents are as follows:

[Plain]View Plaincopy print?
  1. User Nginx;
  2. Worker_processes 1;
  3. Error_log/var/log/nginx/error.log warn;
  4. Pid/var/run/nginx.pid;
  5. Events {
  6. Worker_connections 1024;
  7. }
  8. HTTP {
  9. Include/etc/nginx/mime.types;
  10. Default_type Application/octet-stream;
  11. Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '
  12. ' $status $body _bytes_sent ' $http _referer '
  13. ' "$http _user_agent" "$http _x_forwarded_for";
  14. Access_log/var/log/nginx/access.log main;
  15. Sendfile on;
  16. #tcp_nopush on;
  17. Keepalive_timeout 65;
  18. #gzip on;
  19. Upstream test.miaohr.com {
  20. Server 192.168.232.132:80;
  21. Server 192.168.232.133:80;
  22. }
  23. server {
  24. Listen 80;
  25. server_name test.miaohr.com;
  26. CharSet Utf-8;
  27. Location/{
  28. root HTML;
  29. Index index.html index.htm;
  30. Proxy_pass http://test.miaohr.com;
  31. Proxy_set_header X-real-ip $remote _addr;
  32. Client_max_body_size 100m;
  33. }
  34. Location ~ ^/(Web-inf)/{
  35. Deny all;
  36. }
  37. Error_page 502 503 504/50x.html;
  38. Location =/50x.html {
  39. root/var/www/html/;
  40. }
  41. }
  42. }
User nginx;worker_processes 1;error_log/var/log/nginx/error.log warn;pid/var/run/nginx.pid;events {worker_ Connections 1024;}    HTTP {include/etc/nginx/mime.types;    Default_type Application/octet-stream; Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_sent"    $http _referer "'" $http _user_agent "" $http _x_forwarded_for ";    Access_log/var/log/nginx/access.log main;    Sendfile on;    #tcp_nopush on;    Keepalive_timeout 65;    #gzip on;    Upstream test.miaohr.com {server 192.168.232.132:80;server 192.168.232.133:80;           } server {Listen 80;           server_name test.miaohr.com;           CharSet Utf-8;               Location/{root HTML;               Index index.html index.htm;               Proxy_pass http://test.miaohr.com;               Proxy_set_header X-real-ip $remote _addr; Client_max_Body_size 100m;            } Location-^/(Web-inf)/{deny all;           } error_page 502 503 504/50x.html;           Location =/50x.html {root/var/www/html/; }       }   }

The following browser opens: 192.168.232.134, if 132, 133 alternating display indicates the success of the experiment.

Expand:

1. Polling (default) each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.
2, weight Specify the polling probability, weight and access ratio is proportional to the performance of the backend server is uneven. For example: Upstream bakend {server 192.168.159.10 weight=10; server 192.168.159.11 weight=10;}
3, Ip_hash each request according to the hash result of access IP allocation, so that each visitor fixed access to a back-end server, you can solve the session problem. For example: Upstream resinserver{ip_hash; server 192.168.159.10:8080; server 192.168.159.11:8080;}
4, Fair (third party) according to the response time of the back-end server to allocate the request, response time is a short priority allocation. Upstream resinserver{server server1; server server2; fair;}
5. Url_hash (Third Party)
Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached. Example: Add a hash statement in upstream, the server statement can not write weight and other parameters, Hash_method is the use of the hash algorithm
Upstream resinserver{server squid1:3128; server squid2:3128; hash $request _uri; Hash_method Crc32;}
Tips
Upstream resinserver{#定义负载均衡设备的Ip及设备状态 ip_hash; server 127.0.0.1:8000 down; server 127.0.0.1:8080 weight=2; server 127.0.0.1:6801; Server 127.0.0.1:6802 backup; }
Add Proxy_pass http://resinserver/in servers that need to use load balancing;
The status of each device is set to: 1.down indicates that the server is temporarily not participating in the load 2.weight the default is 1.weight, the larger the load weight. 3.max_fails: The number of times that a request failed is allowed defaults to 1. When the maximum number of times is exceeded, returns the time that the Proxy_next_upstream module defines the error 4.fail_timeout:max_fails after the failure. 5.backup: When all other non-backup machines are down or busy, request the backup machine. So the pressure on this machine is the lightest.
Nginx supports multiple sets of load balancing at the same time, which is used for unused servers.
Client_body_in_file_only set to on can be said that the client post data logging to the file used to do debug Client_body_temp_path set record file directory can be set up to 3 levels directory location Matches the URL. Can redirect or perform new proxy load balancing

Nginx Load Balancer Configuration

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.