nginx 各配置

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

2.常见问题

(1) nginx.pid" failed (2: No such file or directory)
nginx.pid" failed (2: No such file or directory

(2)bad uri no resolver defined to resolve XXX.XXXX.COM
打开 vim /etc/resolv.conf
可以看到一条或多条记录
nameserver xx.xx.xx.xx
在nginx的配置里http或server里加上
resolver xx.xx.xx.xx yy.yy.yy.yy;

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
原文地址:https://www.cnblogs.com/season-xie/p/7366916.html