How to configure server load balancer for centos7 + apache and CentOS + Nginx

Source: Internet
Author: User
Tags curl fpm hash install openssl wrapper centos tomcat nginx load balancing

Install apache on centos7 for load balancing

1. Install apache.

Yum-y install httpd

 
Installed in the/etc/httpd Directory

II. Install mod_jk

Go to http://tomcat.apache.org/connectors-doc/ http://tomcat.apache.org/to download the package
Tomcat Connectors 1.2.40 Released

Install tomcat-connectors-1.2.40-src.tar

If no/usr/bin/apxs is available, install apxs
 
Yum-y install apr-util-devel
Yum-y install httpd-devel

Tar-zxvf tomcat-connectors-1.2.40-src.tar
Cd tomcat-connectors-1.2.40-src/native
./Configure -- with-apxs =/usr/bin/apxs
Make & make install

4. Copy the tomcat-connectors-1.2.40-src/native/apache-2.0/mod_jk.so to the/etc/httpd/modules/directory.

 
Cp tomcat-connectors-1.2.40-src/native/apache-2.0/mod_jk.so/etc/httpd/modules

 
V. Configuration

Vi/etc/httpd/conf/httpd. conf


Add

Include/etc/httpd/conf/mod_jk.conf

6.

/Etc/httpd/conf/

Create

Mod_jk.conf

File. Configuration content

 
LoadModule jk_module/etc/httpd/modules/mod_jk.so
JkWorkersFile/etc/httpd/conf/workers. properties
JkMountFile/etc/httpd/conf/uriworkermap. properties
JkLogFile/etc/httpd/logs/mod_jk.log
JkLogLevel warn
JkLogStampFormat "[% a % B % d % H: % M: % S % Y]"
# JkMount/*. * controller

7.


/Etc/httpd/conf/
Create and configure the workers. properties file
Worker. list = controller, status

Worker. tomcat129.port = 8009
Worker. tomcat129.host = 192.168.152.129
Worker. tomcat129.type = ajp13
Worker. tomcat129.lbfactor = 1

Worker. tomcat130.port = 8009
Worker. tomcat130.host = 192.168.152.130
Worker. tomcat130.type = ajp13
Worker. tomcat130.lbfactor = 1

Worker. controller. type = lb
Worker. controller. balance_workers = tomcat129, tomcat130
Worker. controller. sticky_session = 0

Worker. status. type = status

8.


/Etc/httpd/conf/
Create the uriworkermap. properties file

/* = Controller
#/Jkstatus = status
#! /*. Gif = controller
#! /*. Jpg = controller
#! /*. Png = controller
#! /*. Css = controller
#! /*. Js = controller
#! /*. Htm = controller
#! /*. Html = controller


The server load balancer is configured.

If an error is reported during startup

Modify the httpd. conf file and find ServerName to add
# ServerName www.example.com: 80

ServerName localhost: 80

The remaining sessions are copied.



CentOS + Nginx step by step to configure server load balancer

Nginx load balancing

Nginx is a lightweight and high-performance WebServer. It can do the following two things:

As an http server (same as apache)
Server load balancer as a reverse proxy server

Nginx can be seen everywhere now. It is often seen that nginx is displayed on the webpage after a crash, this also shows that Nginx is used by more and more users due to its high performance, simple configuration, and open-source ticket.

The first type is used as an http server, combined with the php-fpm process, to process the sent requests. nginx itself does not parse php, but serves as a server and accepts the requests sent from the client, if it is a php request, it is handed over to the php process for processing and the result after php processing is completed is sent to the client. This is simple. After nginx + php-fpm is installed, configure the respective configuration files and start them. For the operating principle, see the following explanation:

Nginx does not support direct calling or parsing of external programs. All external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket in Linux (this socket can be a file socket or an ip socket ). To call the CGI program, you also need a FastCGI wrapper (wrapper can be understood as the program used to start another program). This wrapper is bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to this socket, through the FastCGI interface, wrapper receives the request and then derives a new thread, this thread calls the interpreter or external program to process the script and read the returned data. Then, wrapper transmits the returned data to Nginx through the FastCGI interface along a fixed socket. Finally, nginx sends the returned data to the client. This is the entire operation process of Nginx + FastCGI, as shown in the figure below.

The above section explains the operating mechanism of nginx + fastcgi. In the nginx configuration file, requests are matched and processed accordingly, for example, if an error file is returned directly (this is a little different from the above mentioned, I guess nginx can parse html and other static files like the above figure ), use the php process to process php requests (multiple php processes can be used here ).

The second approach is to use reverse proxy for load balancing, which is actually very simple. In other words, we define a group of servers to match the requests and forward the requests to any server for processing, to reduce the pressure on each server, first look at the definition of reverse proxy on the Internet:

The Reverse Proxy method is to use a Proxy server to accept connection requests from the internet, and then forward the requests to the server on the internal network, return the result obtained from the server to the client requesting connection from the internet. The proxy server is displayed as a reverse proxy server.

The reverse proxy is the opposite of the forward proxy (or proxy). You have heard of the proxy. In order to access B resources more conveniently, you can access B resources indirectly through resource, the feature is that the user knows what the website is to be accessed, but the reverse proxy user does not know what processing is done behind the proxy server. The reverse proxy service actually processes the server on the intranet, the internet can only access the reverse proxy server, which greatly improves security.


Install software

Nginx installation is simple

1. Environment required for nginx installation, Pcre (for rewrite), zlib (for compression), ssl, this can also be downloaded, compiled, and installed by yourself

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

During configuration, you only need to modify the content between http {}. The first modification is to set the server group and add between http nodes.

Upstream myServer {

Server www.88181.com: 80; # Here is the server address for your own server load balancer.

Server www.linux.com: 8080; # Here is the address to participate in server load balancer 2

}

Upstream in nginx supports the following methods: Round Robin (by default, all servers are accessed one by one in chronological order. If a server goes down, it will be automatically removed), weight (the server's azimuth probability is proportional to weight, which can be configured when server configuration is uneven), ip_hash (hash calculation for the ip address of each request, and assign the corresponding server according to certain rules) and fair (distribute requests according to the response time (rt) of each server, and rt knows that the allocation is prioritized) and url_hash (requests are allocated according to the hash value of the access url). Here I use the default training mode.

Direct requests to myServer

Location /{

Proxy_pass http: // myServer;

}

The complete file (delete comment) is as follows:

Worker_processes 1;
Events {
Worker_connections 1024;
}
Http {
Include mime. types;
Default_type application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
Upstream myServer {
Server www.linux.com: 80;
Server www.88181.com: 8080;
}
Server {
Listen 80;
Server_name my22;
Location /{
Proxy_pass http: // myServer;
}
}
}


Sets the backend of the reverse proxy as the two servers of server load balancer.

We can see that there are two server addresses in the previous step: www.linux.com: 80 and www.88181.com: 8080. The nginx above is installed on the virtual machine, and the two servers are installed on the win8 system of the local machine, apache virtualhost is used to set two domain names. The codes under these two domain names are independent of each other, and the settings are simple:

1. Set the apache configuration file

I am using the xampp integrated environment. There are two places to modify. Add the listening port in httpd. conf.

Listen 8080.

That is to say, this part listens to two ports.

Listen 80
Listen 8080.

Check whether the following sentence is opened. If it is not opened, open it, as shown below.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Add the following content in the httpd-vhosts.conf,

<VirtualHost *: 80>
ServerName www.linux.com # corresponding domain name, server address of the server load balancer
DocumentRoot E: \ soft \ xampp \ htdocs \ www.linux.com # Code folder
</VirtualHost>
<VirtualHost *: 8080>
ServerName www.88181.com
DocumentRoot E: \ soft \ xampp \ htdocs \ www.88181.com
</VirtualHost>

Modify the windows hosts file and append the following content.

127.0.0.1 www.linux.com
127.0.0.1 www.88181.com

Modify the linux/etc/hosts file and append the following content.

192.168.1.12 www.linux.com # the address above this address corresponds to the IP address of my win8 host
192.168.1.12 www.88181.com

I put a file index. php [E: \ soft \ xampp \ htdocs \ www.linux.com \ index. php] in www.linux.com: 80]

Www.88181.com: 8080 also contains a file index. php [E: \ soft \ xampp \ htdocs \ www.88181.com \ index. php]

The content in the file is basically the same, but there is a difference between I'm the 88181, one is linux, and the other is 88181.

If you can enter www.linux.com: 80 and www.88181.com: 8080 in the win8 browser, different results will be displayed.

In addition, the following results are displayed under CentOS (I beautify it myself), indicating that the configuration is successful.

[Root @ bogon nginx] # curl www.linux.com: 80
I'm the linux <br> [view] 1
[Root @ bogon nginx] # curl www.88181.com: 8080
I'm the 88181 <br> [view] 1

<? Php
Session_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 88181 <br> ";
Echo "[view] {$ _ SESSION ['View']}";

View Results

After all are OK, you can access it through a browser to see the effect.

Forget to mention that the nginx proxy server address is http: // 192.168.1.113,

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

I'm the 88181, I'm the linux

The two pages are exchanged back and forth, and the view is not refreshed twice. This also proves that the default training mode is used.

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.