Skip to content

Instantly share code, notes, and snippets.

@miaozilong
Last active July 27, 2019 11:16
Show Gist options
  • Save miaozilong/4f3a0fdfac7e752d73f2db9ea24f611a to your computer and use it in GitHub Desktop.
Save miaozilong/4f3a0fdfac7e752d73f2db9ea24f611a to your computer and use it in GitHub Desktop.
nginx响应消息记录方法
local chunk, eof = ngx.arg[1], ngx.arg[2]
local buffered = ngx.ctx.buffered
if not buffered then
buffered = {}
ngx.ctx.buffered = buffered
end
if chunk ~= "" then
buffered[#buffered + 1] = chunk
ngx.arg[1] = nil
end
if eof then
local whole = table.concat(buffered)
ngx.ctx.buffered = nil
ngx.arg[1] = whole
ngx.var.resp_body = ngx.arg[1]
end
#user nobody;
worker_processes 4;
#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;
#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 mylog escape=none '浏览器IP地址:$remote_addr|本地时间:$time_iso8601|请求方法:$request_method|请求URI:$request_uri|状态码:$status|响应时间:$upstream_response_time|请求体:$request_body|响应体:$resp_body|响应数据大小:$bytes_sent|响应体大小:$body_bytes_sent|客户端代理:$http_user_agent';
log_escape_non_ascii off;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $date $1$2$3;
}
set $resp_body "";
lua_need_request_body on;
body_filter_by_lua_file lua/log_by.lua;
access_log logs/access_$date.log mylog;
location / {
root html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://192.168.1.42:8080/configmanage;
access_log logs/access_api_$date.log mylog;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment