美文网首页技术分享
nginx优化以及多级缓存

nginx优化以及多级缓存

作者: dark68 | 来源:发表于2021-05-15 08:49 被阅读0次

1 nginx优化

配置文件:

user nginx; #用户
worker_processes auto;#进程数量 推荐auto,自动去服务器的cpu核心数
error_log /var/log/nginx/error.log  error;
pid /var/run/nginx.pid;
worker_rlimit_nofile 204800;
events
{
    use epoll;#使用Linux中效率最高的epoll
    worker_connections 204800;
}

http
{
    include mime.types;#nginx支持的文件类型
    default_type application/octet-stream;
    charset utf-8;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
    keys_zone=TEST:10m
    inactive=5m;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 4k;
    fastcgi_buffers 8 4k;
    fastcgi_busy_buffers_size 8k;
    fastcgi_temp_file_write_size 8k;
    fastcgi_cache TEST;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;

    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;

    tcp_nodelay on;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
}

2 多级缓存

2.1 lua介绍

lua是一个小巧的脚本语言,由标准C编写而成,几乎在所有操作系统和平台上都可以编译运行。其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。

应用场景:游戏开发、独立应用脚本、redis中嵌套调用实现类似事务的功能,web容器汇总处理NGINX的过滤缓存等等逻辑

2.2 openresty介绍

OpenResty是一个基于Nginx与Lua的高性能web平台,由中国人章亦春发起,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。用于方便搭建能处理超高并发、扩展性极高的动态Web应用、web服务和动态网关

OpenResty简单理解成就相当于封装了NGINX,并且集成了LUA脚本,开发人员只需要简单的使用其提供了模块就可以实现相关的逻辑,而不像之前,还需要在NGINX中编写lua的脚本。

2.3 openresty的安装(docker)

1.拉取一个openresty的镜像

docker pull openresty/openresty

2.随便构建一个容器用于拷贝配置文件

#指定端口90,防止与之前的容器冲突
docker run -p 90:90 -d --name openresty openresty/openresty

3.进入容器,查找配置文件路径(这里直接给出)

docker exec -it openresty bash

cd /etc/nginx/conf.d

4.退出容器,复制容器中配置文件到宿主机

docker cp openresty:/etc/nginx/conf.d/default.conf /docker/openresty/conf/default.conf

#停止并删除之前容器,稍后进行挂载
docker stop openresty
docker rm openresty

docker run -p 90:90 -d --name openresty -v /docker/openresty/conf/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www --privileged=true openresty/openresty

5.修改配置文件

#指定共享空间,后面lua脚本使用
lua_shared_dict dis_cache 10m;
server
{
    listen       90;
    listen       [::]:90;
    server_name  localhost;
    root /docker/www/webserver;
    index index.html;

    location /lmrs_home_index {
       content_by_lua_file /docker/www/lua/lmrs_home_index.lua;
    }
}
2.2 首页多级缓存策略(这里以商城首页的分类数据为例)

1、使用Lua查询Nginx缓存,如果有缓存,则直接将缓存中的分类数据返回

2、如果Nginx缓存中没有分类数据,则通过Lua脚本查询Redis,如果Redis中有数据,则将数据存入到Nginx缓存中,并返回查询到的数据

3、如果Redis中也没有缓存,则此时通过Lua脚本查询Mysql,如果Mysql中有数据,将分类数据存入到Redis缓存,并返回数据

ngx.header.content_type = "application/json;charset=utf8"
local cache_ngx = ngx.shared.dis_cache;
local contentCache = cache_ngx:get("lmrs_home_index");
if contentCache == "" or contentCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    red:connect("redis容器的ip", 6379)
    local rescontent = red:get("lmrs_home_index");
    if ngx.null == rescontent or false == rescontent or "" == rescontent then
        local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "mysql容器的ip",
            port = 3306,
            database = "database",
            user = "db_user",
            password = "db_password "
        }
        local res = db:connect(props);
        local select_sql = "select * from lmrs_product_categorys"
        res = db:query(select_sql);
        local responsejson = cjson.encode(res);
        red:set("lmrs_home_index", responsejson);
        ngx.say(responsejson);
        db:close()
        else
        cache_ngx:set("lmrs_home_index", rescontent, 10 * 60);
        ngx.say(rescontent)
    end
    red:close()
    else
    ngx.say(contentCache)
end

相关文章

  • nginx优化以及多级缓存

    1 nginx优化 配置文件: 2 多级缓存 2.1 lua介绍 lua是一个小巧的脚本语言,由标准C编写而成,几...

  • Day81 订单交易全链路优化

    多级缓存 CDN资源 nginx lvs和shared dict模块,提前将缓存数据放到nginx的shared模...

  • 第一章-并发基础

    cpu多级缓存- 缓存一致性 cpu多级缓存- 乱序执行优化 JAVA内存模型【java Memory Model...

  • CPU缓存及内存屏障

    CPU 性能优化手段 - 缓存 CPU 缓存模型: 多级缓存 L1 Cache (一级缓存) CPU第一级高速缓存...

  • 你的系统是怎样支持高并发的?-多级缓存架构

    ​ 目录 ① 多级缓存使用场景 ② 多级缓存读写逻辑 ③缓存预热 ④总结 1 多级缓存使用场景 多级缓存适合用在对...

  • nginx优化

    nginx优化并发优化长连接压缩静态缓存一、并发优化nginx工作模式:主进程+工作进程 启动工作进程数量work...

  • 写入操作优化

    一般我们读的操作可以通过CDN缓存,nginx缓存,分布式缓存来提高读取性能。那么写入操作的话就没法采用这么多级缓...

  • 多级缓存之Nginx.

    Caffeine既然是缓存的一种,肯定需要有缓存的清除策略,不然的话内存总会有耗尽的时候。 Caffeine提供了...

  • 02章 并发基础

    CPU多级缓存 - 缓存一致性 用于保证多个CPU cache之间缓存共享数据的一致 CPU多级缓存 - 乱序执行...

  • NGINX----模块----缓存

    如果有人问你怎么优化网站,那么你一定要告诉他使用缓存,nginx有着简单方便的缓存机制。 存放放在哪? nginx...

网友评论

    本文标题:nginx优化以及多级缓存

    本文链接:https://www.haomeiwen.com/subject/gimqjltx.html