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;
是否緩存查找時發生錯誤的文件一類的信息;