Play Load Balancer---Configure Nginx under Windows and Linux

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

Some time recently, we started touching on load balancing, from hardware F5 to Citrix Netscalar. However, because the hardware configuration is not complicated, but the expensive price also makes the general user prohibitive (a hundred thousand of to hundreds of thousands of), so can only turn to nginx,squid this kind has the reverse proxy function software. Fortunately its settings are not very troublesome.

This article on the previous installation and configuration steps to do a summary share, so as not to forget the future.
The first is the Windows system, it is recommended to use Window 2003 Enterprise Edition, but not the role of Win7 (too new, I encountered the problem of unable to start Nginx). To say, under Windows to configure the installation Nginx is still very easy, less linux under the download of the tar and then configure the compilation parameters and so on, we just download the corresponding ZIP package from the following address (about 750KB), the address is as follows:

Http://sysoev.ru/nginx/nginx-0.8.21.zip


It is necessary to note that this download is an Nginx 0.8.21 development, in other words, it can only be used for the purpose of building a test environment, not for the actual production environment (limited by the number of Windows file handle limit).

Download and extract the files in the package to the C drive, in order to configure the use of convenience, I extracted the folder name from "nginx-0.8.19" to "Nginx", so that we will be load-balanced site in IIS after setting up, Put the corresponding link address in the corresponding configuration file of Nginx, here we open the C:\nginx\conf\nginx.conf file, put the following content to the file "server {" Above the line:

Upstream Mylocalsite {
Server 10.0.2.137:8088;
Server 10.0.2.137:8089;
}

Because Nginx cannot run the dynamic script, the Proxy_pass property is used here to proxy, so find the following in the configuration file:

Location/{
root HTML;
Index index.html index.htm;
}

Modify its contents as follows:

Location/{
Proxy_pass Http://mylocalsite;
#proxy_set_header Host $host;
#proxy_set_header X-real-ip $remote _addr; ; #防止ajax安全请求问题
#proxy_set_header remote-host $remote _addr;; #防止ajax安全请求问题
}

After the modification, also modify the server listening port, the original content is as follows:

server {
Listen 80;
server_name localhost;
......

The following changes are completed:

server {
Listen 8086;
server_name 10.0.2.136;
......

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

Note: The command to close Ngnix: nginx-s Stop

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

Of course, the function of Nginx load Balancing is also very strong, and it is generally used as a seven-layer load Balancer (Application protocol layer). The following is a description of the four common settings supported by upstream:

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: Specifies the polling probability, the weight and the access ratio are proportional to the performance of the backend server is uneven.
2), 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.
3), fair (third-party): Allocates requests by the response time of the backend server, with a short response time priority allocation.
4), Url_hash (third party)

As described above, we can make the following changes to our upstream:

Upstream Mylocalsite {
#ip_hash;
Server 10.0.2.137:8088 weight=2;
Server 10.0.2.137:8089 weight=1;
}

This when three HTTP requests are made, two of them are assigned to 10.0.2.137:8088 and one is assigned to 10.0.2.137:8089. Of course, at the outset, the request can be assessed in a way that is not too strictly set, but when the number of requests is large, it is basically close to the weighted value we have assigned. Of course, the load-balancing algorithm, using weight is only one of them, and often use 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 deployment. Of course, at present, Nginx algorithm compared to a lot less, hehe.

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

Maximum number of descriptors supported by select () is 1024x768 and 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 under #use epoll;//linux
Worker_connections 20240;
}

However, it is still not possible to expand the number of file handles to 20240, or to report 1024 the maximum number of handle errors. Finally there is no way, have to start trying to install the configuration Nginx under Linux. It also raises the following content.

In fact, on the internet about how to install Nginx under Linux is much more than Windows, it must be ' clan ' bar.

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

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

Sudo-s # Become a Super Admin first

Then enter into the SRC current under and under the current directory download nginx.tar.gz package

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

If the Rewirte rule is not available by default in the downloaded Nginx, you will need to download the extension of the Pcre package to implement this feature:

wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-7.8.tar.gz # Download Pcre
Tar xzvf pcre-7.8.tar.gz # unzip Pcre

The following compilation installs the Pcre

cd/usr/src/pcre-7.8;
./configure--prefix=/usr/local/pcre--enable-utf8--enable-unicode-properties

Compile and install Nginx below

cd/usr/src/nginx-0.7.62

Start configuring the parameters to be compiled (note: the content is longer and easy to get wrong.) 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-- Pid-path=/usr/local/nginx/var/nginx.pid--lock-path=/usr/local/nginx/var/nginx.lock--http-client-body-temp-path =/dev/shm/nginx_temp/client_body--http-proxy-temp-path=/dev/shm/nginx_temp/proxy--without-http_rewrite_module- -without-http-cache--without-http_map_module--without-http_gzip_module # using Debuginfo information--with-debug#

The next step is to compile and generate the appropriate files:

Make
Make install

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

Mkdir/dev/shm/nginx_temp

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
Mkdir-p/data0/vshare/htdocs
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)

Ulimit-shn 51200 #设置打开文件句柄

Here, we can use the VI editor under Linux to edit:

cd/usr/src/nginx-0.7.62/conf/
VI nginx.conf

Replace the previously modified content under window (press the INSERT key into edit mode) into the current file, and when the modification is complete, press the colon (":") to switch to command mode, then type "Wq" to save and exit. (Force exit (not save), enter q!, then return)

Note:

Events {
Use Epoll;
Worker_connections 20240;
}

Note: Use Epoll; For Linux use, for more information see Nginxchsoptimizations

This will allow you to run the Nginx:

/usr/local/nginx/sbin/nginx-t–c

Create a soft link: $ sudo ln-s/user/local/nginx/sbin/nginx/usr/bin/nginx #之前安过的话要先删除sudo Rm/usr/bin/nginx

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

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

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

Of course, in Nginx, the file cache is also supported, in order to cache those static files on the local nginx server, but to modify its 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; #设置浏览器过期时间7天
Root Data/nginx_cache/iis; #静态文件根目录目录 (must correspond to Proxy_temp_path)
Proxy_store on; #开启缓存机制
Proxy_store_access USER:RW GROUP:RW ALL:RW; #缓存读写规则
Proxy_temp_path Data/nginx_cache/iis; #存放静态文件的缓存目录
# include proxy.conf; # The detailed configuration of the external proxy, such as Proxy_set_header, Client_max_body_size.
if (!-e $request _filename) {
Proxy_pass http://10.0.2.136;/
}
}

This will generate temporary information for the corresponding gif,jpg and other files at Data/nginx_cache/iis, and when the client request arrives, it will be returned to the client after the corresponding file binding is retrieved from the directory to relieve the pressure on the IIS server and network bandwidth.

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

Playing load Balancer---Configuring Nginx under Windows and Linux

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.