侧边栏壁纸
  • 累计撰写 85 篇文章
  • 累计创建 39 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录

使用Nginx实现UDP/TCP协议Syslog反向代理(负载均衡)

散漫的老何
2023-09-01 / 0 评论 / 0 点赞 / 937 阅读 / 5,737 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-14,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

使用Nginx实现UDP/TCP协议Syslog 反向代理(负载均衡)

1 说明

Nginx 从1.9.13起开始发布 ngx_stream_core_module 模块不仅能支持 TCP 代理及负载均衡同时还支持 UDP 协议。

2 安装Nginx程序包

2.1 下载Nginx相关依赖软件包

[root@other-server opt]$ yum -y install proc* openssl* pcre*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * centos-sclo-sclo: mirrors.163.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package procps-ng-3.3.10-28.el7.x86_64 already installed and latest version
Package pcre-8.32-17.el7.x86_64 already installed and latest version
Package pcre-devel-8.32-17.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package openssl.x86_64 1:1.0.2k-25.el7_9 will be updated
---> Package openssl.x86_64 1:1.0.2k-26.el7_9 will be an update
---> Package openssl-devel.x86_64 1:1.0.2k-25.el7_9 will be updated
---> Package openssl-devel.x86_64 1:1.0.2k-26.el7_9 will be an update
---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-25.el7_9 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-26.el7_9 will be an update
---> Package openssl-perl.x86_64 1:1.0.2k-26.el7_9 will be installed
--> Processing Dependency: perl(WWW::Curl::Easy) for package: 1:openssl-perl-1.0.2k-26.el7_9.x86_64
---> Package openssl-pkcs11.x86_64 0:0.4.10-1.el7 will be installed
---> Package openssl-static.x86_64 1:1.0.2k-26.el7_9 will be installed
---> Package openssl098e.x86_64 0:0.9.8e-29.el7.centos.3 will be installed
---> Package openssl11.x86_64 1:1.1.1k-5.el7 will be installed
---> Package openssl11-devel.x86_64 1:1.1.1k-5.el7 will be installed
---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed
---> Package openssl11-static.x86_64 1:1.1.1k-5.el7 will be installed
---> Package pcre-static.x86_64 0:8.32-17.el7 will be installed
---> Package pcre-tools.x86_64 0:8.32-17.el7 will be installed
---> Package pcre2.x86_64 0:10.23-2.el7 will be installed
---> Package pcre2-devel.x86_64 0:10.23-2.el7 will be installed
---> Package pcre2-static.x86_64 0:10.23-2.el7 will be installed
---> Package pcre2-tools.x86_64 0:10.23-2.el7 will be installed
---> Package pcre2-utf16.x86_64 0:10.23-2.el7 will be installed
---> Package pcre2-utf32.x86_64 0:10.23-2.el7 will be installed
---> Package procServ.x86_64 0:2.7.0-1.el7 will be installed
--> Processing Dependency: libtelnet.so.2()(64bit) for package: procServ-2.7.0-1.el7.x86_64
---> Package procenv.x86_64 0:0.60-1.el7 will be installed
---> Package procmail.x86_64 0:3.22-36.el7_4.1 will be installed
---> Package procps-ng-devel.x86_64 0:3.3.10-28.el7 will be installed
---> Package procps-ng-i18n.x86_64 0:3.3.10-28.el7 will be installed
---> Package procyon.noarch 0:0.5.33-0.1.pre03.el7 will be installed
--> Processing Dependency: java-headless for package: procyon-0.5.33-0.1.pre03.el7.noarch
---> Package procyon-compilertools.noarch 0:0.5.33-0.1.pre03.el7 will be installed
---> Package procyon-core.noarch 0:0.5.33-0.1.pre03.el7 will be installed
---> Package procyon-decompiler.noarch 0:0.5.33-0.1.pre03.el7 will be installed
--> Processing Dependency: beust-jcommander for package: procyon-decompiler-0.5.33-0.1.pre03.el7.noarch
---> Package procyon-expressions.noarch 0:0.5.33-0.1.pre03.el7 will be installed
---> Package procyon-reflection.noarch 0:0.5.33-0.1.pre03.el7 will be installed
--> Running transaction check
---> Package beust-jcommander.noarch 0:1.30-5.el7 will be installed
--> Processing Dependency: java >= 1.5 for package: beust-jcommander-1.30-5.el7.noarch
---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.382.b05-1.el7_9 will be installed
---> Package libtelnet.x86_64 0:0.23-1.el7 will be installed
---> Package perl-WWW-Curl.x86_64 0:4.15-13.el7 will be installed
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.382.b05-1.el7_9 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================================================================================
 Package                          Arch                          Version                          Repository                          Size
=================================================================================================================================================
Installing:
 openssl-perl                     x86_64                        1:1.0.2k-26.el7_9                          updates                          73 k
 openssl-pkcs11                   x86_64                        0.4.10-1.el7                               epel                             61 k
 openssl-static                   x86_64                        1:1.0.2k-26.el7_9                          updates                          1.2 M
 openssl098e                      x86_64                        0.9.8e-29.el7.centos.3                     base                             793 k
 openssl11                        x86_64                        1:1.1.1k-5.el7                             epel                             693 k
 openssl11-devel                  x86_64                        1:1.1.1k-5.el7                             epel                             2.3 M
 openssl11-libs                   x86_64                        1:1.1.1k-5.el7                             epel                             1.5 M
 openssl11-static                 x86_64                        1:1.1.1k-5.el7                             epel                             1.6 M
 pcre-static                      x86_64                        8.32-17.el7                                base                             326 k
 pcre-tools                       x86_64                        8.32-17.el7                                base                              88 k
 pcre2                            x86_64                        10.23-2.el7                                base                             201 k
 pcre2-devel                      x86_64                        10.23-2.el7                                base                             545 k
 pcre2-static                     x86_64                        10.23-2.el7                                base                             371 k
 pcre2-tools                      x86_64                        10.23-2.el7                                base                             104 k
 pcre2-utf16                      x86_64                        10.23-2.el7                                base                             189 k
 pcre2-utf32                      x86_64                        10.23-2.el7                                base                             181 k
 procServ                         x86_64                        2.7.0-1.el7                                epel                             161 k
 procenv                          x86_64                        0.60-1.el7                                 epel                              85 k
 procmail                         x86_64                        3.22-36.el7_4.1                            base                             171 k
 procps-ng-devel                  x86_64                        3.3.10-28.el7                              base                              38 k
 procps-ng-i18n                   x86_64                        3.3.10-28.el7                              base                             100 k
 procyon                          noarch                        0.5.33-0.1.pre03.el7                       epel                              10 k
 procyon-compilertools            noarch                        0.5.33-0.1.pre03.el7                       epel                             1.2 M
 procyon-core                     noarch                        0.5.33-0.1.pre03.el7                       epel                             151 k
 procyon-decompiler               noarch                        0.5.33-0.1.pre03.el7                       epel                              28 k
 procyon-expressions              noarch                        0.5.33-0.1.pre03.el7                       epel                             223 k
 procyon-reflection               noarch                        0.5.33-0.1.pre03.el7                       epel                             332 k
Updating:
 openssl                          x86_64                        1:1.0.2k-26.el7_9                          updates                          494 k
 openssl-devel                    x86_64                        1:1.0.2k-26.el7_9                          updates                          1.5 M
 openssl-libs                     x86_64                        1:1.0.2k-26.el7_9                          updates                          1.2 M
Installing for dependencies:
 beust-jcommander                 noarch                        1.30-5.el7                                 base                              63 k
 java-1.8.0-openjdk               x86_64                        1:1.8.0.382.b05-1.el7_9                    updates                          318 k
 java-1.8.0-openjdk-headless      x86_64                        1:1.8.0.382.b05-1.el7_9                    updates                           33 M
 libtelnet                        x86_64                        0.23-1.el7                                 epel                              18 k
 perl-WWW-Curl                    x86_64                        4.15-13.el7                                base                              53 k

Transaction Summary
=================================================================================================================================================
Install  27 Packages (+5 Dependent packages)
Upgrade   3 Packages

Total download size: 49 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/35): beust-jcommander-1.30-5.el7.noarch.rpm                                     |  63 kB  00:00:00     
(2/35): libtelnet-0.23-1.el7.x86_64.rpm                                            |  18 kB  00:00:00     
(3/35): java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64.rpm                        | 318 kB  00:00:00     
(4/35): openssl-1.0.2k-26.el7_9.x86_64.rpm                                         | 494 kB  00:00:01     
(5/35): openssl-pkcs11-0.4.10-1.el7.x86_64.rpm                                     |  61 kB  00:00:00     
(6/35): openssl-perl-1.0.2k-26.el7_9.x86_64.rpm                                    |  73 kB  00:00:00     
(7/35): openssl11-1.1.1k-5.el7.x86_64.rpm                                          | 693 kB  00:00:00     
(8/35): openssl11-devel-1.1.1k-5.el7.x86_64.rpm                                    | 2.3 MB  00:00:00     
(9/35): openssl11-libs-1.1.1k-5.el7.x86_64.rpm                                     | 1.5 MB  00:00:00     
(10/35): openssl11-static-1.1.1k-5.el7.x86_64.rpm                                  | 1.6 MB  00:00:00     
(11/35): pcre-static-8.32-17.el7.x86_64.rpm                                        | 326 kB  00:00:01     
(12/35): openssl098e-0.9.8e-29.el7.centos.3.x86_64.rpm                             | 793 kB  00:00:02     
(13/35): pcre-tools-8.32-17.el7.x86_64.rpm                                         |  88 kB  00:00:00     
(14/35): pcre2-10.23-2.el7.x86_64.rpm                                              | 201 kB  00:00:00     
(15/35): openssl-static-1.0.2k-26.el7_9.x86_64.rpm                                 | 1.2 MB  00:00:03     
(16/35): pcre2-tools-10.23-2.el7.x86_64.rpm                                        | 104 kB  00:00:00     
(17/35): pcre2-devel-10.23-2.el7.x86_64.rpm                                        | 545 kB  00:00:01     
(18/35): pcre2-utf16-10.23-2.el7.x86_64.rpm                                        | 189 kB  00:00:00     
(19/35): pcre2-utf32-10.23-2.el7.x86_64.rpm                                        | 181 kB  00:00:00     
(20/35): procServ-2.7.0-1.el7.x86_64.rpm                                           | 161 kB  00:00:00     
(21/35): procenv-0.60-1.el7.x86_64.rpm                                             |  85 kB  00:00:00     
(22/35): perl-WWW-Curl-4.15-13.el7.x86_64.rpm                                      |  53 kB  00:00:00     
(23/35): procps-ng-devel-3.3.10-28.el7.x86_64.rpm                                  |  38 kB  00:00:00     
(24/35): procmail-3.22-36.el7_4.1.x86_64.rpm                                       | 171 kB  00:00:00     
(25/35): procps-ng-i18n-3.3.10-28.el7.x86_64.rpm                                   | 100 kB  00:00:00     
(26/35): procyon-0.5.33-0.1.pre03.el7.noarch.rpm                                   |  10 kB  00:00:00     
(27/35): procyon-compilertools-0.5.33-0.1.pre03.el7.noarch.rpm                     | 1.2 MB  00:00:00     
(28/35): procyon-core-0.5.33-0.1.pre03.el7.noarch.rpm                              | 151 kB  00:00:00
(29/35): procyon-decompiler-0.5.33-0.1.pre03.el7.noarch.rpm                        |  28 kB  00:00:00
(30/35): procyon-expressions-0.5.33-0.1.pre03.el7.noarch.rpm                       | 223 kB  00:00:00
(31/35): procyon-reflection-0.5.33-0.1.pre03.el7.noarch.rpm                        | 332 kB  00:00:00
(32/35): openssl-devel-1.0.2k-26.el7_9.x86_64.rpm                                  | 1.5 MB  00:00:04
(33/35): openssl-libs-1.0.2k-26.el7_9.x86_64.rpm                                   | 1.2 MB  00:00:03     
(34/35): pcre2-static-10.23-2.el7.x86_64.rpm                                       | 371 kB  00:00:01     
(35/35): java-1.8.0-openjdk-headless-1.8.0.382.b05-1.el7_9.x86_64.rpm              |  33 MB  00:01:29     
-------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                   			      565 kB/s |  49 MB  00:01:29     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                                 						1/39 
  Updating   : 1:openssl-1.0.2k-26.el7_9.x86_64                                      						2/39 
  Installing : pcre2-utf32-10.23-2.el7.x86_64                                        						3/39 
  Installing : pcre2-utf16-10.23-2.el7.x86_64                                        						4/39 
  Installing : procyon-compilertools-0.5.33-0.1.pre03.el7.noarch                     						5/39 
  Installing : procyon-core-0.5.33-0.1.pre03.el7.noarch                              						6/39 
  Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64                                  						7/39 
  Installing : pcre2-10.23-2.el7.x86_64                                              						8/39 
  Installing : 1:java-1.8.0-openjdk-headless-1.8.0.382.b05-1.el7_9.x86_64         							9/39 
  Installing : 1:java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64                  							10/39 
  Installing : beust-jcommander-1.30-5.el7.noarch                                 							11/39 
  Installing : procyon-decompiler-0.5.33-0.1.pre03.el7.noarch                     							12/39 
  Installing : pcre2-devel-10.23-2.el7.x86_64                                     							13/39 
  Installing : 1:openssl11-devel-1.1.1k-5.el7.x86_64                              							14/39 
  Updating   : 1:openssl-devel-1.0.2k-26.el7_9.x86_64                             							15/39 
  Installing : perl-WWW-Curl-4.15-13.el7.x86_64                                   							16/39 
  Installing : libtelnet-0.23-1.el7.x86_64                                        							17/39 
  Installing : procyon-expressions-0.5.33-0.1.pre03.el7.noarch                    							18/39 
  Installing : procyon-reflection-0.5.33-0.1.pre03.el7.noarch                     							19/39 
  Installing : procyon-0.5.33-0.1.pre03.el7.noarch                                							20/39 
  Installing : procServ-2.7.0-1.el7.x86_64                                        							21/39 
  Installing : 1:openssl-perl-1.0.2k-26.el7_9.x86_64                              							22/39 
  Installing : 1:openssl-static-1.0.2k-26.el7_9.x86_64                            							23/39 
  Installing : 1:openssl11-static-1.1.1k-5.el7.x86_64                             							24/39 
  Installing : pcre2-static-10.23-2.el7.x86_64                                    							25/39 
  Installing : pcre2-tools-10.23-2.el7.x86_64                                     							26/39 
  Installing : 1:openssl11-1.1.1k-5.el7.x86_64                                    							27/39 
  Installing : openssl-pkcs11-0.4.10-1.el7.x86_64                                 							28/39 
  Installing : pcre-static-8.32-17.el7.x86_64                                     							29/39 
  Installing : procps-ng-i18n-3.3.10-28.el7.x86_64                                							30/39 
  Installing : procenv-0.60-1.el7.x86_64                                          							31/39 
  Installing : procps-ng-devel-3.3.10-28.el7.x86_64                               							32/39 
  Installing : pcre-tools-8.32-17.el7.x86_64                                      							33/39 
  Installing : procmail-3.22-36.el7_4.1.x86_64                                    							34/39 
  Installing : openssl098e-0.9.8e-29.el7.centos.3.x86_64                          							35/39 
  Cleanup    : 1:openssl-devel-1.0.2k-25.el7_9.x86_64                             							36/39 
  Cleanup    : 1:openssl-1.0.2k-25.el7_9.x86_64                                   							37/39 
  Cleanup    : openssl-libs.x86_64                                                							38/39 
  Cleanup    : openssl-libs.x86_64                                                							39/39 
  Verifying  : 1:java-1.8.0-openjdk-headless-1.8.0.382.b05-1.el7_9.x86_64         							1/39 
  Verifying  : openssl098e-0.9.8e-29.el7.centos.3.x86_64                          							2/39 
  Verifying  : procyon-reflection-0.5.33-0.1.pre03.el7.noarch                     							3/39 
  Verifying  : pcre2-10.23-2.el7.x86_64                                           							4/39 
  Verifying  : beust-jcommander-1.30-5.el7.noarch                                 							5/39 
  Verifying  : 1:java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64                  							6/39 
  Verifying  : 1:openssl11-libs-1.1.1k-5.el7.x86_64                               							7/39 
  Verifying  : 1:openssl11-static-1.1.1k-5.el7.x86_64                             							8/39 
  Verifying  : procyon-core-0.5.33-0.1.pre03.el7.noarch                           							9/39 
  Verifying  : procyon-expressions-0.5.33-0.1.pre03.el7.noarch                    							10/39 
  Verifying  : procmail-3.22-36.el7_4.1.x86_64                                    							11/39 
  Verifying  : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                              							12/39 
  Verifying  : procServ-2.7.0-1.el7.x86_64                                        							13/39 
  Verifying  : pcre2-devel-10.23-2.el7.x86_64                                     							14/39 
  Verifying  : 1:openssl-1.0.2k-26.el7_9.x86_64                                   							15/39 
  Verifying  : pcre-tools-8.32-17.el7.x86_64                                      							16/39 
  Verifying  : procps-ng-devel-3.3.10-28.el7.x86_64                               							17/39 
  Verifying  : libtelnet-0.23-1.el7.x86_64                                        							18/39 
  Verifying  : procyon-decompiler-0.5.33-0.1.pre03.el7.noarch                     							19/39 
  Verifying  : 1:openssl11-devel-1.1.1k-5.el7.x86_64                              							20/39 
  Verifying  : 1:openssl-devel-1.0.2k-26.el7_9.x86_64                             							21/39 
  Verifying  : procyon-compilertools-0.5.33-0.1.pre03.el7.noarch                  							22/39 
  Verifying  : perl-WWW-Curl-4.15-13.el7.x86_64                                   							23/39 
  Verifying  : procenv-0.60-1.el7.x86_64                                          							24/39 
  Verifying  : 1:openssl-static-1.0.2k-26.el7_9.x86_64                            							25/39 
  Verifying  : procyon-0.5.33-0.1.pre03.el7.noarch                                							26/39 
  Verifying  : pcre2-utf16-10.23-2.el7.x86_64                                     							27/39 
  Verifying  : 1:openssl-perl-1.0.2k-26.el7_9.x86_64                              							28/39 
  Verifying  : pcre2-static-10.23-2.el7.x86_64                                    							29/39 
  Verifying  : 1:openssl11-1.1.1k-5.el7.x86_64                                    							30/39 
  Verifying  : openssl-pkcs11-0.4.10-1.el7.x86_64                                 							31/39 
  Verifying  : pcre2-utf32-10.23-2.el7.x86_64                                     							32/39 
  Verifying  : procps-ng-i18n-3.3.10-28.el7.x86_64                                							33/39 
  Verifying  : pcre-static-8.32-17.el7.x86_64                                     							34/39 
  Verifying  : pcre2-tools-10.23-2.el7.x86_64                                     							35/39 
  Verifying  : 1:openssl-1.0.2k-25.el7_9.x86_64                                   							36/39 
  Verifying  : 1:openssl-libs-1.0.2k-25.el7_9.x86_64                              							37/39 
  Verifying  : 1:openssl-devel-1.0.2k-25.el7_9.x86_64                             							38/39 
  Verifying  : 1:openssl-libs-1.0.2k-19.el7.x86_64                                							39/39 
  
Installed:
  openssl-perl.x86_64 1:1.0.2k-26.el7_9   openssl-pkcs11.x86_64 0:0.4.10-1.el7       openssl-static.x86_64 1:1.0.2k-26.el7_9    
  openssl098e.x86_64 0:0.9.8e-29.el7.centos.3      openssl11.x86_64 1:1.1.1k-5.el7   openssl11-devel.x86_64 1:1.1.1k-5.el7
  openssl11-libs.x86_64 1:1.1.1k-5.el7    openssl11-static.x86_64 1:1.1.1k-5.el7     pcre-static.x86_64 0:8.32-17.el7
  pcre-tools.x86_64 0:8.32-17.el7         pcre2.x86_64 0:10.23-2.el7                 pcre2-devel.x86_64 0:10.23-2.el7                    
  pcre2-static.x86_64 0:10.23-2.el7       pcre2-tools.x86_64 0:10.23-2.el7           pcre2-utf16.x86_64 0:10.23-2.el7       
  pcre2-utf32.x86_64 0:10.23-2.el7        procServ.x86_64 0:2.7.0-1.el7              procenv.x86_64 0:0.60-1.el7                           
  procmail.x86_64 0:3.22-36.el7_4.1       procps-ng-devel.x86_64 0:3.3.10-28.el7     procps-ng-i18n.x86_64 0:3.3.10-28.el7  	
  procyon.noarch 0:0.5.33-0.1.pre03.el7   procyon-compilertools.noarch 0:0.5.33-0.1.pre03.el7   procyon-core.noarch 0:0.5.33-0.1.pre03.el7
  procyon-decompiler.noarch 0:0.5.33-0.1.pre03.el7 procyon-expressions.noarch 0:0.5.33-0.1.pre03.el7  procyon-reflection.noarch 0:0.5.33-0.1.pre03.el7

Dependency Installed:
  beust-jcommander.noarch 0:1.30-5.el7    java-1.8.0-openjdk.x86_64 1:1.8.0.382.b05-1.el7_9    java-1.8.0-openjdk-headless.x86_64 1:1.8.0.382.b05-1.el7_9
  libtelnet.x86_64 0:0.23-1.el7           perl-WWW-Curl.x86_64 0:4.15-13.el7                      

Updated:
  openssl.x86_64 1:1.0.2k-26.el7_9        openssl-devel.x86_64 1:1.0.2k-26.el7_9    openssl-libs.x86_64 1:1.0.2k-26.el7_9                                                                      
Complete!
[root@other-server opt]# 

2.2 下载Nginx源代码安装包

[root@other-server opt]$ wget https://nginx.org/download/nginx-1.25.2.tar.gz
--2023-08-30 16:34:13--  https://nginx.org/download/nginx-1.25.2.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1214903 (1.2M) [application/octet-stream]
Saving to: 'nginx-1.25.2.tar.gz'

100%[===========================================================================================================>] 1,214,903   1.17MB/s   in 1.0s 

2023-08-30 16:34:15 (1.17 MB/s) - 'nginx-1.25.2.tar.gz' saved [1214903/1214903]
[root@other-server opt]$ ll
-rw-r--r--  1 root root 1214903 Aug 16 01:36 nginx-1.25.2.tar.gz

2.3 解压Nginx源代码安装包

[root@other-server opt]$ tar zxvf nginx-1.25.2.tar.gz
[root@other-server opt]$ ll
drwxr-xr-x  8 rsyslog rsyslog     158 Aug 16 01:03 nginx-1.25.2
-rw-r--r--  1 root    root    1214903 Aug 16 01:36 nginx-1.25.2.tar.gz

2.4 创建nginx用户和用户组

[root@other-server ]$ groupadd mygroup  # 创建用户组
[root@other-server ]$ useradd nginx     # 创建用户
[root@other-server ]$ passwd nginx      # 给nginx用户设置密码
Changing password for user nginx.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

2.5 开始编译安装Nginx

编译安装命令:

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/log/nginx/nginx.pid \
--lock-path=/var/log/nginx/nginx.lock \
--http-client-body-temp-path=/var/log/nginx/client_temp \
--http-proxy-temp-path=/var/log/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/log/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/log/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/log/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
  1. 进入/opt/nginx-1.25.2目录

    [root@other-server opt]$ cd nginx-1.25.2
    
  2. 开始编译安装nginx

    [root@other-server nginx-1.25.2]$ ./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/log/nginx/nginx.pid \
    --lock-path=/var/log/nginx/nginx.lock \
    --http-client-body-temp-path=/var/log/nginx/client_temp \
    --http-proxy-temp-path=/var/log/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/log/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/log/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/log/nginx/scgi_temp \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_v2_module \
    --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
    checking for OS
     + Linux 3.10.0-1160.76.1.el7.x86_64 x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
    checking for gcc -pipe switch ... found
    checking for -Wl,-E switch ... found
    ······省略······
    Configuration summary
      + using threads
      + using system PCRE2 library
      + using system OpenSSL library
      + using system zlib library
    
      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/var/log/nginx/nginx.pid"
      nginx error log file: "/var/log/nginx/error.log"
      nginx http access log file: "/var/log/nginx/access.log"
      nginx http client request body temporary files: "/var/log/nginx/client_temp"
      nginx http proxy temporary files: "/var/log/nginx/proxy_temp"
      nginx http fastcgi temporary files: "/var/log/nginx/fastcgi_temp"
      nginx http uwsgi temporary files: "/var/log/nginx/uwsgi_temp"
      nginx http scgi temporary files: "/var/log/nginx/scgi_temp"
    # 检查通过!!!
    
  3. 开始make && make install

    [root@other-server nginx-1.25.2]$ make && make install
    make -f objs/Makefile
    make[1]: Entering directory '/opt/nginx-1.25.2'
    cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
    	-o objs/src/core/nginx.o \
    	src/core/nginx.c
    ······省略······
    test -d '/usr/local/nginx/html' \
    	|| cp -R html '/usr/local/nginx'
    test -d '/var/log/nginx' \
    	|| mkdir -p '/var/log/nginx'
    make[1]: Leaving directory '/opt/nginx-1.25.2'
    
  4. 安装完成 - 查看nginx版本信息

    [root@other-server nginx-1.25.2]$ /usr/local/nginx/nginx -v
    nginx version: nginx/1.25.2
    [root@other-server nginx-1.25.2]$ 
    

2.6 设置Nginx环境变量

  1. 修改/etc/profile配置文件添加$NGINX_HOME环境变量

    [root@other-server ]$ vim /etc/profile
    [root@other-server ]$ cat /etc/profile
    export NGINX_HOME="/usr/local/nginx"
    export PATH=$PATH:$NGINX_HOME/sbin
    [root@other-server ]$ source /etc/profile  # 刷新/etc/profile配置文件
    
  2. 测试nginx环境变量是否生效,查看nginx版本信息

    [root@other-server opt]$ nginx -v
    nginx version: nginx/1.25.2
    
  3. 将nginx/conf配置文件软链接到/etc/nginx目录中

    1. 创建/etc/nginx目录

      [root@other-server opt]$ mkdir -p /etc/nginx
      
    2. 创建软链接

      [root@other-server opt]$ ln -s /usr/local/nginx/conf /etc/nginx/conf
      
    3. 查看创建状态

      [root@other-server nginx]$ pwd
      /etc/nginx
      [root@other-server nginx]$ ll
      total 0
      lrwxrwxrwx 1 root root 22 Aug 30 22:01 conf -> /usr/local/nginx/conf/
      # 补充:删除软链接:unlink nginx
      

2.7 创建nginx.service使用systemctl管理

创建/usr/lib/systemd/system/nginx.service配置文件,使用systemctl管理Nginx服务

[root@OtherServer opt]$ cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/log/nginx/nginx.pid

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp = true

[Install]
WantedBy=multi-user.target

3 创建NginxUDP/TCP协议转发配置文件

3.1 nginx.conf

stream nginx.conf 主配置文件,加载514.conf配置文件。

stream {  # 需要与http同一级
	log_format basic 'time=[$time_local] remote_addr=$remote_addr '  # 设置basic日志格式
					'protocol=$protocol '
					'status=$status '
					'bytes_sent=$bytes_sent '
					'bytes_received=$bytes_received '
					'session_time=$session_time'
	include /usr/local/nginx/conf/stream/*.conf;  # 加载udp.d目录下的配置文件
}

3.2 514.conf

514 端口转发配置文件[负载均衡]

upstream udp_514_server {
    # 希负载均衡策略可以通过客户端 IP($remote_addr)实现简单的会话保持,其可将同一IP客户端始终转发给同一台后端服务器。
    hash $remote_addr;    
    server 10.10.0.102:514 weight=5;
    server 10.10.0.103:514 weight=5;
    # weight 权重值
    # udp不建议添加<max_fails>和<fail_timeout>参数
}

upstream tcp_514_server {
    # 希负载均衡策略可以通过客户端 IP($remote_addr)实现简单的会话保持,其可将同一IP客户端始终转发给同一台后端服务器。
    hash $remote_addr;
    server 10.10.0.102:514 weight=5 max_fails=3 fail_timeout=30s;
    server 10.10.0.103:514 weight=5 max_fails=3 fail_timeout=30s;
    # weight 权重值
    # max_fails:在服务器被标记为不可用的时间内必须发生的失败尝试次数(默认为1次尝试)
    # fail_timeout:多次尝试失败而将服务器标记为不可用的时间,以及将服务器标记为不可用的时间(默认为10秒)
    # udp不建议添加<max_fails>和<fail_timeout>参数
}

# udp 514 端口代理
server {
    listen 514 udp;                                 # 监听udp 514端口
    proxy_timeout           1s;                     # 获取被代理服务器的响应最大超时时间为1s
    proxy_connect_timeout   1s;                     # 与被代理服务器建立连接的超时时间为1s
    access_log              /var/log/nginx/udp_514_server.log  basic;
    error_log               /var/log/nginx/udp_error_514_server.log;
    proxy_pass              udp_514_server;         # 代理upstream名称
}

# tcp 514 端口代理
server {
    listen 514;                                         # 监听tcp 514端口
    proxy_timeout               1s;                     # 获取被代理服务器的响应最大超时时间为1s
    proxy_connect_timeout       1s;                     # 与被代理服务器建立连接的超时时间为1s
    proxy_next_upstream         on;                     # 当被代理的服务器返回错误或超时时,将未返回响应的客户端连接请求传递给upstream中的下一个服务器
    proxy_next_upstream_tries   3;                      # 转发尝试请求最多3次
    proxy_next_upstream_timeout 10s;                    # 总尝试超时时间为10s
    proxy_socket_keepalive      on;                     # 开启SO_KEEPALIVE选项进行心跳检测
    access_log                  /var/log/nginx/tcp_514_server.log  basic;
    error_log                   /var/log/nginx/tcp_error_514_server.log;
    proxy_pass                  tcp_514_server;         # 代理upstream名称
}

3.3 启动nginx服务使配置生效

[root@other-server conf]$ systemctl status nginx
* nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-08-30 22:46:07 CST; 7s ago
  Process: 52593 ExecStop=/usr/local/nginx/sbin/nginx -s stop (code=exited, status=1/FAILURE)
  Process: 106075 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 106073 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 106078 (nginx)
    Tasks: 5
   Memory: 3.4M
   CGroup: /system.slice/nginx.service
           |-106078 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           |-106079 nginx: worker process
           |-106080 nginx: worker process
           |-106081 nginx: worker process
           `-106082 nginx: worker process

Aug 30 22:46:07 other-server systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 30 22:46:07 other-server nginx[106073]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Aug 30 22:46:07 other-server nginx[106073]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Aug 30 22:46:07 other-server systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@other-server conf]$ systemctl enable nginx #  设置开机启动

4 实现效果

1693497714425

0
广告 广告

评论区