Centos 7 build Nginx load balancing, the most simple.

1. Install Nginx

1.1 Download the installation package Nginx

Nginx official website (https://nginx.org)
The choice is nginx-1.6.3.tar.gz version, installation environment is centos7.
The installation package is then uploaded to the downloaded Centos7 SecureCRT tool or by using the command to download the installation package wget

#下载nginx-1.6.3.tar.gz
wget -c https://nginx.org/download/nginx-1.6.3.tar.gz

If the following prompt appears

-bash: wget: command not found

The execution

yum install wget

2. Install Nginx premise environment

1) install gcc environment

yum install gcc-c++

2) Installation dependent libraries PCRE

yum install -y pcre pcre-devel

3) Installation dependent libraries zlib

yum install -y zlib zlib-devel

4) install the OpenSSL cryptographic library Secure Sockets Layer

yum install -y openssl openssl-devel

5) decompression Nginx

#解压文件夹
tar -zxvf nginx-1.6.3.tar.gz

6) execute configuration commands

cd进入文件夹
cd nginx-1.6.3

Execute configuration commands

./configure

Appears in the following figure: for success

Here Insert Picture DescriptionHere Insert Picture description
7) compile the installation command

 

make install

8) Finding the installation path

whereis nginx

The results are as follows:

Here Insert Picture DescriptionHere Insert Picture description
9) to start the service

 

Into the directory of nginx

cd /usr/local/nginx/sbin/

Run the following command

#启动
./nginx

#停止,此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s stop

#停止,此方式停止步骤是待nginx进程处理任务完毕进行停止
./nginx -s quit

#重新加载配置文件,Nginx服务不会中断
./nginx -s reload

10) modify the configuration file

For example, modify the port number, the default port number is 80, we here into 83;

Enter the Configuration folder

cd /usr/local/nginx/conf

Backup the original configuration file

cp nginx.conf nginx.conf.back

Edit the configuration file nginx.conf

vim nginx.conf

Find the server in listen, modify the port number is 83

Here Insert Picture DescriptionHere Insert Picture Description
Start Service

 

#要进入sbin/下才能执行
./nginx

View nginx process

ps -ef|grep nginx

Here Insert Picture DescriptionHere insert a picture describing
this, nginx basic installation is complete, access the server address directly in the browser: virtual machine ip: 83, you can enter the page
Here Insert Picture Descriptionhere Insert Picture Description

3. Install Tomcat

tomcat's official website (http://tomcat.apache.org/)
selected this time is the latest version of this installation environment is centos7.

I was directly wget command to download

Here Insert Picture DescriptionHere Insert Picture Description

Extract the folder

tar -zxvf apache-tomcat-8.5.40.tar.gz

Rename

mv apache-tomcat-8.5.40 tomcat-1

Similarly, again extract the installation package named tomcat-2

mv apache-tomcat-8.5.40 tomcat-2

1) Modify the port number tomcat

将 tomcat-1 的 http 端口设置为8080,将 tomcat-2 的 http 端口设置为8081。

进入tomcat的conf文件夹,修改server.xml

vim server.xml

修改SHUTDOWN、HTTP/1.1、redirectPort、AJP/1.3端口,使其错开,避免重启的时候,报端口被占用问题

tomcat-1 的SHUTDOWN、HTTP/1.1、redirectPort、AJP/1.3设置如下:

<!--关闭服务端口-->
<Server port="9005" shutdown="SHUTDOWN">
...
<!--HTTP服务端口8080,跳转端口9443-->
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="9443" />

<!--AJP服务端口-->
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />
...
</Server>

tomcat-2 的SHUTDOWN、HTTP/1.1、redirectPort、AJP/1.3设置如下:

<!--关闭服务端口-->
<Server port="10005" shutdown="SHUTDOWN">
...
<!--HTTP服务端口8081-->
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="10443" />

<!--AJP服务端口-->
<Connector port="10009" protocol="AJP/1.3" redirectPort="10443" />
...
</Server>

2)启动服务

分别进入 tomcat-1 、 tomcat-2 的bin文件夹,执行脚本,启动服务

sh startup.sh

查看服务是否启动成功

ps -ef|grep tomcat

可以直接在浏览器上分别输入ip:8080、ip:8081进行访问了,结果如下:

Here Insert Picture Description在这里插入图片描述
3)编写Html

 

为了便于测试,我们创建一个html格式的页面,文件命名为index.html,内容如下:
进入tomcat-1的webapps文件夹,删除ROOT文件夹里面的东西,创建index.html文件;

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        Hello server1!
    </body>
</html>

进入tomcat-1的webapps文件夹,删除ROOT文件夹里面的东西,创建index.html文件;

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        Hello server2!
    </body>
</html>

4)测试

创建好了之后,分别在浏览器上访问ip:8080、ip:8081;

ip:8080,结果如下:

Here Insert Picture Description在这里插入图片描述
Here Insert Picture Description在这里插入图片描述

实现负载均衡

进入 Nginx 的配置文件夹

cd /usr/local/nginx/conf

编辑nginx.conf配置文件

vim nginx.conf

主要新增upstream集群配置点,配置如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {  
    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

     #服务器的集群(这个就是我们要配置的地方)
     #test.com:服务器集群名字,自定义
    upstream  test.com {
        #服务器配置   weight是权重的意思,权重越大,分配的概率越大。
        #127.0.0.1:8080、127.0.0.1:8081对应tomcat服务器地址
        server    127.0.0.1:8080  weight=1;
        server    127.0.0.1:8081  weight=2;
    }

    server {  
        listen       83;
        server_name  localhost;

    location / {
    ·        #配置反向代理地址
            proxy_pass http://test.com;
            proxy_redirect default;
        }


        error_page   500 502 503 504  /50x.html;  
        location = /50x.html {  
            root   html;  
        }  
    }  
}

参数说明:

• worker_processes: the number of work processes, generally consistent with the number of cpu core computers
• worker_connections: The maximum number of connections a single process (the maximum number of connections * = number of process connections)
• the include: file extension and file type map
• default_type : default file type
• sendfile: open and efficient file transfer mode, nginx sendfile directive specifies whether to call sendfile function to output files for common applications is set to on, if used to download applications such as disk IO heavy load applications, can be set to off, to balance disk and network I / O processing speed and reduce the load on the system. Note: If the picture is not displayed properly put into this off.
• keepalive_timeout: long connection timeout, in seconds
• upstream: point server cluster configuration

Once configured, enter / usr / local / nginx / sbin / folder, reload the configuration file

./nginx -s reload

Finally, access Nginx server's ip: 83 address, repeatedly refreshing to see results:

Here Insert Picture DescriptionHere insert a picture described
brush a few times
Here Insert Picture Descriptionhere Insert Picture Description

Firewall open

####开启防火墙
[root@localhost httpd]# systemctl start firewalld
####添加81端口放行规则
[root@localhost sysconfig]# firewall-cmd --zone=public --add-port=83/tcp --permanent
success
####添加8080端口放行规则
[root@localhost sysconfig]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
####添加8081端口放行规则
[root@localhost sysconfig]# firewall-cmd --zone=public --add-port=8081/tcp --permanent
success
####重新加载一下
[root@localhost httpd]# sudo firewall-cmd --reload

More please pay attention to public resources and tutorials number: non-Coban Coban.
If you think I write can also Give me praise, thank you, your encouragement is the driving force of my work

 

 

Guess you like

Origin www.cnblogs.com/liduchang/p/11779035.html