Nginx反向代理功能-实现TCP负载均衡实战案例

             Nginx反向代理功能-实现TCP负载均衡实战案例

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.安装MariaDB并授权

1>.安装MariaDB数据库

[[email protected] ~]# yum -y install mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                                                                 | 3.6 kB  00:00:00     
extras                                                                                                                                               | 2.9 kB  00:00:00     
updates                                                                                                                                              | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package mariadb-server.x86_64 1:5.5.64-1.el7 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.64-1.el7 for package: 1:mariadb-server-5.5.64-1.el7.x86_64
--> Processing Dependency: mariadb(x86-64) = 1:5.5.64-1.el7 for package: 1:mariadb-server-5.5.64-1.el7.x86_64
--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.64-1.el7.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.64-1.el7.x86_64
--> Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.64-1.el7.x86_64
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.64-1.el7 will be installed
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be updated
---> Package mariadb-libs.x86_64 1:5.5.64-1.el7 will be an update
---> Package perl-DBD-MySQL.x86_64 0:4.023-6.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
--> Running transaction check
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                       Arch                                 Version                                        Repository                          Size
============================================================================================================================================================================
Installing:
 mariadb-server                                x86_64                               1:5.5.64-1.el7                                 base                                11 M
Installing for dependencies:
 mariadb                                       x86_64                               1:5.5.64-1.el7                                 base                               8.7 M
 perl-DBD-MySQL                                x86_64                               4.023-6.el7                                    base                               140 k
 perl-DBI                                      x86_64                               1.627-4.el7                                    base                               802 k
 perl-Net-Daemon                               noarch                               0.48-5.el7                                     base                                51 k
 perl-PlRPC                                    noarch                               0.2020-14.el7                                  base                                36 k
Updating for dependencies:
 mariadb-libs                                  x86_64                               1:5.5.64-1.el7                                 base                               759 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package  (+5 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 22 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/7): perl-DBD-MySQL-4.023-6.el7.x86_64.rpm                                                                                                         | 140 kB  00:00:00     
(2/7): perl-DBI-1.627-4.el7.x86_64.rpm                                                                                                               | 802 kB  00:00:00     
(3/7): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                                                                         |  51 kB  00:00:00     
(4/7): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                                                                           |  36 kB  00:00:00     
(5/7): mariadb-libs-5.5.64-1.el7.x86_64.rpm                                                                                                          | 759 kB  00:00:01     
(6/7): mariadb-5.5.64-1.el7.x86_64.rpm                                                                                                               | 8.7 MB  00:00:01     
(7/7): mariadb-server-5.5.64-1.el7.x86_64.rpm                                                                                                        |  11 MB  00:00:02     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       8.2 MB/s |  22 MB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                                                                       1/8 
  Installing : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                            2/8 
  Installing : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                        3/8 
  Installing : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                          4/8 
  Installing : perl-DBI-1.627-4.el7.x86_64                                                                                                                              5/8 
  Installing : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                        6/8 
  Installing : 1:mariadb-server-5.5.64-1.el7.x86_64                                                                                                                     7/8 
  Cleanup    : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                     8/8 
  Verifying  : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                                                                       1/8 
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                        2/8 
  Verifying  : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                            3/8 
  Verifying  : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                        4/8 
  Verifying  : 1:mariadb-server-5.5.64-1.el7.x86_64                                                                                                                     5/8 
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                                                                                                              6/8 
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                          7/8 
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                     8/8 

Installed:
  mariadb-server.x86_64 1:5.5.64-1.el7                                                                                                                                      

Dependency Installed:
  mariadb.x86_64 1:5.5.64-1.el7  perl-DBD-MySQL.x86_64 0:4.023-6.el7  perl-DBI.x86_64 0:1.627-4.el7  perl-Net-Daemon.noarch 0:0.48-5.el7  perl-PlRPC.noarch 0:0.2020-14.el7 

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.64-1.el7                                                                                                                                        

Complete!
[[email protected] ~]# 
[[email protected] ~]# yum -y install mariadb-server

2>.启动数据库

[[email protected] ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[[email protected] ~]# 
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      50                                                         *:3306                                                                   *:*                  
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[[email protected] ~]# 

3>.对数据库进行安全初始化操作

[[email protected] ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[[email protected] ~]# 

4>.授权Nginx服务器可以连接MySQL数据库

[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
| root | 127.0.0.1 | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
| root | ::1       | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'jason'@'172.30.1.101' IDENTIFIED BY 'yinzhengjie' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+-------+--------------+-------------------------------------------+
| user  | host         | password                                  |
+-------+--------------+-------------------------------------------+
| root  | localhost    | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
| jason | 172.30.1.101 | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
| root  | 127.0.0.1    | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
| root  | ::1          | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
+-------+--------------+-------------------------------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW GRANTS FOR jason@'172.30.1.101';
+--------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for jason@172.30.1.101                                                                                                              |
+--------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jason'@'172.30.1.101' IDENTIFIED BY PASSWORD '*BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7' WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[[email protected] ~]# 
[[email protected] ~]# 

5>.Nginx节点测试连接数据库

[[email protected] ~]# mysql -h node107.yinzhengjie.org.cn -u jason -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

mysql> 
mysql> QUIT
Bye
[[email protected] ~]# 
[[email protected] ~]# 

 

二.安装Redis并授权

1>.安装epel源

[[email protected] ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                            | 3.6 kB  00:00:00     
extras                                                                                                          | 2.9 kB  00:00:00     
updates                                                                                                         | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                              Arch                           Version                      Repository                      Size
=======================================================================================================================================
Installing:
 epel-release                         noarch                         7-11                         extras                          15 k

Transaction Summary
=======================================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                                                    |  15 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                                                            1/1 
  Verifying  : epel-release-7-11.noarch                                                                                            1/1 

Installed:
  epel-release.noarch 0:7-11                                                                                                           

Complete!
[[email protected] ~]# 
[[email protected] ~]# yum -y install epel-release

2>.利用epel源安装Redis服务

[[email protected] ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                            | 9.2 kB  00:00:00     
 * base: mirror.bit.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
epel                                                                                                            | 5.4 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                                     |  90 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                   | 1.0 MB  00:00:01     
(3/3): epel/x86_64/primary_db                                                                                   | 6.9 MB  00:00:01     
Available Packages
Name        : redis
Arch        : x86_64
Version     : 3.2.12
Release     : 2.el7
Size        : 544 k
Repo        : epel/x86_64
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
            : structure server since keys can contain strings, hashes, lists, sets and
            : sorted sets.
            : 
            : You can run atomic operations on these types, like appending to a string;
            : incrementing the value in a hash; pushing to a list; computing set
            : intersection, union and difference; or getting the member with highest
            : ranking in a sorted set.
            : 
            : In order to achieve its outstanding performance, Redis works with an
            : in-memory dataset. Depending on your use case, you can persist it either
            : by dumping the dataset to disk every once in a while, or by appending
            : each command to a log.
            : 
            : Redis also supports trivial-to-setup master-slave replication, with very
            : fast non-blocking first synchronization, auto-reconnection on net split
            : and so forth.
            : 
            : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
            : limited time-to-live, and configuration settings to make Redis behave like
            : a cache.
            : 
            : You can use Redis from most programming languages also.

[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# yum info redis
[[email protected] ~]# yum -y install redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * epel: mirrors.yun-idc.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                         Arch                          Version                               Repository                   Size
=======================================================================================================================================
Installing:
 redis                           x86_64                        3.2.12-2.el7                          epel                        544 k
Installing for dependencies:
 jemalloc                        x86_64                        3.6.0-1.el7                           epel                        105 k

Transaction Summary
=======================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 648 k
Installed size: 1.7 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/jemalloc-3.6.0-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY 
Public key for jemalloc-3.6.0-1.el7.x86_64.rpm is not installed
(1/2): jemalloc-3.6.0-1.el7.x86_64.rpm                                                                          | 105 kB  00:00:00     
(2/2): redis-3.2.12-2.el7.x86_64.rpm                                                                            | 544 kB  00:00:00     
---------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                  487 kB/s | 648 kB  00:00:01     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) "
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jemalloc-3.6.0-1.el7.x86_64                                                                                         1/2 
  Installing : redis-3.2.12-2.el7.x86_64                                                                                           2/2 
  Verifying  : redis-3.2.12-2.el7.x86_64                                                                                           1/2 
  Verifying  : jemalloc-3.6.0-1.el7.x86_64                                                                                         2/2 

Installed:
  redis.x86_64 0:3.2.12-2.el7                                                                                                          

Dependency Installed:
  jemalloc.x86_64 0:3.6.0-1.el7                                                                                                        

Complete!
[[email protected] ~]# 
[[email protected] ~]# yum -y install redis
[[email protected] ~]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf
/etc/redis.conf
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli
/usr/bin/redis-sentinel
/usr/bin/redis-server
/usr/lib/systemd/system/redis-sentinel.service
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown
/usr/share/doc/redis-3.2.12
/usr/share/doc/redis-3.2.12/00-RELEASENOTES
/usr/share/doc/redis-3.2.12/BUGS
/usr/share/doc/redis-3.2.12/CONTRIBUTING
/usr/share/doc/redis-3.2.12/MANIFESTO
/usr/share/doc/redis-3.2.12/README.md
/usr/share/licenses/redis-3.2.12
/usr/share/licenses/redis-3.2.12/COPYING
/usr/share/man/man1/redis-benchmark.1.gz
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis
/var/log/redis
/var/run/redis
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# rpm -ql redis

3>.启动Redis

[[email protected] ~]# grep bind /etc/redis.conf | grep -v ^#
bind 127.0.0.1
[[email protected] ~]# 
[[email protected] ~]# vim /etc/redis.conf 
[[email protected] ~]# 
[[email protected] ~]# grep bind /etc/redis.conf | grep -v ^#
bind 172.30.1.106
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                                     *:80                                                  *:*                  
LISTEN      0      128                                     *:22                                                  *:*                  
LISTEN      0      128                                    :::22                                                 :::*                  
[[email protected] ~]# 
[[email protected] ~]# systemctl start redis
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                          172.30.1.106:6379                                                *:*                  
LISTEN      0      128                                     *:80                                                  *:*                  
LISTEN      0      128                                     *:22                                                  *:*                  
LISTEN      0      128                                    :::22                                                 :::*                  
[[email protected] ~]# 
[[email protected] ~]# 

 

三.配置Nginx支持Mysql和Redis数据库的负载均衡实战

1>.修改nginx的主配置文件

[[email protected] ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
 
#加载Nginx支持的TCP反向代理配置文件
include /yinzhengjie/softwares/nginx/conf/tcp/*.conf;
  
http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti
me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[[email protected] ~]# 
[[email protected] ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[[email protected] ~]# 
[[email protected] ~]# 

2>.修改nginx的子配置文件

[[email protected] ~]# cat /yinzhengjie/softwares/nginx/conf/tcp/tcp.conf
stream {
    upstream redis_server {
        server 172.30.1.106:6379 max_fails=3 fail_timeout=30s;
    }

    upstream mysql_server {
        least_conn;
        server 172.30.1.107:3306 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 172.30.1.101:3306;
        proxy_connect_timeout 6s;
        proxy_timeout 15s;
        proxy_pass mysql_server;
    }

    server {
        listen 172.30.1.101:6379;
        proxy_connect_timeout 3s;
        proxy_timeout 3s;
        proxy_pass redis_server;
   }
}
[[email protected] ~]# 
[[email protected] ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[[email protected] ~]# 
[[email protected] ~]# 

3>.启动nginx服务

[[email protected] ~]# netstat -untalp | grep nginx
[[email protected] ~]# 
[[email protected] ~]# nginx                 
[[email protected] ~]# 
[[email protected] ~]# netstat -untalp | grep nginx
tcp        0      0 172.30.1.101:3306       0.0.0.0:*               LISTEN      23976/nginx: master 
tcp        0      0 172.30.1.101:6379       0.0.0.0:*               LISTEN      23976/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23976/nginx: master 
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      23976/nginx: master 
[[email protected] ~]# 
[[email protected] ~]# 

4>.客户端测试

[[email protected] ~]# yum -y install mariadb-5.5.64-1.el7.x86_64  redis-3.2.12-2.el7.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                                 | 8.8 kB  00:00:00     
 * base: mirrors.huaweicloud.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
epel                                                                                                                                                 | 5.4 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                                                                          |  90 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                                                        | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                                                                        | 6.9 MB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.64-1.el7 will be installed
---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                  Arch                                   Version                                         Repository                            Size
============================================================================================================================================================================
Installing:
 mariadb                                  x86_64                                 1:5.5.64-1.el7                                  base                                 8.7 M
 redis                                    x86_64                                 3.2.12-2.el7                                    epel                                 544 k
Installing for dependencies:
 jemalloc                                 x86_64                                 3.6.0-1.el7                                     epel                                 105 k

Transaction Summary
============================================================================================================================================================================
Install  2 Packages (+1 Dependent package)

Total download size: 9.4 M
Installed size: 50 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/jemalloc-3.6.0-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for jemalloc-3.6.0-1.el7.x86_64.rpm is not installed
(1/3): jemalloc-3.6.0-1.el7.x86_64.rpm                                                                                                               | 105 kB  00:00:00     
(2/3): redis-3.2.12-2.el7.x86_64.rpm                                                                                                                 | 544 kB  00:00:00     
(3/3): mariadb-5.5.64-1.el7.x86_64.rpm                                                                                                               | 8.7 MB  00:00:01     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       7.9 MB/s | 9.4 MB  00:00:01     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) "
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jemalloc-3.6.0-1.el7.x86_64                                                                                                                              1/3 
  Installing : redis-3.2.12-2.el7.x86_64                                                                                                                                2/3 
  Installing : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                            3/3 
  Verifying  : redis-3.2.12-2.el7.x86_64                                                                                                                                1/3 
  Verifying  : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                            2/3 
  Verifying  : jemalloc-3.6.0-1.el7.x86_64                                                                                                                              3/3 

Installed:
  mariadb.x86_64 1:5.5.64-1.el7                                                         redis.x86_64 0:3.2.12-2.el7                                                        

Dependency Installed:
  jemalloc.x86_64 0:3.6.0-1.el7                                                                                                                                             

Complete!
[[email protected] ~]# 
[[email protected] ~]# yum -y install mariadb-5.5.64-1.el7.x86_64 redis-3.2.12-2.el7.x86_64        #安装mariadb和redis的客户端连接工具
[[email protected] ~]# which mysql
/usr/bin/mysql
[[email protected] ~]# 
[[email protected] ~]# rpm -qf /usr/bin/mysql
mariadb-5.5.64-1.el7.x86_64
[[email protected] ~]# 
[[email protected] ~]# which redis-cli
/usr/bin/redis-cli
[[email protected] ~]# 
[[email protected] ~]# rpm -qf /usr/bin/redis-cli
redis-3.2.12-2.el7.x86_64
[[email protected] ~]# 

4>.连接mariadb服务器

[[email protected] ~]# mysql -h node101.yinzhengjie.org.cn -u jason -pyinzhengjie
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[[email protected] ~]# 
[[email protected] ~]# 

5>.连接redis服务器

[[email protected] ~]# redis-cli -h node101.yinzhengjie.org.cn
node101.yinzhengjie.org.cn:6379> 
node101.yinzhengjie.org.cn:6379> SET name jason2019
OK
node101.yinzhengjie.org.cn:6379> GET name
"jason2019"
node101.yinzhengjie.org.cn:6379> 
node101.yinzhengjie.org.cn:6379> QUIT
[[email protected] ~]# 
[[email protected] ~]# 

 

 

 

 

你可能感兴趣的:(Nginx反向代理功能-实现TCP负载均衡实战案例)