天天看點

nginx 用lua中擷取url路徑相關資訊配置的nginx.conf的配置檔案使用對照修改參數!!!

nginx 用lua中擷取url路徑相關資訊配置的nginx.conf的配置檔案使用對照修改參數!!!
nginx 用lua中擷取url路徑相關資訊配置的nginx.conf的配置檔案使用對照修改參數!!!

代碼:

ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
--擷取本地緩存
local cache_ngx = ngx.shared.dis_cache;
--根據ID 擷取本地緩存資料
local contentCache = cache_ngx:get('content_cache_'..id);

if contentCache == "" or contentCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    red:connect("192.168.211.132", 6379)
    local rescontent=red:get("content_"..id);

    if ngx.null == rescontent then
        local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "192.168.211.132",
            port = 3306,
            database = "changgou_content",
            user = "root",
            password = "123456"
        }
        local res = db:connect(props);
        local select_sql = "select url,pic from tb_content where status ='1' and category_id="..id.." order by sort_order";
        res = db:query(select_sql);
        local responsejson = cjson.encode(res);
        red:set("content_"..id,responsejson);
        ngx.say(responsejson);
        db:close()
    else
        cache_ngx:set('content_cache_'..id, rescontent, 10*60);
        ngx.say(rescontent)
    end
    red:close()
else
    ngx.say(contentCache)
end
           

配置的nginx.conf的配置檔案

user  root root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #定義Nginx緩存子產品,子產品名字叫dis_cache,容量大小128M
    lua_shared_dict dis_cache 128m;

    #限流設定
    limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;

    #根據IP位址來限制,存儲記憶體大小10M
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    #個人IP顯示
    limit_conn_zone $binary_remote_addr zone=perip:10m;
    #針對整個服務所有的并發量控制
    limit_conn_zone $server_name zone=perserver:10m;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
     #"虛拟機"
    server {
        listen       80;
        #監聽的域名
        server_name  localhost;

        #192.168.211.1
        location /brand {
         limit_conn perip 3;      #單個用戶端ip與伺服器的連接配接數.
            limit_conn perserver 5;  #限制與伺服器的總連接配接數
            #同一個IP隻允許有2個并發連接配接
            #limit_conn addr 2;
            #所有以/brand的請求,都将交給  192.168.211.1伺服器的18081程式處理.
            proxy_pass http://192.168.211.1:18081;
        }

        #表示所有以 localhost/read_content的請求都由該配置處理
        location /read_content {
            #使用指定限流配置,burst=4表示允許同時有4個并發連接配接,如果不能同時處理,則會放入隊列,等請求處理完成後,再從隊列中拿請求
            #nodelay 并行處理所有請求
            limit_req zone=contentRateLimit burst=4 nodelay;
            #content_by_lua_file:所有請求都交給指定的lua腳本處理(/root/lua/read_content.lua)
            content_by_lua_file /usr/local/server/lua65/read_content.lua;
        }

        #表示所有以 localhost/update_content的請求都由該配置處理
        location /update_content {
            #content_by_lua_file:所有請求都交給指定的lua腳本處理(/root/lua/update_content.lua)
            content_by_lua_file /usr/local/server/lua65/update_content.lua;
        }
    }
}

           

使用對照修改參數!!!

繼續閱讀