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模块的更多相关文章

  1. nginx系列10:通过upstream模块选择上游服务器和负载均衡策略round-robin

    upstream模块的使用方法 1,使用upstream和server指令来选择上游服务器 这两个指令的语法如下图: 示例: 2,对上游服务使用keepalive长连接 负载均衡策略round-rob ...

  2. Nginx Upstream模块源码分析(上)

    Upstream模块是一个很重要的模块,很多其他模块都会使用它来完成对后端服务器的访问, 达到反向代理和负载均衡的效果.例如Fastcgi.Memcached.SessionSticky等. 如果自己 ...

  3. nginx upstream模块--负载均衡

    Module ngx_http_upstream_module英文文档 upstream模块相关说明1.upstream模块应放于nginx.conf配置的http{}标签内2.upstream模块默 ...

  4. nginx upstream模块

    upstream模块 upstream模块 (100%) nginx模块一般被分成三大类:handler.filter和upstream.前面的章节中,读者已经了解了handler.filter. 利 ...

  5. [转帖]nginx upstream模块--负载均衡

    nginx upstream模块--负载均衡 https://www.cnblogs.com/linjiqin/p/5494783.html Module ngx_http_upstream_modu ...

  6. Nginx - upstream 模块及参数测试

    目录 - 1. 前言- 2. 配置示例及指令说明    - 2.1 配置示例    - 2.2 指令    - 2.3 upstream相关变量- 3. 参数配置及测试    - 3.1 max_fa ...

  7. nginx之proxy、cache、upstream模块学习

    nginx之proxy反向代理模块: location ^~ /proxy_path/ { root "/www/html"; 这里没必要配置 index index.html; ...

  8. 利用Nginx中的Upstream模块配置服务器负载均衡

    1. 前言 nginx有一个最大的功能就是可以实现服务器的负载均衡,本篇博文就利用nginx中的upstream模块来配置一个简单的负载均衡.关于nginx的安装和配置文件可以查阅博文:windows ...

  9. 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能

    2016年11月21日 10:30:00 xian_02 阅读数:10943   Nginx是一款轻量级高性能服务器软件,虽然轻量,但功能非常强大,可用于提供WEB服务.反向代理.负载均衡.缓存服务. ...

随机推荐

  1. 查找算法(5)--Tree table lookup--树表查找

    1.树表查找 (1) 最简单的树表查找算法——二叉树查找算法. [1]基本思想:二叉查找树是先对待查找的数据进行生成树,确保树的左分支的值小于右分支的值,然后在就行和每个节点的父节点比较大小,查找最适 ...

  2. 【APM】Pinpoint 安装部署(一)

    Pinpoint简介 Pinpoint是用Java / PHP编写的大规模分布式系统的APM(应用程序性能管理)工具.受Dapper的启发,Pinpoint提供了一种解决方案,可通过跟踪跨分布式应用程 ...

  3. 关于怎么提取m3u8地址

    摘自: https://blog.51cto.com/4373601/1920758 很长时间没有写博客了,这一段时间比较忙,接下来的日子要坚持写博客了,后期抽空会把这一年多的测试心得补上来,写博客其 ...

  4. maven多模块和继承

    https://blog.csdn.net/mafan121/article/details/50477852 1.maven 打包Could not resolve dependencies for ...

  5. Classic BAdi and New BAdi

    Former Member Classic BAdi and New BAdi ... 2007年04月27日 04:43 | 1.5k Views Hi all, I have a question ...

  6. 开发日记:Windows进程守护工具

    近期,中心应用服务无故关闭.在检查系统和应用程序日志无果后采取了进程守护的方法.测试期内,脚本未出现系统资源占用过多的情况. 使用说明:1.进程守护.vbs  使用时需修改运行周期(10行).守护进程 ...

  7. tomcat安全基线

    为了符合tomcat安全基线,需要做一下加固: 1.管理用户的密码加密:<摘要算法加密tomcat登录密码> 管理用户在conf/tomcat-users.xml中配置,密码一般是明文形式 ...

  8. 使用极光第三方IM的时候服务器报错Caused by: java.net.UnknownHostException: api.im.jpush.cn

    Caused by: java.net.UnknownHostException: api.im.jpush.cn 服务器报这个:首先查看服务器是否能ping通,如果能ping通,则看下 vi /et ...

  9. Python的编码规范

    7. 什么是 PEP8? 8号Python增强提案,是针对Python代码格式而编写的风格指南 8. 了解 Python 之禅么? 通过 import this 语句可以获取其具体的内容.它告诉大家何 ...

  10. 【springcloud】【idea】启动服务报错Command line is too long. Shorten command line for XXXApplication or also for Spring Boot default configuration.

    在workspace.xml 在标签<component name="PropertiesComponent">里 添加<property name=" ...