在http的功能里添加log_format模块,内容如下:
log_format main escape=json '{ "@timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr",' '"request_time": "$request_time",' '"upstream_response_time": "$upstream_response_time",' '"status": $status,' '"x_forwarded": "$http_x_forwarded_for",' '"http_referer": "$http_referer",' '"request": "$request",' '"upstream_addr": "$upstream_addr",' '"body_bytes_sent":$body_bytes_sent,' '"request_body":$request_body,' '"http_user_agent": "$http_user_agent" }'; access_log /usr/local/openresty/nginx/logs/access.log main;
########################################以下是一个完整的nginx.conf文件的配置与说明#########################
user nginx nginx; #启动用户
worker_processes 4; #nginx的进程数,建议和cpu核数一致 #error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#error_log logs/error.log warn; #全局LOG定义pid logs/nginx.pid; # 进程文件ID
worker_rlimit_nofile 65535; #nginx进程打开的文件数
#工作模式与连接数配置events { use epoll; # epoll是高版本内核优化后的网络I/O模型 worker_connections 65535; #单个进程最大连接数 multi_accept on; #打开快速接收新连接}#http服务设置
http { include mime.types;#文件扩展名与类型映射表 default_type application/octet-stream;#默认文件类型charset utf-8;#默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小 client_header_buffer_size 32k; #上传文件大小限制 large_client_header_buffers 4 32k; #设定请求缓存数 client_max_body_size 32m; #设定请求缓存大小 #include proxy.conf; #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;
#resolver 8.8.8.8; sendfile on; #开启高效文件传输 tcp_nopush on; #防止网络阻塞 tcp_nodelay on;#keepalive_timeout 0;
keepalive_timeout 30; #长连接超时时间,默认单位秒 ####解决跨域问题########### add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With,Content-Type,If-Modified-Since; add_header Access-Control-Allow-Methods GET,POST,OPTIONS;#以下是FastCGI 的相关参数,主要作用减少资源占用优化网站性能提高访问速度
fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 64k;set_real_ip_from 0.0.0.0/0;real_ip_header X-Forwarded-For;open_file_cache max=10240 inactive=20s;open_file_cache_min_uses 1;open_file_cache_valid 30s; #gizp优化配置,加速传速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;gzip_disable msie6;proxy_redirect off;
proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;proxy_max_temp_file_size 0;proxy_connect_timeout 90;proxy_send_timeout 90;proxy_read_timeout 90;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k; log_format main escape=json '{ "@timestamp": "$time_iso8601", ' '"remote_addr": "$remote_addr",' '"request_time": "$request_time",' '"upstream_response_time": "$upstream_response_time",' '"status": $status,' '"x_forwarded": "$http_x_forwarded_for",' '"http_referer": "$http_referer",' '"request": "$request",' '"upstream_addr": "$upstream_addr",' '"body_bytes_sent":$body_bytes_sent,' '"request_body":$request_body,' '"http_user_agent": "$http_user_agent" }';access_log /usr/local/openresty/nginx/logs/access.log main;error_log /usr/local/openresty/nginx/logs/error.log;# 虚拟主机配置我这定义到了/usr/local/openresty/nginx/conf/vhosts目录下include vhosts/*.conf;}