Nginx高级课程-高效

Nginx内存缓存

strace
一般应用为静态文件元数据信息缓存
sendfile执行过程

epoll_wait(8, [{EPOLLIN, {u32=1904243152, u64=140709327827408}}, {EPOLLIN, {u32=1904242704, u64=140709327826960}}], 512, 25215) = 2
recvfrom(10, "GET / HTTP/1.1\r\nHost: 192.168.44"..., 1024, 0, NULL, NULL) = 475
stat("/usr/local/nginx//html/index.html", {st_mode=S_IFREG|0644, st_size=1429, ...}) = 0
open("/usr/local/nginx//html/index.html", O_RDONLY|O_NONBLOCK) = 11
fstat(11, {st_mode=S_IFREG|0644, st_size=1429, ...}) = 0
writev(10, [{iov_base="HTTP/1.1 200 OK\r\nServer: nginx/1"..., iov_len=263}], 1) = 263
sendfile(10, 11, [0] => [1429], 1429) = 1429
write(4, "192.168.44.1 - - [27/May/2022:14"..., 193) = 193
close(11)

open_file_cache

open_file_cache max=500 inactive=60s
open_file_cache_min_uses 1;
open_file_cache_valid 60s;
open_file_cache_errors on 

max缓存最大数量,超过数量后会使用LRU淘汰
inactive 指定时间内未被访问过的缓存将被删除
pen_file_cache_min_uses
被访问到多少次后会开始缓存
open_file_cache_valid
间隔多长时间去检查文件是否有变化
open_file_cache_errors
对错误信息是否缓存 

Nginx外置缓存缓存

error_page
指定状态码

error_page 404 =302 http://www.atguigu.com;

默认指向location
匿名location
nginx + memcached
memcached安装
yum -y install memcached
默认配置文件在
/etc/sysconfig/memcached
查看状态

memcached-tool 127.0.0.1:11211 stats

nginx配置

 upstream backend {
# server 192.168.44.102 weight=8 down;
server 192.168.44.104:8080;
}
location / {
set $memcached_key "$uri?$args";
memcached_pass 127.0.0.1:11211;
 add_header X-Cache-Satus HIT;
add_header Content-Type 'text/html; charset=utf-8'; # 强制响应数据格式为html
# root html;
}

nginx + redis

Redis安装
7.0下载地址
https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.0.0
安装

依赖
yum install -y tcl-devel
解压
make
make install 

redis2-nginx-module
redis2-nginx-module是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis
服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。
https://www.nginx.com/resources/wiki/modules/redis2/
https://github.com/openresty/redis2-nginx-module
redis快速安装 

yum install epel-release
yum install -y redis

redis2-nginx-module 安装

test

 location = /foo {
default_type text/html;
redis2_query auth 123123;
set $value 'first';
 redis2_query set one $value;
redis2_pass 192.168.199.161:6379;
}

get

location = /get {
default_type text/html;
redis2_pass 192.168.199.161:6379;
redis2_query auth 123123;
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
redis2_query get $key;
}

set

# GET /set?key=one&val=first%20value
location = /set {
default_type text/html;
redis2_pass 192.168.199.161:6379;
redis2_query auth 123123;
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
set_unescape_uri $val $arg_val; # this requires ngx_set_misc
redis2_query set $key $val;

pipeline

set $value 'first';
redis2_query set one $value;
redis2_query get one;
 redis2_query set one two;
redis2_query get one;
redis2_query del key1; 

list

 redis2_query lpush key1 C;
redis2_query lpush key1 B;
redis2_query lpush key1 A;
redis2_query lrange key1 0 -1;

集群

upstream redis_cluster {
server 192.168.199.161:6379;
server 192.168.199.161:6379;
 }
location = /redis {
default_type text/html;
redis2_next_upstream error timeout invalid_response;
redis2_query get foo;
redis2_pass redis_cluster;

Stream模块

 限流
QPS限制
官方文档
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
测试工具
https://jmeter.apache.org/
配置

limit_req_zone $binary_remote_addr zone=test:10m rate=15r/s;
limit_req zone=req_zone_wl burst=20 nodelay;

日志
ngx_http_log_module
http://nginx.org/en/docs/http/ngx_http_log_module.html
ngx_http_empty_gif_module
http://nginx.org/en/docs/http/ngx_http_empty_gif_module.html
json

log_format ngxlog json '{"timestamp":"$time_iso8601",'
'"source":"$server_addr",'
'"hostname":"$hostname",'
'"remote_user":"$remote_user",'
'"ip":"$http_x_forwarded_for",'
'"client":"$remote_addr",'
 '"request_method":"$request_method",'
'"scheme":"$scheme",'
'"domain":"$server_name",'
'"referer":"$http_referer",'
'"request":"$request_uri",'
'"requesturl":"$request",'
'"args":"$args",'
'"size":$body_bytes_sent,'
'"status": $status,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamaddr":"$upstream_addr",'
'"http_user_agent":"$http_user_agent",'
'"http_cookie":"$http_cookie",'
'"https":"$https"'
'}';

 errorlog
http://nginx.org/en/docs/ngx_core_module.html#error_log
日志分割
1.脚本
2.Logrotate
重试机制
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream
max_fails
最大失败次数
0为标记一直可用,不检查健康状态
fail_timeout
失败时间
当fail_timeout时间内失败了max_fails次,标记服务不可用
fail_timeout时间后会再次激活次服务
proxy_next_upstream
proxy_next_upstream_timeout
重试最大超时时间
proxy_next_upstream_tries
重试次数,包括第一次
proxy_next_upstream_timeout时间内允许proxy_next_upstream_tries次重试
主动健康检查
tengine版
https://github.com/yaoweibin/nginx_upstream_check_module
nginx商业版
http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html
配置

upstream backend {
# server 192.168.44.102 weight=8 down;
server 192.168.44.104:8080;
server 192.168.44.105:8080;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
 location /status {
check_status;
access_log off;
}
location / {
proxy_pass http://backend;
root html;
}

  • 55
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狠情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值