dockerインストールopenresty

11081 ワード

dockerインストールopenresty
  • 公式ミラーを引き出して実行
  • docker run --name openresty -p 80:80 -p 443:443 -di  openresty/openresty
    
  • アクセステスト
  • http://121.199.79.7/    
    
  • コンテナ修正ファイル
  • docker exec -it openresty /bin/bash
    
  • 容器取付vi
  • apt-get update
    apt-get -y install vim
    

    キャッシュウォームアップ
  • スクリプトの作成
  • cd /root
    mkdir lua
    cd lua
    vim ad_update.lua
    #   vim         esc-》:-》set mouse-=a             
    #   mysql redis    ,   docker     ,     ip
    
    ngx.header.content_type="application/json;charset=utf8"
    local cjson = require("cjson")
    local mysql = require("resty.mysql")
    local uri_args = ngx.req.get_uri_args()
    local position = uri_args["position"]
    
    local db = mysql:new()
    db:set_timeout(1000)  
    local props = {  
        host = "121.199.79.7",  
        port = 3306,  
        database = "changgou_business",  
        user = "root",  
        password = "123456"  
    }
    
    local res = db:connect(props)  
    local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"  
    res = db:query(select_sql)  
    db:close()  
    
    local redis = require("resty.redis")
    local red = redis:new()
    red:set_timeout(2000)
    
    local ip ="121.199.79.7"
    local port = 6379
    red:connect(ip,port)
    
    red:set("ad_"..position,cjson.encode(res))
    red:close()
    
    ngx.say("{flag:true}")
    
  • nginx構成情報の変更
  • vim /usr/local/openresty/nginx/conf/nginx.conf
    #        
    user root root;
    #       include /etc/nginx/conf.d/*.conf;
    #        server  
    
    #user  nobody;
    user root root;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
            charset utf-8;
            #access_log  logs/host.access.log  main;
            #   
            location /ad_update {
                content_by_lua_file /root/lua/ad_update.lua;
            }
            
            # redirect server error pages to the static page /50x.html
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }        
        }
    }
    
  • 再起動nginx
  • /usr/local/openresty/nginx/sbin/nginx -s reload
    
  • テスト
  • http://192.168.31.42/ad_update?position=web_index_lb
    #        {flag:true}
    #           
    

    広告キャッシュ読み込み
  • スクリプトの作成
  • vim /root/lua/ad_read.lua
    #     set mouse-=a      
    #   redis     
    
    --       
    ngx.header.content_type="application/json;charset=utf8"
    --        ID
    local uri_args = ngx.req.get_uri_args();
    local position = uri_args["position"];
    --  redis 
    local redis = require("resty.redis");
    --  redis  
    local red = redis:new()
    --      
    red:set_timeout(2000)
    --  
    local ok, err = red:connect("121.199.79.7", 6379)
    --  key  
    local rescontent=red:get("ad_"..position)
    --        
    ngx.say(rescontent)
    --    
    red:close()
    
  • nginx構成の変更
  • #  /usr/local/openresty/nginx/conf/nginx.conf server     
    vim /usr/local/openresty/nginx/conf/nginx.conf
    
            location /ad_read {
                content_by_lua_file /root/lua/ad_read.lua;
            }
    
  • 再起動nginx
  • /usr/local/openresty/nginx/sbin/nginx -s reload
    
  • テスト
  • http://192.168.31.42/ad_read?position=web_index_lb 
    

    2次キャッシュ-openrestyローカルキャッシュへの追加
  • スクリプトの作成
  • #     read  
    vim /root/lua/ad_read.lua
    #      
    #   set mouse-=a     
    #      redis     
    
    --       
    ngx.header.content_type="application/json;charset=utf8"
    --        ID
    local uri_args = ngx.req.get_uri_args();
    local position = uri_args["position"];
    
    --      
    local cache_ngx = ngx.shared.dis_cache;
    --  ID         
    local adCache = cache_ngx:get('ad_cache_'..position);
    
    if adCache == "" or adCache == nil then
    
    	--  redis 
    	local redis = require("resty.redis");
    	--  redis  
    	local red = redis:new()
    	--      
    	red:set_timeout(2000)
    	--  
    	local ok, err = red:connect("121.199.79.7", 6379)
    	--  key  
    	local rescontent=red:get("ad_"..position)
    	--        
    	ngx.say(rescontent)
    	--    
    	red:close()
    	-- redis         nginx    
    	cache_ngx:set('ad_cache_'..position, rescontent, 10*60);
    else
     	--nginx              
     	ngx.say(adCache)
    end
    
  • nginx構成の変更
  • #   nginx    ,http       :
    vim /usr/local/openresty/nginx/conf/nginx.conf
    #  http         5m      
    
    #  redis     
    lua_shared_dict dis_cache 5m;  #      
    
  • 再起動nginx
  • /usr/local/openresty/nginx/sbin/nginx -s reload
    
  • テスト
  • http://192.168.31.42/ad_read?position=web_index_lb 
    

    nginx限流
  • 均一速限流
  • docker exec -it openresty /bin/bash
    
    vim /usr/local/openresty/nginx/conf/nginx.conf
    
    #  http        ,     
    
     #        $binary_remote_addr:    ip    myRateLimit:    10m rate:    2   
     limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=2r/s;
    
    #  server location        
    limit_req zone=myRateLimit;
    
    #     
    # nginx.conf  --  docker-openresty
    #
    # This file is installed to:
    #   `/usr/local/openresty/nginx/conf/nginx.conf`
    # and is the file loaded by nginx at startup,
    # unless the user specifies otherwise.
    #
    # It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
    # section and adds this directive:
    #     `include /etc/nginx/conf.d/*.conf;`
    #
    # The `docker-openresty` file `nginx.vh.default.conf` is copied to
    # `/etc/nginx/conf.d/default.conf`.  It contains the `server section
    # of the upstream `nginx.conf`.
    #
    # See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
    #
    
    #user  nobody;
    user root root;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #    dis_cache       ,     5m。
        lua_shared_dict dis_cache 5m; 
    
         #       
         limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=2r/s;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        # See Move default writable paths to a dedicated directory (#119)
        # https://github.com/openresty/docker-openresty/issues/119
        client_body_temp_path /var/run/openresty/nginx-client-body;
        proxy_temp_path       /var/run/openresty/nginx-proxy;
        fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
        uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
        scgi_temp_path        /var/run/openresty/nginx-scgi;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
            charset utf-8;
            #access_log  logs/host.access.log  main;
            #   
            location /ad_update {
                content_by_lua_file /root/lua/ad_load.lua;
            }
    
            location /ad_read {
                limit_req zone=myRateLimit;
                content_by_lua_file /root/lua/ad_read.lua;
            }
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    docker安装openresty_第1张图片1 sは2回リフレッシュするとストリームが制限され、500 msごとに1つのリクエストを処理することを表し、500 ms内に複数のリクエストがある場合はdocker安装openresty_第2张图片を拒否する
  • 処理バーストトラフィック
  • docker exec -it openresty /bin/bash
    
    vim /usr/local/openresty/nginx/conf/nginx.conf
    
    #  server location                  burst nodelay
    limit_req zone=myRateLimit  burst=5 nodelay;
    #          2   ,     5   ,      5      ,    ,      ,         。
    #   nginx
     /usr/local/openresty/nginx/sbin/nginx -s reload
    
  • nginxエージェントgoods
  • docker安装openresty_第3张图片すべての/brandリクエストを自機のgoodsサービスdocker安装openresty_第4张图片制限毎秒2リクエストdocker安装openresty_第5张图片
    最後に、プレッシャーリクエストテストのソフトウェアjmeterをお勧めします.
    #   
    http://jmeter.apache.org/download_jmeter.cgi
    #   
       bin/jmeter.bat
    #     
       bin/jmeter.properties
       40   
     language=Zh_CN
    

    使用
    docker安装openresty_第6张图片