Load balancing using Nginx This paper configures Nginx to implement load under Windows and Linux _linux

Source: Internet
Author: User
Tags bz2 epoll nginx server vshare nginx reverse proxy nginx load balancing

There are two ways to implement the site load, one is to buy hardware to achieve, such as hardware F5 to Citrix Netscalar, these devices are hundreds of thousands of, not the average person to play, the other is to use software to achieve, such as Nginx, Squid This kind has the reverse proxy function The software, this article Nginx installs realizes the load.

First is the Windows system, it is recommended to use Window 2003 Enterprise Edition instead of Win7 (too new, I encountered a problem that could not start nginx). To say, configure the installation Nginx under Windows is easy, less linux download tar and configure the compilation parameters, and so on, we simply download the corresponding Zip package (about 750kb) from the following address:


It should be explained that this download package is a Nginx 0.8.21 development version, in other words, it can only be used to build a test environment, and cannot be used in the actual production environment (limited by the number of Windows file handles).

Download and extract the files from the package to C disk, in order to configure the use of convenience, I will extract the folder name from "nginx-0.8.19" to "Nginx", so that we as long as the load-balanced site in IIS set good, Place the corresponding link address in the appropriate configuration file for Nginx, where we open the C:\nginx\conf \nginx.conf file and place the following in the line above the file's "server {":

Upstream Mylocalsite {

Because Nginx cannot run a dynamic script, you use the Proxy_pass property to broker this, so find the following content for the configuration file:

root HTML;
Index index.html index.htm;

Modify its contents as follows:

Proxy_pass Http://mylocalsite;

After the modification, you also need to modify the server's listening port, the original contents are as follows:

server {
Listen 80;
server_name localhost;

After the modified content is as follows:

server {
Listen 8086;

In this way, Nginx starts listening for a local IP ( 8086 port request after startup, and then moves its request to the two IIS sites specified in mylocalsite and forwards the results of the execution to the client. If everything is configured correctly, then you can run C:/nginx/nginx.exe (or run "start Nginx" under cmd) and you can see a nginx process started in Task Manager. (Note: If there are errors in the configuration file, you can check the error log to C:\nginx\logs\error.log for further scheduling errors).

Note: Turn off the Ngnix command: nginx-s stop

Config file ngnix.conf correctness judgment command: nginx-t

Of course Nginx load Balancing function is also very strong, and it is generally as a seven-tier load balancing (Application protocol layer). Here is a description of the four common settings that are supported by its upstream:

1), polling (default): Each request in chronological order allocated to different back-end servers, if the back-end server down, can be automatically removed.
2), Weight: Specify the polling probability, weight and access ratio is proportional to the performance of the backend server.
2, Ip_hash: Each request according to the Access IP hash result allocation, so each visitor fixed access to a back-end server, can solve the session problem.
3), Fair (third party): According to the response time of the backend server to allocate requests, short response time priority allocation.
4), Url_hash (third party)

As indicated above, we can make the following modifications to our upstream:

Upstream Mylocalsite {
Server weight=2;
Server weight=1;

This when three HTTP requests are made, two of them are assigned to the and one is assigned to Of course, at first, the requests that can be apportioned can be set not too rigidly, but when the number of requests is much more, it is almost as close to our assigned weights. Of course, in the load-balancing algorithm, the use of weight is only one of them, and often used many, such as LVS in its internal implementation of a variety of load balancing algorithm , according to the user's actual environment to set up the department. Of course, at present, the Nginx algorithm is still a lot less, hehe.

It can be said that it is convenient to install the configuration under Windows, but if you use LoadRunner to do concurrent testing, you will find that its Logs/error.log will report the following error:

Maximum number of descriptors supported by select () are 1024 while connecting to upstream

I have checked the solution on the Internet, including modifying the worker_connections of the configuration file, as follows:

Worker_rlimit_nofile 20240;
Events {
Use #use Epoll;//linux
Worker_connections 20240;

However, it is still not possible to extend the number of file handles to 20240 or to report 1024 maximum number of handle errors. Finally there is no way, only to start to try to install the configuration Nginx Linux. The following content is also raised.

In fact, on the Internet to introduce how to install Nginx Linux under the more than Windows, it must be a ' clan ' bar.

Since the network administrator installed the CentOS5 only on the virtual machine, it was only possible to attempt to install it under the Linux branch version. Fortunately, the virtual machine has been installed, the rest of the work is not too much.

First you need to log into the system as root, and then switch identities to Super admin:

Then go to SRC under current, and under the current directory download nginx.tar.gz pack

CD/USR/SRC # Download files to this directory
wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz# Download installation package
Tar xzvf nginx-0.6.34.tar.gz #解压

If Rewirte rules may not be available by default in the downloaded Nginx, the extension of the Pcre package needs to be downloaded to achieve this functionality:

wget http://syslab.comsenz.com/downloads/linux/pcre-7.8.tar.bz2 # Download Pcre
Tar xjvf pcre-7.8.tar.bz2 # decompression Pcre

The following compiles the installation Pcre


The following compiles the installation Nginx

Start configuring the parameters to compile (note: The content is long, easy to lose error.) Specific parameter settings See http://wiki.codemongers.com/NginxChsInstall)

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/ nginx/conf/nginx.conf
--error-log-path=/usr/local/nginx/logs/error.log--http-log-path=/usr/local/nginx/logs /access.log
-- Http-client-body-temp-path=/dev/shm/nginx_temp/client_body--http-proxy-temp-path=/dev/shm/nginx_temp/proxy
--user=www--group=www--with-cpu-opt=pentium4-- Without-select_module--without-poll_module
--with-http_realip_module--with-http_sub_module--with-http_gzip_ Static_module--with-http_stub_status_module
--without-http_ssi_module--without-http_userid_module-- Without-http_geo_module--without-http_memcached_module
--without-http_map_module "#如要取消ssl可去掉该项
-- Without-mail_pop3_module--without-mail_imap_modul--without-mail_smtp_module--with-pcre=/usr/local/pcre/lib

The next step is to start compiling the resulting file:

Make install

Then/DEV/SHM is in memory, creating a Nginx_temp folder

Then create the WWW users and groups, and the directories they use:

/usr/sbin/groupadd Www-g 48
/usr/sbin/useradd-u 48-g www www
chmod +w/data0/vshare/htdocs
Chown-r Www:www/data0/vshare/htdocs

At this point you can expand the number of file handles (Windows is not so easy to expand, eh)

Here, we can use the Linux under the VI Editor Editor:

VI nginx.conf

Replace the previous changes under window (press Insert to enter into edit mode) to the current file, when the modification is complete, press the colon (":") to switch to command mode, and then type "WQ" to save and exit. (Force exit (not saved), enter q!, then return)


Events {
Use Epoll;
Worker_connections 20240;

Note: Use Epoll; Linux use, more content see nginxchsoptimizations

This will allow you to run the Nginx:

After startup is complete, you can view its running information in memory by following these instructions:

# PS aux | egrep ' (pid|nginx) '

So when we use the LoadRunner run again, we can see that the annoying "1024 error" is not reported in the Error.log.

Of course, in Nginx, file caching is also supported so that the static files can be cached to a local nginx server, except to modify their config file configuration as follows:

VI nginx.conf

Enter the following at the appropriate node in the file:

--> Location ~. *\. (GIF|JPG|JPEG|PNG|BMP|SWF|JS|HTML|HTM|CSS) $ {#指定缓存文件类型
Expires 7d; # set Browser expiration Time 7 days
Root Data/nginx_cache/iis; #静态文件根目录目录 (must correspond to Proxy_temp_path)
Proxy_store on; #开启缓存机制
Proxy_store_access USER:RW GROUP:RW ALL:RW; # cache Read and write rules
Proxy_temp_path Data/nginx_cache/iis; #存放静态文件的缓存目录
# include proxy.conf; # Detailed configuration of the external proxy such as Proxy_set_header, client_max_body_size.
if (!-e $request _filename) {

This will generate temporary information for files such as gif,jpg in Data/nginx_cache/iis, and will be returned to the client when a client request arrives to reduce the pressure on the IIS server and network bandwidth when the corresponding file bindings are retrieved from the directory.

Finally, I attach a configuration file on that CentOS, you can compare reference, because the nginx itself provides too much configuration node information, more information can see this article .

RELATED links are as follows:

Zhang Yi Build a Web server 10 times times better than Apache (5th edition) [Original]

Hold each day (net name) Nginx reverse proxy configuration and optimization

Original link: http://www.cnblogs.com/daizhj/archive/2009/11/03/1595292.html

Author: Daizhj, the generation of earthquake troops

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.