nginx的四层转发功能
架构图

配置过程
配置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的四层转发功能的更多相关文章
- 用haproxy实现nginx的proxy_pass转发功能
公司的网站有个需求,主站点上有两个URL,没有在本地nginx上配置,而是在另一台主机的nginx上配置的站点.如果使用nginx作为反向代理,可以使用proxy_pass指令转发对这两个URL的请求 ...
- nginx实现请求转发
反向代理适用于很多场合,负载均衡是最普遍的用法. nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理. nginx 反向代理官方文档: NGINX REVERSE PROXY 当在 ...
- Nginx配置proxy_pass转发的/路径问题
Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...
- Nginx支持Socket转发过程详解
序言 一网友在群中问,nginx支持socket转发吗? 实话说,我没做过socket转发,但是我知道socket跟http一样都是通过tcp或者udp通信的,我猜测啦一下nginx应该支持吧,然后又 ...
- nginx的反向代理功能和缓存功能
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Nginx 实现端口转发
https://www.cnblogs.com/zhaoyingjie/p/7248678.html Nginx 实现端口转发 什么是端口转发 当我们在服务器上搭建一个图书以及一个电影的应用,其中图书 ...
- 【Nginx】 Nginx实现端口转发
什么是端口转发 当我们在服务器上搭建一个图书以及一个电影的应用,其中图书应用启动了 8001 端口,电影应用启动了 8002 端口.此时如果我们可以通过 localhost:8001 //图书 loc ...
- 【转】Nginx反向代理转发tomcat
http://blog.csdn.net/mlc1218559742/article/details/53117520 最近刚接触nginx,在网上查阅了相关资料,看到最多的形容nginx的词就是反向 ...
- 使用Nginx的proxy_cache缓存功能取代Squid(转)
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
随机推荐
- redis的主从复制(哨兵模式)
p.p1 { margin: 0; font: 10px ".SF NS Text" } Master以写为主,Slave以读为主 读写分离 容灾恢复 一.一主多从 配置文件修改: ...
- JMeter5.4.1源码IDEA构建&二次开发(实战)
JMeter5.4.1源码IDEA构建&二次开发(实战) 目录 JMeter5.4.1源码IDEA构建&二次开发(实战) 1.下载源码 2.导入IDEA 2.1 先设置Gradle目录 ...
- wap视频广告遇到的问题
最近在做一个wap端的视频广告,耗了很多心力在上面,仍旧做不好.没想到wap浏览器对video标签这么不友好.广告需要在原编辑视频播完后插入并自动播放. ios浏览器点击播放按钮后喜欢自动全屏播放,希 ...
- vue 快速入门 系列 —— vue 的基础应用(下)
其他章节请看: vue 快速入门 系列 vue 的基础应用(下) 上篇聚焦于基础知识的介绍:本篇聚焦于基础知识的应用. 递归组件 组件是可以在它们自己的模板中调用自身的.不过它们只能通过 name 选 ...
- 从一个Demo开始,揭开Netty的神秘面纱
本文是Netty系列第5篇 上一篇文章我们对于I/O多路复用.Java NIO包 和 Netty 的关系有了全面的认识. 到目前为止,我们已经从I/O模型出发,逐步接触到了Netty框架.这个过程中, ...
- 2. linux下如何上传和下载文件
一. 安装工具包 yum install -y lrzsz lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议,可以用在windows与linux 系统之间的文件传输,体积小速度 ...
- 1. Intellij IDEA导入,主题修改,布局界面+部分工具栏菜单介绍
Project 和module 的区别 module 相当与eclispe的项目project 相当与eclpise的工作空间 主题的修改 Setting的快捷键:Ctrl+shift+S
- aws EKS EFS storageclass PV PVC Pod
storageclass [root@localhost specs]# cat storageclass.yaml kind: StorageClass apiVersion: storage.k8 ...
- 腾讯云TCA开发工程师认证考试
1.关于云硬盘CBS的描述,错误的是哪一项?(B) A.云硬盘提供数据块级别的数据存储,采用三副本的分布式机制,为 CVM 提供数据可靠性保证 B.云硬盘可在同一可用区中自由挂载.卸载;挂载和卸载过程 ...
- [转]自建CDN防御DDoS
自建CDN防御DDoS(1):知己知彼,建设持久防线 前言 本议题是我们在OWASP杭州区2013年岁末年初安全沙龙中进行分享的内容,在此我们对这个议题的整体内容进行了重新归纳梳理,形成了文字版. 在 ...