Centos+nginx Configuring load Balancing from scratch

Source: Internet
Author: User
Tags install openssl nginx load balancing

Understanding of Nginx Load Balancing

Nginx is a lightweight, high-performance webserver, he can mainly do the following two things:

    • As an HTTP server (as with Apache)
    • Load balancing as a reverse proxy server

Now Nginx everywhere can be seen, often see the page after the outage will show Nginx words, which also shows that nginx because of high performance, use configuration simple, open source single These features are more and more users accept, used.

One of the first as an HTTP server, combined with the PHP-FPM process, the request sent to the processing, nginx itself will not parse PHP, he just as a server, accept the request from the client, if it is a PHP request, then to the PHP process processing, and send the results of PHP processing to the client. This is very simple, install the NGINX+PHP-FPM after the configuration of the respective configuration file, start can be achieved. The principle of operation can be seen in the following paragraph explanation:

Nginx does not support direct invocation or parsing of external programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (the socket can be either a file socket or an IP socket). In order to invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as the program used to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; The wrapper then passes the returned data through the FastCGI interface, passing it to nginx along a fixed socket, and finally, Nginx sends the returned data to the client. This is the whole process of nginx+fastcgi, as shown.

The above paragraph explains the nginx+fastcgi operation mechanism, in the Nginx configuration file will match the request, and make the corresponding processing, such as directly return the error file (here and above said a little difference, I think it is nginx internal to the HTML and other static files can do a similar resolution, the PHP process to use the PHP request processing (here the PHP process can be multiple).

The second is to use the reverse proxy load balancing, this is actually very simple, that is, to define a set of servers, to match the request, and transfer the request to any of the server processing, to alleviate the pressure of each server, first look at the online definition of the reverse proxy:

The reverse proxy method refers to a proxy server that accepts connection requests on the Internet, then forwards the request to a server on the internal network and returns the results from the server to the client requesting the connection on the Internet, Reverse. At this point the proxy server appears as a reverse proxy server externally.

Reverse proxy is the opposite of the proxy (or proxy), you have to listen to the proxy, in order to more convenient access to B resources, through a resource indirect access to B resources, the characteristics of users know what the final site to visit is what, but the reverse proxy users do not know what to do behind the proxy server, Reverse proxy in the service of the real processing server in the intranet, the external network can only access the reverse proxy server, which greatly improves security.

Installing the Software

Nginx installation is simple

1, install Nginx needs environment, pcre (function rewrite), zlib (function compression), SSL, this can also download the compilation installation

Yum-y install zlib;

Yum–y install pcre;

Yum–y install OpenSSL;

2. Download and install nginx-*.tar.gz.

TAR–ZXVF nginx-1.2.8.tar.gz–c./;

CD nginx-1.2.8;

./congigure--prefix=/usr/local/nginx;

Make && make install;

3. Configuration

This configuration only needs to modify the content between http{}, the first place to modify is to set up a server group, between the HTTP nodes to add

Upstream myserver{
Server www.myapp2.com:80; #这里是你自己要做负载均衡的服务器地址1
Server www.myapp1.com:8080; #这里是要参与负载均衡的地址2
}

The upstream in Nginx supports the following methods: polling (by default, one access to all servers in chronological order, automatically culling if a server goes down), weight (the server's azimuth probability is proportional to the weight, This can be configured when the server configuration is uneven, ip_hash (hash of each request IP, and according to certain rules assigned to the corresponding server), fair (according to each server response time (RT) To allocate the request, RT know priority allocation), Url_ Hash (according to the hash value of the access URL to allocate the request), I use the default rotation method here.

Point the request to MyServer

Location/{
Proxy_pass Http://myServer;
}

The complete file (delete comment) is as follows:

Worker_processes1; events {Worker_connections1024x768;}    HTTP {include mime.types; Default_type Application/octet-stream;    Sendfile on; Keepalive_timeout $; Upstream myserver{server www.myapp1.com: the; Server www.myapp2.com:8080; } Server {Listen the;        server_name my22; Location/{proxy_pass http://MyServer;        }    }}
Set up the reverse proxy backend as a load balancer of two servers

You can see the previous step has two server addresses, www.myapp1.com:80 and www.myapp2.com:8080, above the Nginx I was installed on the virtual machine above, these two servers I was installed in the native WIN8 system, Using Apache's VirtualHost, set up two domain names, the code under the two domain names is independent of each other, and the settings are simple:

1. Set Apache configuration file

I am using the XAMPP integration environment, there are two places to modify, add in the httpd.conf where the listening port is

Listen 8080

Which means this place is listening to two ports.

Listen 80
Listen 8080

See if the following sentence is open, not open, open, open as shown below

# Virtual Hostsinclude conf/extra/httpd-vhosts.conf

Add the following to the httpd-vhosts.conf

<virtualhost *:>    ServerName www.myapp1.com #对应的域名, load-balanced server address    documentroot E:\soft\ xampp\htdocs\www.myapp1.com #代码文件夹</virtualhost><virtualhost *:8080>     ServerName www.myapp2.com    documentroot E:\soft\xampp\htdocs\www.myapp2.com</VirtualHost>

Modify the Windows Hosts file to append the following content

127.0. 0.1         www.myapp1.com127.0. 0.1        Www.myapp2.com

Modify the Linux/etc/hosts file to append the following content

192.168. 1.12         www.myapp1.com #这里前面的地址对应我win8本机的ip地址 192.168. 1.12        Www.myapp2.com

I put a file in www.myapp1.com:80. index.php "E:\soft\xampp\htdocs\www.myapp1.com\index.php"

www.myapp2.com:8080 also put a file index.php "E:\soft\xampp\htdocs\www.myapp2.com\index.php"

The contents of the file are basically the same, just I ' m The myapp2 this place is different, one is MyApp1, the other is myapp2.

If you can enter www.myapp1.com:80 and www.myapp2.com:8080 in the Win8 browser to see different effects

And below the CentOS see below the result (oneself beautify the next) explanation the configuration is successful

[Email protected] nginx]# Curl www.myapp1.com:I'm The myapp1<br> "view" 1 [[email protected] nginx]# Curl www.myapp2.com:8080I'm The myapp2<br> "view" 1 
<?PHPSession_save_path("./");Session_Start();Header("Content-type:text/html;charset=utf-8");if(isset($_session[' View '])){    $_session[' view '] =$_session[' View '] + 1;}Else{    $_session[' view '] = 1;}Echo"I ' m The Myapp2<br>";Echo"View" {$_session[' View ']} ";

Look at the effect

After all OK can be hard through browser access to see the effect

Forgot to say, the address of Nginx Proxy server is http://192.168.1.113,

After the browser input http://192.168.1.113/index.php, you will find that the

I ' m the myapp2, I ' m the MyApp1

The two pages are exchanged back and forth, the view will not be refreshed twice, which also proves that the default is rotation, but there is a more common problem, when the user visited the site, did not handle the case, Session will be saved on different servers (I use two different folders to simulate two servers), session data may be more than one set, how to solve the problem, the next article said that the problem, in fact, is also very simple.

The copyright belongs to the author Iforever ([email protected]) all, without the author's consent to prohibit any form of reprint, reproduced article must be in the article page obvious location to the author and the original text connection, otherwise reserve the right to pursue legal responsibility.

Centos+nginx Configuring load Balancing from scratch

Related Article

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.