nginx _lua (缓存,proxy)

Posted q55091

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx _lua (缓存,proxy)相关的知识,希望对你有一定的参考价值。

1.安装
http://www.ttlsa.com/nginx/nginx-modules-ngx_lua/

 

2.常见问题

 

3.proxy应用基于lua(类似anyproxy)
proxy_pass http://$http_host;

body_filter_by_lua_file /data/init.lua;

local data = ngx.arg[1] or ""
local html = string.gsub(data,"618","6119")
ngx.arg[1] = html

 

4.缓存
./nginx -s reload(/data/nginx/sbin)

 

nginx.conf(/data/nginx/conf)

proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/temp_dir;
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30g;

 

vhosts(/data/nginx/conf/vhosts)

location ^~ /ad-test/ {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;


proxy_ignore_headers Expires Set-Cookie;
proxy_cache cache_one;
proxy_cache_valid 200 304 1h;
expires 3h;
#add_header Access-Control-Allow-Origin $referer_domain;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;

proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $http_host;
proxy_pass http://localhost:8080;

}

 

5.incudle
#定制proxy_cache的key,去除imei和sn等个性化参数。
set $custom_proxy_cache_key $host$uri$is_args$args;
include vhosts/customize_proxy_cache_key;
调用vhosts/customize_proxy_cache_key 生成 customize_proxy_cache_key
proxy_cache_key $custom_proxy_cache_key;

#vhosts/customize_proxy_cache_key:
if ( $custom_params ~ ^(.*)(&sn=[^&]+)(.*)$) {
set $a $1;
set $c $3;
set $custom_params "${a}${c}";
}
set $custom_proxy_cache_key $host$uri$is_args$custom_params;


6.log
(1)自定义log信息
log_format log_req_resp ‘$remote_addr - $remote_user [$time_local] ‘
‘"$request" $status $body_bytes_sent ‘

(2)加入log
access_log logs/host.access.log log_req_resp;
‘"$http_referer" "$http_user_agent" $request_time req_body:"$request_body" resp_body:"$resp_body"‘;

(3)配置 resp_body
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end



















































以上是关于nginx _lua (缓存,proxy)的主要内容,如果未能解决你的问题,请参考以下文章

nginx添加缓存

【NGINX入门】3.Nginx的缓存服务器proxy_cache配置

基于Nginx+Lua改进多级缓存

NGINX proxy 模块如何如何缓存动态页面内容?如何让缓存过期

基于nginx实现缓存功能

Nginx_lua缓存问题,关闭lua_code_cache