整体思路
在nginx配置接收请求时间,同时打印出请求时间、上游处理时间。然后在前端将业务操作的时间放在request header里面,并且将其打印在nginx的日志中。将用户操作的时间,接到请求的时间和处理完的时间一比较自然就清楚地知道,是哪个地方出了问题,包括网络、程序是否有问题。
1、配置格式
在http配置
这里配置打印的格式,
请求时长: rt=$request_time ,
上游处理时长:urt=$upstream_response_time ,
自定义request head: mt=$http_myrequestdate';
这里要特别注意:前面要加上http_,要不然会报变量找不到。
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'rt=$request_time urt=$upstream_response_time mt=$http_myrequestdate';
2、在service配置上自己的日志
access_log /usr/local/nginx/logs/accesslog9092.log main;
3、给出一份完整的代码
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 3072;
}
http {
include mime.types;
default_type application/octet-stream;
#这里配置打印的格式,请求时长: rt=$request_time ,上游处理时长:urt=$upstream_response_time ,自定义request head: mt=$http_myrequestdate';
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'rt=$request_time urt=$upstream_response_time mt=$http_myrequestdate';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript;
server {
listen 9092;
server_name localhost;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
#charset koi8-r;
#配置日志的地方
access_log /usr/local/nginx/logs/accesslog9092.log main;
location / {
root /usr/local/nginx/html/xxxxx;
#解决vue项目刷新页面找不到静态文件报404
try_files $uri $uri/ @router;
index index.html index.htm;
#请求头添加真实IP
proxy_set_header host $host;
#设置代理服务器ip头,代码获取时的参数
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
#允许将发送到被代理服务器的请求头重新定义或者增加一些字段,显示真实的客户端的IP
proxy_set_header X-Real-IP $remote_addr;
}
location /edit-label {
root /usr/local/nginx/html/xxxxx;
#解决vue项目刷新页面找不到静态文件报404
try_files $uri $uri/ @router;
index index.html index.htm;
#请求头添加真实IP
proxy_set_header host $host;
#设置代理服务器ip头,代码获取时的参数
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
#允许将发送到被代理服务器的请求头重新定义或者增加一些字段,显示真实的客户端的IP
proxy_set_header X-Real-IP $remote_addr;
}
location /ltza {
proxy_pass http://127.0.0.1:8080/xxxx;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
}
测试结果会是:

3225

被折叠的 条评论
为什么被折叠?



