nginx优化以及多级缓存

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容