阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx中使用srcache_nginx模块构建缓存

237次阅读
没有评论

共计 9419 个字符,预计需要花费 24 分钟才能阅读完成。

nginx 中可以将 lua 嵌,让 nginx 执行 lua 脚本,可以处理高并发,非阻塞的处理各种请求,openresty 项目中可以使用 nignx 可以直接构建 srcache_nginx + redis 缓存,而不用通过动态语言来处理(QPS 可以轻松的提高了)

Nginx 中使用 srcache_nginx 模块构建缓存

看一下 openresty 中 srcache-nginx-module 的工作流

好了废话不多说

一、安装

pcre

cd /usr/local/src

wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

tar zxf pcre-8.38.tar.gz

drizzle7

cd /usr/local/src/

wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz

tar xzvf drizzle-2011.07.21.tar.gz

cd drizzle-2011.07.21/

./configure

make

make install

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

JIT(Just-In-Time Compiler)

wget -c http://luajit.org/download/LuaJIT-2.0.2.tar.gz

tar xzvf LuaJIT-2.0.2.tar.gz

cd LuaJIT-2.0.2

make install PREFIX=/usr/local/luajit

echo “/usr/local/luajit/lib” > /etc/ld.so.conf.d/usr_local_luajit_lib.conf

ldconfig

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

nginx

cd /usr/local/src

wget -c http://nginx.org/download/nginx-1.9.9.tar.gz

git clone https://github.com/simpl/ngx_devel_kit.git

git clone https://github.com/openresty/set-misc-nginx-module.git

git clone https://github.com/openresty/memc-nginx-module.git

git clone https://github.com/openresty/echo-nginx-module.git

git clone https://github.com/openresty/lua-nginx-module.git

git clone https://github.com/openresty/srcache-nginx-module.git

git clone https://github.com/openresty/drizzle-nginx-module.git

git clone https://github.com/openresty/rds-json-nginx-module.git

wget http://people.freebsd.org/~osa/ngx_http_redis-0.3.7.tar.gz

tar zxf nginx-1.9.9.tar.gz

cd nginx-1.9.9

 

./configure \

–prefix=/usr/local/nginx-1.9.9 \

–add-module=../memc-nginx-module \

–add-module=../srcache-nginx-module \

–add-module=../ngx_devel_kit \

–add-module=../ngx_image_thumb \

–add-module=../redis2-nginx-module \

–add-module=../echo-nginx-module  \

–add-module=../lua-nginx-module \

–add-module=../set-misc-nginx-module \

–add-module=../ngx_http_redis-0.3.7 \

–with-pcre=../pcre-8.38 \

–with-pcre-jit

make && make install

redis

cd /usr/local

wget http://download.redis.io/releases/redis-3.0.6.tar.gz

tar zxf redis-3.0.6.tar.gz

cd redis-3.0.6

make

./src/redis-server &

配置文件

daemonize yes

pidfile /var/run/redis-6379.pid

port 6379

bind 127.0.0.1

timeout 0

tcp-keepalive 0

loglevel notice

logfile stdout

databases 16

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

maxmemory 8096mb   

maxmemory-policy volatile-ttl

appendonly no

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

nginx 的简单配置

user  www www;

worker_processes  auto;

 

error_log  logs/error.log  info;

 

pid        logs/nginx.pid;

 

 

events {

    use epoll;

    worker_connections  65536;

}

 

 

http {

    include      mime.types;

    default_type  application/octet-stream;

    charset  utf-8;

 

    log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

        ‘$status $body_bytes_sent “$http_referer” ‘

        ‘”$http_user_agent” “$http_x_forwarded_for”‘;

 

    log_format srcache_log ‘$remote_addr – $remote_user [$time_local] “$request” ‘

        ‘”$status” $body_bytes_sent $request_time $bytes_sent $request_length ‘

      ‘[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]’;

   

    server_tokens off;

 

    keepalive_timeout          60 20;

    client_header_timeout      3m;

    client_body_timeout        3m;

    send_timeout                3m;

 

    client_header_buffer_size          16k;

    large_client_header_buffers        4 32k;

    server_names_hash_max_size          512;

    server_names_hash_bucket_size      64;

 

    sendfile        on;

    tcp_nopush      on;

    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;

 

    upstream memcache {

        server 192.168.1.30:12000;

        keepalive 10;

    }

    upstream redis {

        server 127.0.0.1:6379;

        keepalive 20;

    }

 

    server

    {

        listen 90 default;

        server_name _;

        return 444;

    }

    include vhosts/*.conf;

}

二、srcache+memcache

server {

    listen      8099;

    server_name  192.168.1.30;

    root /data/www;

    index  index.php index.html index.htm;

    default_type text/plain;

 

    access_log  logs/host.access.log  main;

 

    location /hello{

        echo “This is a test”;

    }

    location = /lua-version {

        content_by_lua ‘

            if jit then

                ngx.say(jit.version)

            else

                ngx.say(_VERSION)

                    end

          ‘;

    }

 

 

    location /memc {

        internal;

        memc_connect_timeout 100ms;

        memc_send_timeout 100ms;

        memc_read_timeout 100ms;

        set $memc_key $query_string;

        set $memc_exptime 300;

        memc_pass memcache;

    }

 

    location ~ \.php$ {

        charset        utf-8;

        default_type  text/html;

        set $key $uri$args;

        srcache_fetch GET /memc $key;

        srcache_store PUT /memc $key;

        add_header X-Cached-From $srcache_fetch_status;

        add_header X-Cached-Store $srcache_store_status;

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index  index.php;

        include        fastcgi_params;

        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

 

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  html;

    }

}

第一次访问的时候

Connection:keep-alive

Content-Encoding:gzip

Content-Type:text/html; charset=UTF-8

Date:Wed, 20 Jan 2016 16:32:32 GMT

Keep-Alive:timeout=20

Server:nginx

Transfer-Encoding:chunked

Vary:Accept-Encoding

X-Cached-From:<strong>MISS</strong>

X-Cached-Store:STORE

第二次访问

Connection:keep-alive

Content-Encoding:gzip

Content-Type:text/html; charset=UTF-8

Date:Wed, 20 Jan 2016 16:33:17 GMT

Keep-Alive:timeout=20

Server:nginx

Transfer-Encoding:chunked

Vary:Accept-Encoding

X-Cached-From:<strong>HIT</strong>

X-Cached-Store:BYPASS

可以自定义哪些需要访问

三、srcache+redis

redis 配置测试

server {

    listen      9001;

    server_name  192.168.1.30;

    root /data/www;

    index  index.php index.html index.htm;

    default_type text/plain;

    access_log  logs/host.access.log  main;

 

    location /testx{

        echo ‘1’;

    }

 

    location ~ .*\.php {

        srcache_store_private on;

        srcache_methods GET;

        srcache_response_cache_control off;

 

        set $key $uri;

        set_escape_uri $escaped_key $key;

        srcache_default_expire 172800;

        srcache_fetch GET /redis_get $key;

        srcache_store PUT /redis_set key=$escaped_key&exptime=$srcache_expire;

 

        add_header X-Cached-From $srcache_fetch_status;

        set_md5 $md5key $key;

        add_header X-md5-key $md5key;

        add_header X-Cached-Store $srcache_store_status;

        add_header X-Key $key;

        add_header X-Query_String $query_string;

        add_header X-expire $srcache_expire;

        add_header X-uri $uri;

        access_log logs/9001-access.log srcache_log;

 

        include fastcgi_params;

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_connect_timeout 60;

        fastcgi_send_timeout 180;

        fastcgi_read_timeout 180;

        fastcgi_buffer_size 128k;

        fastcgi_buffers 4 256k;

        fastcgi_busy_buffers_size 256k;

        fastcgi_temp_file_write_size 256k;

        fastcgi_intercept_errors on;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        fastcgi_split_path_info ^(.+\.php)(.*)$;

        fastcgi_param PATH_INFO $fastcgi_path_info;

    }

 

    location = /redis_get {

        internal;

        set_md5 $redis_key $args;

        redis_pass redis;

    }

    location =/show{

        echo $request_uri;

        echo $args;

    }

 

    location = /redis_set {

        internal;

 

        set_unescape_uri $exptime $arg_exptime;

        set_unescape_uri $key $arg_key;

        set_md5 $key;

 

        redis2_query set $key $echo_request_body;

        redis2_query expire $key $exptime;

        redis2_pass redis;

    }

 

    location = /one {

        set $value ‘first’;

        redis2_query set one $value;

        redis2_pass redis;

    }

 

    location = /get {

        set_unescape_uri $key $arg_key;  # this requires ngx_set_misc

        redis2_query get $key;

        redis2_pass redis;

    }

 

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  html;

    }

}

可以查看日志缓存是否命中

四、lua

Nginx 下 Lua 处理阶段与使用范围

init_by_lua            http

set_by_lua            server, server if, location, location if

rewrite_by_lua        http, server, location, location if

access_by_lua          http, server, location, location if

content_by_lua        location, location if

header_filter_by_lua  http, server, location, location if

body_filter_by_lua    http, server, location, location if

log_by_lua            http, server, location, location if

timer

lua 代码

ngx.req.read_body()  — explicitly read the req body

local data = ngx.req.get_body_data()

if data then

    ngx.say(“body data:”)

    ngx.print(data)

    return

end

 

— body may get buffered in a temp file:

local file = ngx.req.get_body_file()

if file then

    ngx.say(“body is in file “, file)

else

    ngx.say(“no body found”)

end

 

local res = ngx.location.capture(“/foo/index.php”)

if res then

  ngx.say(“status: “, res.status)

  ngx.say(“body:”)

  ngx.print(res.body)

end

nginx 中配置

location /lua_test {

        content_by_lua_file conf/lua_test.lua;                                                                                     

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/127790.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9419字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中