Linux学习120 nginx高级模块应用及优化思路
一、相应模块
1、ngx_stream_core_module
a、查看此模块
[root@www ~]# rpm -qa|grep nginx nginx-mod-stream-1.16.1-3.el7.x86_64 nginx-filesystem-1.16.1-3.el7.noarch nginx-mod-http-perl-1.16.1-3.el7.x86_64 nginx-mod-http-xslt-filter-1.16.1-3.el7.x86_64 nginx-1.16.1-3.el7.x86_64 nginx-all-modules-1.16.1-3.el7.noarch nginx-mod-mail-1.16.1-3.el7.x86_64 nginx-mod-http-image-filter-1.16.1-3.el7.x86_64 [root@www ~]# rpm -ql nginx-mod-stream-1.16.1-3.el7.x86_64 /usr/lib64/nginx/modules/ngx_stream_module.so /usr/share/nginx/modules/mod-stream.conf [root@www ~]# cat /usr/share/nginx/modules/mod-stream.conf load_module "/usr/lib64/nginx/modules/ngx_stream_module.so";
b、对nginx来讲如果你不准备使用http服务就可以不用使用http而是使用stream即可。所有4层负载均衡及代理功能都应该放在stream上下文中。又因为在stream中由于其为伪四层代理因此面向客户端一侧所有功能依然需要使用server来表示,需要监听在哪个端口上来代理至具体的哪个后端主机。
c、配置我们的四层代理
(1)、创建配置文件并启动服务
[root@www ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } stream { server { listen 22922; proxy_pass 192.168.10.41:22; } } [root@www ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@www ~]# systemctl restart nginx [root@www ~]# netstat -anpt|grep 229 tcp 0 0 0.0.0.0:22922 0.0.0.0:* LISTEN 1436/nginx: master
(2)、尝试进行连接
[root@www ~]# ssh -p22922 root@192.168.10.43 root@192.168.10.43's password: Last login: Wed Dec 16 04:06:30 2020 from 192.168.10.43 [root@localhost ~]# ip a |grep 10. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.10.41/24 brd 192.168.10.255 scope global noprefixroute ens33
(3)、配置80端口代理
[root@www ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } stream { server { listen 22922; proxy_pass 192.168.10.41:22; } server { listen 80; proxy_pass 192.168.10.42:80; } } [root@www ~]# curl 192.168.10.43 upstream server2:192.168.10.42
d、配置负载均衡
(1)、配置upstream:此处的upstream模块是我们stream模块中的upstream,与我们前面配置的http中的upstream很相似而已。
[root@www ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } stream { upstream sshsrvs { server 192.168.10.41:22; server 192.168.10.42:22; } upstream websrvs { server 192.168.10.41:80; server 192.168.10.42:80; } server { listen 22922; proxy_pass sshsrvs; } server { listen 80; proxy_pass websrvs; } } [root@www ~]# for i in {1..4};do curl 192.168.10.43;done upstream server1:192.168.10.41 upstream server2:192.168.10.42 upstream server1:192.168.10.41 upstream server2:192.168.10.42
(2)、配置mysql的负载均衡
[root@www ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } stream { upstream sshsrvs { server 192.168.10.41:22; server 192.168.10.42:22; } upstream websrvs { server 192.168.10.41:80; server 192.168.10.42:80; } upstream mysrvs { server 192.168.10.41:3306; server 192.168.10.42:3306; } server { listen 22922; proxy_pass sshsrvs; } server { listen 80; proxy_pass websrvs; } server { listen 3306; proxy_pass mysrvs; } } [root@www ~]# nginx -s reload [root@www ~]# netstat -anpt|grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1436/nginx: master
(3)、我们也可以将来自同一个IP的请求转发至同一个后端主机。(此模块不支持ip_hash,不过支持hash)
e、总结
二、nginx编译安装
1、编译示例
2、nginx打补丁方式
3、课外实践