nginx的stream模块和upstream模块
nginx7层调度方式
使用upstream模块定义集群名称和节点地址 定义在server字段之外httpd字段之内
upstream staticweb {
server 172.17.0.2; #也可以指定weight=2 指定权(默认为轮询算法rr)
server 172.17.0.3;
}
server {
listen 8088;
server_name www.stephenzhong.com;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_pass http://staticweb; #代理至集群名称
}
ip_hash使用ip_hash对后端服务器权重取模。然后调度到同一台服务器
一致性hash算法,对固定数值取模
upstream staticweb {
hash $remote_addr consistent; #一致性hash即使权重之和出问题也不会调度也不会乱套
server 172.17.0.2 weight=2;
server 172.17.0.3;
hash $request_uri consistent; $对uri做一致性hash计算。绑定什么意为这什么不变
2、server address [parameters];
在upstream上下文中server成员,以及相关的参数;Context: upstream
address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
parameters:
weight=number
权重,默认为1;
max_fails=number
失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;
fail_timeout=time
设置将服务器标记为不可用状态的超时时长;
max_conns
当前的服务器的最大并发连接数;
backup
将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;
down
标记为“不可用”;
3、least_conn;
最少连接调度算法,当server拥有不同的权重时其为wlc;
4、 ip_hash;
源地址hash调度方法;
5、hash key [consistent];
基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;
作用:将请求分类,同一类请求将发往同一个upstream server;
If the consistent parameter is specified the ketama consistent hashing method will be used instead.
示例:
hash $request_uri consistent;
hash $remote_addr;
6、keepalive connections;
为每个worker进程保留的空闲的长连接数量;
upstream memcached_backend {
server 127.0.0.1:11211;
server 10.0.0.2:11211;
keepalive 32;
}
server {
...
location /memcached/ {
set $memcached_key $uri;
memcached_pass memcached_backend;
}
}
server 172.17.0.2 weight=2 fail_timeout=2 max_fails=2;#定义后端监控状态失败超时时长2秒最大测试连接次数2秒
server 172.17.0.3 weight=2 fail_timeout=2 max_fails=2 backup;
nginx stream 模块
方向代理mysql
docker run --name db1 -d -e "MYSQL_ROOT_PASSWORD=123456" -v /vols/db1:/var/lib/mysql mysql:5.7 #启动mysql容器
docker exec -it db1 /bin/sh #连接容器设置远程连接账户
grant all on wpdb.* to wpuser@'%' identified by '123456'
vim /etc/nginx/nginx.conf 将stream定义在http字段之外。
stream {
server {
listen 3306;
proxy_pass 172.17.0.4:3306;
}
}
[root@centos7 nginx]# mysql -uwpuser -h192.168.1.198 -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
########################定义四层调度的负载集群
upstream backend {
hash $remote_addr consistent;
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345;
server unix:/tmp/backend3;
server backup1.example.com:12345 backup;
server backup2.example.com:12345 backup;
}
server {
listen 12346;
proxy_passbackend;
}
nginx负载均衡优化
net.ipv4.tcp_fin_timeout=30 当服务器主动关闭连接时,设定的超时时长
net.ipv4.tcp_max_tw_buckets=8000,允许time_wait套接字数量的较大值,建议8000
net.ipv4.ip_local_port_range=1024 65000 udp和tcp端口取值范围
net.ipv4.tcp_syncookies=1 与性能无关用于解决tcp的syn攻击
net.ipv4.tcp_max_syn_backlog=8192 接收消息队列最大数值
net.ipv4.tcp_tw_recycle=1 用于timewait快速回收
net.ipv4.tcp_max_orphans=262114 限制放置简单的dos攻击
对客户端进行限制的相关配置:
18、limit_rate rate;
限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;
19、limit_except method ... { ... }
限制对指定的请求方法之外的其它方法的使用客户端;
limit_except GET {
allow 192.168.1.0/24;
deny all;
}
文件操作优化的配置
20、aio on | off | threads[=pool];
是否启用aio功能;
21、directio size | off;
在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;
22、open_file_cache off;
open_file_cache max=N [inactive=time];
nginx可以缓存以下三种信息:
(1) 文件的描述符、文件大小和最近一次的修改时间;
(2) 打开的目录结构;
(3) 没有找到的或者没有权限访问的文件的相关信息;
max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;
inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项;
23、open_file_cache_valid time;
缓存项有效性的检查频率;默认为60s;
24、open_file_cache_min_uses number;
在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项;
25、open_file_cache_errors on | off;
是否缓存查找时发生错误的文件一类的信息;
nginx的stream模块和upstream模块的更多相关文章
- nginx系列10:通过upstream模块选择上游服务器和负载均衡策略round-robin
upstream模块的使用方法 1,使用upstream和server指令来选择上游服务器 这两个指令的语法如下图: 示例: 2,对上游服务使用keepalive长连接 负载均衡策略round-rob ...
- Nginx Upstream模块源码分析(上)
Upstream模块是一个很重要的模块,很多其他模块都会使用它来完成对后端服务器的访问, 达到反向代理和负载均衡的效果.例如Fastcgi.Memcached.SessionSticky等. 如果自己 ...
- nginx upstream模块--负载均衡
Module ngx_http_upstream_module英文文档 upstream模块相关说明1.upstream模块应放于nginx.conf配置的http{}标签内2.upstream模块默 ...
- nginx upstream模块
upstream模块 upstream模块 (100%) nginx模块一般被分成三大类:handler.filter和upstream.前面的章节中,读者已经了解了handler.filter. 利 ...
- [转帖]nginx upstream模块--负载均衡
nginx upstream模块--负载均衡 https://www.cnblogs.com/linjiqin/p/5494783.html Module ngx_http_upstream_modu ...
- Nginx - upstream 模块及参数测试
目录 - 1. 前言- 2. 配置示例及指令说明 - 2.1 配置示例 - 2.2 指令 - 2.3 upstream相关变量- 3. 参数配置及测试 - 3.1 max_fa ...
- nginx之proxy、cache、upstream模块学习
nginx之proxy反向代理模块: location ^~ /proxy_path/ { root "/www/html"; 这里没必要配置 index index.html; ...
- 利用Nginx中的Upstream模块配置服务器负载均衡
1. 前言 nginx有一个最大的功能就是可以实现服务器的负载均衡,本篇博文就利用nginx中的upstream模块来配置一个简单的负载均衡.关于nginx的安装和配置文件可以查阅博文:windows ...
- 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能
2016年11月21日 10:30:00 xian_02 阅读数:10943 Nginx是一款轻量级高性能服务器软件,虽然轻量,但功能非常强大,可用于提供WEB服务.反向代理.负载均衡.缓存服务. ...
随机推荐
- SVM – 回归
SVM的算法是很versatile的,在回归领域SVM同样十分出色的.而且和SVC类似,SVR的原理也是基于支持向量(来绘制辅助线),只不过在分类领域,支持向量是最靠近超平面的点,在回归领域,支持向量 ...
- java Random 随机重排
将一个数组或序列随机重新排列. /** * Created by xc on 2019/11/23 * 随机重排 */ public class Test7_7 { public static voi ...
- LeetCode_485. Max Consecutive Ones
485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in th ...
- oracle 解决 exp 空表不能导出的问题
原因:oralce_11g 中有个新特性,当表无数据时,不分配 segment,以节省空间,这也就导致了 exp 在导出表时,没有数据的表会被忽略 方法一:我们可以向表中插入数据,在删除,这样数据表就 ...
- windows下的计算时间间隔 -- GetTickCount()
用法: #include "windows.h" DWORD lastTime =0;DWORD currentTime = 0;DWORD spendTime = 0; last ...
- 分布式事务的 N 种实现
转自:http://myfjdthink.com/2019/04/26/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1%E7%9A%84-n-%E7%A7% ...
- linux查看openssh和openssl版本
查看 openssh 版本命令 ssh -V 查看 openssl 版本命令 openssl version
- vue 项目不显示样式 排版错乱
vue中的css 样式都在index.html中 看这里是否有导入css
- Ubuntu 固定自己的IP
使用以下命令 sudo vi /etc/network/interfaces 以下方文件内容进行覆盖 # interfaces(5) file used by ifup(8) and ifdown( ...
- Jenkins版本迭代以及回滚
一.摘要 在上一篇文章,链接如下: https://www.cnblogs.com/xiao987334176/p/11434849.html 镜像打的是latest版,如果需要回滚的话,就比较麻烦了 ...