nginx的四层转发功能

摘要:
[ root@web01/Etc/nginx/conf。d] #mkdir-p/website/test#2。添加主页[root@web01/etc/nginx/conf.d]#echo'isis<


架构图


nginx的四层转发功能第1张

配置过程

配置web服务器

# 1、配置web01,更改配置文件
[root@web01 /etc/nginx/conf.d]# vi test1.conf 
server {
        listen 8007;
        server_name test.gong.com;
        root /website/test;
        index index.html;
}

# 2、创建站点目录。
[root@web01 /etc/nginx/conf.d]# mkdir -p /website/test

# 2、添加主页
[root@web01 /etc/nginx/conf.d]# echo 'This is <h1 style="color:red;">web01</h1> page!!' >/website/test/index.html

# 4、重启nginx
[root@web01 /etc/nginx/conf.d]# nginx -s reload

# 用同样的方法配置web02
## web02监听的8008端口
[root@web01 /etc/nginx/conf.d]# vi test1.conf 
server {
        listen 8008;
        server_name test.gong.com;
        root /website/test;
        index index.html;
}

配置七层负载均衡

# 1、配置负载均衡
[root@lb01 /etc/nginx/conf.d]# vi upstream.conf 
upstream test_gong {
        172.16.1.7:8007;
        172.16.1.8:8008;
}

server {
        listen 8005;
        server_name test.gong.com;
        location / {
                proxy_pass http://test_gong;
                include proxy_params;
        }
}

[root@lb01 /etc/nginx/conf.d]# nginx -s reload

# 第二台也使用相同的配置
##
[root@db01 /etc/nginx/conf.d]# vi upstream.conf 
upstream test_gong {
        server 172.16.1.7:8007;
        server 172.16.1.8:8008;
}

server {
        listen 8051;
        server_name test.gong.com;
        location / {
                proxy_pass http://test_gong;
                include proxy_params;
        }
}

四层负载均衡概念

四层负载均衡

七层负载均衡:只识别域名,是http层。

四层负载均衡:不识别域名,是tcp层,类似于端口转发。

在nginx-1.9.0之前的版本没有四层负载均衡。

ngx_stream_core_module

用于四层负载均衡

The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

“ngx_stream_core_”模块自1.9.0版起提供。默认情况下,此模块不是生成的,应使用“--with-stream”配置参数启用它。

官方实例

因为是四层的协议,所以不能写在http模块当中。

stream {
    upstream backend {
        hash $remote_addr consistent;

        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }

    upstream dns {
       server 192.168.0.1:53535;
       server dns.example.com:53;
    }

    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }

    server {
        listen 127.0.0.1:53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }

    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
}

四层负载作用

端口转发

做7层负载的高可用

7层负载的端口限制

四层转发的效率比七层的高,因为在tcp的第四层。

大并发的场景会在,七层负载前面添加四层负载。

动静分离

不需要运维来做,开发做的。

  • 动态请求:该请求会调用数据库中的数据。

  • 静态请求:用户请求不会调用数据库。

  • 动态页面:后端开发写的需要调用数据库的页面(python、java、C、php)

  • 静态页面:前端开发写的不需要调用数据库。


配置四层负载均衡

四层负载均衡比七层的转发效率要高,在yum安装nginx的时候不带支持四层负载均衡的模块所以要使用源码编译安装。

[root@nfs01 ~]# tar -xf nginx-1.16.1.tar.gz

[root@nfs01 /application]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

[root@nfs01 ~/nginx-1.16.1]# ./configure --prefix=/application/nginx-1.16.1 --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-stream

[root@nfs01 ~/nginx-1.16.1]# make && make install

[root@nfs01 ~]# vi /etc/profile.d/nginx.sh
export PATH="/application/nginx/sbin:$PATH"

[root@nfs01 ~]# ln -s /application/nginx-1.16.1/ /application/nginx
[root@nfs01 ~]# source /etc/profile

[root@nfs01 ~]# vi /application/nginx/conf/nginx.conf
	...	
events {
    worker_connections  1024;
}
# 加入它在指定的目录下配置单独的配配置文件
include conf.c/*.conf;

http {
	...
	
	
[root@nfs01 /application/nginx/conf/conf.c]# vi four_upstream.conf
stream {
    upstream lb {
            server 172.16.1.5:8005;
            server 172.16.1.51:8051;
    }

    log_format  main '$remote_addr $remote_port - [$time_local] $status $protocol '
                     '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
    
    server {
            listen 80;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass lb;


            access_log  logs/access.log  main;
    }
}

nginx的四层转发功能第2张

nginx的四层转发功能第3张

nginx的四层转发功能第4张

nginx的四层转发功能第5张



FBI WARNING
QQ:1402122292 认准原创sheldon 别人叫我晓东
欢迎访问个人站点:shelldon.51vip.biz

免责声明:文章转载自《nginx的四层转发功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Laravel DEBUG模式 反序列化远程代码执行 POP链MySQL高级特性合并表下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

为 setuptools 开路搭桥

赖勇浩(http://laiyonghao.com) 关键字:python, easy_install, setuptools, Bitvise Tunnelier, polipo, windows, linux, socks5, http, proxy在这里,我们有些网站无法访问。其中对我影响比较大的一个就是安装某些 Python 模块的时候,easy_...

Android系统HAL开发实例

1、前言  Android系统使用HAL这种设计模式,使得上层服务与底层硬件之间的耦合度降低,在文件: AOSP/hardware/libhardware/include/hardware/hardware.h 中描述了HAL的编写规范,并且给出了标准接口,本文将通过一个简单的实例讲解HAL的编写。 2、HAL编写规范 在之前的文章中讲解了两个很重要的数据...

Nginx 编译参数详解/大全

Nginx参数: –prefix= 指向安装目录 –sbin-path 指向(执行)程序文件(nginx) –conf-path= 指向配置文件(nginx.conf) –error-log-path= 指向错误日志目录 –pid-path= 指向pid文件(nginx.pid) –lock-path= 指向lock文件(nginx.lock)(安装文件锁...

微服务中的网关

什么是网关   简单点说网关是一个Api服务器,是系统的唯一入口。为每个客户端提供一个定制的Restful API。同时它还需要具有一些业务之外的责任:鉴权。静态响应等处理。 为什么需要gateway   我们知道我们要进入一个服务本身,并不是一件容易的事情。服务本身有自己的通讯协议,这种协议往往不能很好的兼容各个客户端的需求,所以我们只能寻找一种公共协议...

原创-PromQL语法

匹配标签值时可以是等于,也可以使用正则表达式。总共有下面几种匹配操作符: =:完全相等 !=: 不相等 =~: 正则表达式匹配 !~: 正则表达式不匹配 Prometheus的4种数据类型如下。·瞬时向量(Instant vector):一组时间序列,每个时间序列包含单个样本,它们共享相同的时间戳。也就是说,表达式的返回值中只会包含该时间序列中最新的一...

Nginx开启gzip

1. ngx_http_gzip_module ngx_http_gzip_module是nginx中堆响应以gzip方式进行压缩的一个拦截器,以减少传输数据的大小。 配置示例: gzip on; gzip_min_length 5k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_le...