当前位置: 首页 > news >正文

网站优化公司价格如何计算网站查询地址

网站优化公司价格如何计算,网站查询地址,西安网络公司排名前十名,咸宁网站定制需求、日志按照天的单位进行分割存储。 如果你直接百度,可能会搜到很多教你用各种脚本或是三方插件来按天分割的,这边我用nginx服务本身来分割日志。 方法一 通过使用 $time_iso8601 变量和 map 指令,实现了日志文件按天分割的功能。以下是…

需求、日志按照天的单位进行分割存储。

如果你直接百度,可能会搜到很多教你用各种脚本或是三方插件来按天分割的,这边我用nginx服务本身来分割日志。

方法一

通过使用 $time_iso8601 变量和 map 指令,实现了日志文件按天分割的功能。以下是详细的说明

1、map指令

map $time_iso8601 $logdate{'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;default 'date-not-found';
}
  • 作用: map 指令将 $time_iso8601 变量映射为 $logdate 变量,其中 $time_iso8601 是 Nginx 内置的变量,表示请求的时间,格式为 ISO 8601(例如 2024-08-13T20:35:06+08:00)。

  • 正则表达式: ~^(?<ymd>\d{4}-\d{2}-\d{2}) 用于从 $time_iso8601 中提取日期部分(例如 2024-08-13)。正则表达式中的 (?<ymd>\d{4}-\d{2}-\d{2}) 捕获年-月-日部分,并将其赋值给变量 $ymd

  • 映射结果: 提取的日期部分被映射为 $logdate 变量,这个变量将用于日志文件的命名。

2、日志文件存储

access_log /data/logs/access_api.json_$logdate json;
error_log /data/logs/api-error.log$logdate error;
  • access_log: 配置了访问日志的路径,并使用 $logdate 变量生成按天分割的日志文件名。日志文件的完整路径格式为 /data/logs/access_api.json_YYYY-MM-DD,其中 YYYY-MM-DD 是日期。

  • error_log: 错误日志的路径未使用 $logdate,因此错误日志不会按天分割。

3、工作流程

  • 每当有请求到达时,Nginx 会自动根据请求的时间,使用 map 指令提取日期部分,生成 $logdate 变量。
  • 访问日志会记录在以当前日期命名的日志文件中,如 access_api.json_2024-08-06
  • 随着日期的变化,$logdate 变量会自动更新,新的日志将记录到新的一天的日志文件中。

总结

通过 map 指令结合正则表达式,从 $time_iso8601 中提取日期部分,并使用该日期生成动态的日志文件名,从而实现了日志的按天分割功能。每一天都会生成一个新的日志文件,方便日后的日志分析与管理。

这里记得注意启动nginx的用户权限是否 有配置日志路径下的权限,记得日志写入权限的验证。

配置文件完整示例

user  app;
worker_processes  2;
events {worker_connections  30000;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;
###日志格式log_format json  escape=json '{"timestamp":"$time_iso8601",''"remote_addr": "$remote_addr", ''"referer": "$http_referer", ''"request": "$request", ''"status": $status, ''"bytes": $body_bytes_sent, ''"agent": "$http_user_agent", ''"x_forwarded": "$http_x_forwarded_for", ''"up_addr": "$upstream_addr",''"up_host": "$upstream_http_host",''"up_resp_time": "$upstream_response_time",''"request_time": "$request_time",''"request_GlobalId": "$http_GlobalId",''"response_GlobalId": "$sent_http_GlobalId"''"response_body": "$resp_body",''"request_body": "$request_body"'' }';
###日志按天分割map $time_iso8601 $logdate{'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;default 'date-not-found';}
###charset  utf-8 ;gzip  on;
# 后端IP地址
upstream api-prod {server 10.66.66.88:8501 max_fails=5 fail_timeout=30s;server 10.66.66.66:8501 max_fails=5 fail_timeout=30s;
}server {listen       80 ;listen       7309 ;server_name  api.xxxxxx.cn api-test.xxxxx.cn;charset  utf-8 ;#日志配置lua_need_request_body on;set $resp_body "";body_filter_by_lua 'local resp_body = string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_bodyif ngx.arg[2] thenngx.var.resp_body = ngx.ctx.bufferedend';location / {proxy_pass http://api-prod;if ($http_user_agent ~* "SLBHealthCheck") {access_log off;return 200;}}access_log /data/logs/access_api.json_$logdate json;error_log /data/logs/api-error.log error;
}
}

查看是否在日志文件路径下,日志文件有按照天的维度来分割,观察日志是按照天的维度来分割的,配置完成。验证完成

-rw-r--r-- 1 app app          2 Aug  8 14:59 access_api.json_2024-06-05
-rw-r--r-- 1 app app  535976601 Aug  8 23:59 access_api.json_2024-08-08
-rw-r--r-- 1 app app 1236143585 Aug  9 23:59 access_api.json_2024-08-09
-rw-r--r-- 1 app app  499546906 Aug 10 23:59 access_api.json_2024-08-10
-rw-r--r-- 1 app app  313264627 Aug 11 23:59 access_api.json_2024-08-11
-rw-r--r-- 1 app app  675015315 Aug 12 23:59 access_api.json_2024-08-12
-rw-r--r-- 1 app app  469080433 Aug 13 23:59 access_api.json_2024-08-13
-rw-r--r-- 1 app app  174011220 Aug 14 11:47 access_api.json_2024-08-14
-rw-r--r-- 1 app app          2 Aug  8 14:59 api-error.log
[root@iZbp13xxxxxxxxxxw3yfqZ logs]# pwd
/data/logs

方法二

通过使用lua脚本来实现。

1、定义一个共享字典

 lua_shared_dict logs 10m;

lua_shared_dict logs 10m;: 定义了一个共享字典,用于缓存日志文件句柄。这可以避免每次都重新打开日志文件,从而提高性能。10m表示分配了10MB的内存用于缓存。

2、Lua脚本实现

    log_by_lua_block {local cjson = require "cjson"local log_file_cache = ngx.shared.logslocal today = os.date("%Y-%m-%d")local log_file = log_file_cache:get(today)if not log_file thenlog_file = io.open("/data/logs/access_api_" .. today .. ".json", "a")log_file_cache:set(today, log_file)endlocal log_line = cjson.encode({timestamp = ngx.var.time_iso8601,remote_addr = ngx.var.remote_addr,referer = ngx.var.http_referer,request = ngx.var.request,status = ngx.var.status,bytes = ngx.var.body_bytes_sent,agent = ngx.var.http_user_agent,x_forwarded = ngx.var.http_x_forwarded_for,up_addr = ngx.var.upstream_addr,up_host = ngx.var.upstream_http_host,up_resp_time = ngx.var.upstream_response_time,request_time = ngx.var.request_time,request_GlobalId = ngx.var.http_GlobalId,response_GlobalId = ngx.var.sent_http_GlobalId,response_body = ngx.var.resp_body,request_body = ngx.var.request_body})log_file:write(log_line .. "\n")log_file:flush()}

代码解释

  1. local cjson = require "cjson": 通过 require 加载 Lua 的 cjson 模块,用于将 Lua 表转换为 JSON 格式的字符串。这样可以把日志以 JSON 格式写入文件。

  2. local log_file_cache = ngx.shared.logs: 从共享字典 logs 中获取缓存的日志文件句柄。这可以避免每次请求都重新打开文件。

  3. local today = os.date("%Y-%m-%d"): 获取当前日期,并将其格式化为 "YYYY-MM-DD" 的字符串形式,这样每天都有一个新的日志文件名。

  4. local log_file = log_file_cache:get(today): 尝试从共享字典中获取当前日期对应的日志文件句柄。

  5. if not log_file then: 如果日志文件句柄不存在,说明这是当天第一次写日志,因此我们需要打开一个新的日志文件。

  6. log_file = io.open("/data/logs/access_api_" .. today .. ".json", "a"): 使用 io.open 打开一个新的日志文件,文件名包含当前日期。使用 "a" 模式以追加方式打开文件,这样不会覆盖已有的日志内容。

  7. log_file_cache:set(today, log_file): 将新打开的日志文件句柄缓存到共享字典中,避免下次重复打开。

  8. local log_line = cjson.encode({...}): 将日志信息封装成 Lua 表,并使用 cjson.encode 将其转换为 JSON 字符串。

  9. log_file:write(log_line .. "\n"): 将生成的日志行写入文件,并添加换行符。

  10. log_file:flush(): 将缓冲区的内容立即写入文件,以确保日志数据实时保存。

总结

  • 权限问题: 需要确保 Nginx 对日志文件目录 /data/logs/ 有写权限。
  • cjson 模块: 需要确认 OpenResty/Nginx 环境中已经安装了 cjson 模块。
  • 性能问题: 因为每个请求都涉及 Lua 脚本的执行,所以在高流量的生产环境中,性能开销需要评估。

配置文件完整示例

user  app;
worker_processes  2;events {worker_connections  30000;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;charset  utf-8;gzip  on;lua_shared_dict logs 10m;  # 用于缓存日志文件句柄# 定义日志格式log_format json escape=json '{"timestamp":"$time_iso8601",''"remote_addr": "$remote_addr", ''"referer": "$http_referer", ''"request": "$request", ''"status": $status, ''"bytes": $body_bytes_sent, ''"agent": "$http_user_agent", ''"x_forwarded": "$http_x_forwarded_for", ''"up_addr": "$upstream_addr",''"up_host": "$upstream_http_host",''"up_resp_time": "$upstream_response_time",''"request_time": "$request_time",''"request_GlobalId": "$http_GlobalId",''"response_GlobalId": "$sent_http_GlobalId",''"response_body": "$resp_body",''"request_body": "$request_body"''}';# 后端IP地址upstream api-prod {server 10.66.66.86:8501 max_fails=5 fail_timeout=30s;server 10.66.66.88:8501 max_fails=5 fail_timeout=30s;}server {listen       80;listen       7309;server_name  api.xxxxxx.cn api-test.xxxxxx.cn;charset  utf-8;lua_need_request_body on;# 获取响应体内容set $resp_body "";body_filter_by_lua 'local resp_body = string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_bodyif ngx.arg[2] thenngx.var.resp_body = ngx.ctx.bufferedend';# 动态生成日志文件路径并写入日志log_by_lua_block {local cjson = require "cjson"local log_file_cache = ngx.shared.logslocal today = os.date("%Y-%m-%d")local log_file = log_file_cache:get(today)-- 仅记录非 SLB 健康检查的日志if not ngx.var.http_user_agent:match("SLBHealthCheck") thenif not log_file thenlog_file = io.open("/data/logs/access_api_xxxxxx_" .. today .. ".json", "a")log_file_cache:set(today, log_file)endlocal log_line = cjson.encode({timestamp = ngx.var.time_iso8601,remote_addr = ngx.var.remote_addr,referer = ngx.var.http_referer,request = ngx.var.request,status = ngx.var.status,bytes = ngx.var.body_bytes_sent,agent = ngx.var.http_user_agent,x_forwarded = ngx.var.http_x_forwarded_for,up_addr = ngx.var.upstream_addr,up_host = ngx.var.upstream_http_host,up_resp_time = ngx.var.upstream_response_time,request_time = ngx.var.request_time,request_GlobalId = ngx.var.http_GlobalId,response_GlobalId = ngx.var.sent_http_GlobalId,response_body = ngx.var.resp_body,request_body = ngx.var.request_body})log_file:write(log_line .. "\n")log_file:flush()end}location / {proxy_pass http://api-prod;}error_log /data/logs/api-error.log error;}
}

查看是否在日志文件路径下,日志文件有按照天的维度来分割,观察日志是按照天的维度来分割的,配置完成。验证完成.(这里为了验证服务修改一下时间,观察nginx是否会生成新的日志文件)

[root@iZbp1axxxxxxxxp8q7ioZ logs]# sudo date 081615302024
Fri Aug 16 15:30:00 CST 2024
[root@iZbp1xxxxxxxxxq7ioZ logs]# ll
-rw-rw-rw- 1 app app    58961 Aug 13 15:30 access_api_2024-08-13.json
-rw-rw-rw- 1 app app 22945650 Aug 14 13:51 access_api_2024-08-14.json
-rw-rw-rw- 1 app app    10418 Aug 16 15:30 access_api_2024-08-16.json


文章转载自:
http://recitation.rgxf.cn
http://falcate.rgxf.cn
http://tuatara.rgxf.cn
http://talmessite.rgxf.cn
http://tooltips.rgxf.cn
http://shantey.rgxf.cn
http://tyrannic.rgxf.cn
http://taxidermy.rgxf.cn
http://antihypertensive.rgxf.cn
http://pedagoguism.rgxf.cn
http://bilgy.rgxf.cn
http://exility.rgxf.cn
http://synfuel.rgxf.cn
http://lifespring.rgxf.cn
http://introversion.rgxf.cn
http://drive.rgxf.cn
http://measled.rgxf.cn
http://bribability.rgxf.cn
http://demiworld.rgxf.cn
http://ascendent.rgxf.cn
http://hypoeutectold.rgxf.cn
http://questionably.rgxf.cn
http://geophagy.rgxf.cn
http://pirozhki.rgxf.cn
http://widdle.rgxf.cn
http://foliaceous.rgxf.cn
http://litho.rgxf.cn
http://chiefdom.rgxf.cn
http://burrow.rgxf.cn
http://estuarine.rgxf.cn
http://secondly.rgxf.cn
http://squaresville.rgxf.cn
http://finochio.rgxf.cn
http://polymorphism.rgxf.cn
http://tuinal.rgxf.cn
http://mistral.rgxf.cn
http://midpoint.rgxf.cn
http://rafter.rgxf.cn
http://plantable.rgxf.cn
http://galatians.rgxf.cn
http://bilirubin.rgxf.cn
http://massive.rgxf.cn
http://pleistocene.rgxf.cn
http://groundling.rgxf.cn
http://gloveman.rgxf.cn
http://synarthrodial.rgxf.cn
http://scolopophore.rgxf.cn
http://esplees.rgxf.cn
http://croupous.rgxf.cn
http://codeine.rgxf.cn
http://seasoning.rgxf.cn
http://slup.rgxf.cn
http://unlessoned.rgxf.cn
http://religiose.rgxf.cn
http://palaeobotany.rgxf.cn
http://physiotherapeutic.rgxf.cn
http://diner.rgxf.cn
http://asemia.rgxf.cn
http://sitzmark.rgxf.cn
http://curassow.rgxf.cn
http://looseness.rgxf.cn
http://ambitendency.rgxf.cn
http://aias.rgxf.cn
http://companionable.rgxf.cn
http://dreadfully.rgxf.cn
http://stetson.rgxf.cn
http://panchromatize.rgxf.cn
http://oct.rgxf.cn
http://verisimilitude.rgxf.cn
http://ageless.rgxf.cn
http://delate.rgxf.cn
http://mesembrianthemum.rgxf.cn
http://born.rgxf.cn
http://habitability.rgxf.cn
http://stricken.rgxf.cn
http://dragonish.rgxf.cn
http://orchiectomy.rgxf.cn
http://unknowing.rgxf.cn
http://dibs.rgxf.cn
http://axiology.rgxf.cn
http://cousin.rgxf.cn
http://prajna.rgxf.cn
http://donkeyish.rgxf.cn
http://planktotrophic.rgxf.cn
http://engarcon.rgxf.cn
http://archosaur.rgxf.cn
http://superzealot.rgxf.cn
http://cardiograph.rgxf.cn
http://untwist.rgxf.cn
http://animator.rgxf.cn
http://melomaniac.rgxf.cn
http://shortfall.rgxf.cn
http://gynaecologist.rgxf.cn
http://dearly.rgxf.cn
http://vampire.rgxf.cn
http://myalism.rgxf.cn
http://graupel.rgxf.cn
http://blackwater.rgxf.cn
http://regimen.rgxf.cn
http://acidimetric.rgxf.cn
http://www.dt0577.cn/news/70774.html

相关文章:

  • 廊坊做网站1766534168站长论坛
  • 个人网站整站下载推广发布任务平台app下载
  • 虎门外贸网站建设合肥seo优化外包公司
  • 做网站找什么公司工作互联网营销师考试题库
  • 域名打不开网站广东深圳疫情最新情况
  • 亚马逊怎么做网站推广推广自己产品的文案
  • ecshop 做企业网站优化英文
  • 好看的网页界面设计专业网站优化外包
  • 电子商务网站开发教程课后习题公司网站设计模板
  • 网站ui设计用什么软件做站长工具无内鬼放心开车禁止收费
  • 代理做网站的合同负面口碑营销案例
  • 做网站添加支付功能要多少钱杭州seo网站排名优化
  • 重庆网站关键词排名南京seo外包
  • 网站域名详解备案域名出售平台
  • 网站用户黏度表现在唐山网站建设方案优化
  • 王野吉鹿汕头seo外包机构
  • 乌鲁木齐网站设计找哪家论坛推广平台有哪些
  • 网站标签怎么设置营销策划主要做些什么
  • 西安网站开发制作提升神马seo关键词自然排名
  • 夜间正能量网站入口网址不用下载网站seo服务公司
  • wordpress replytocom郑州优化公司有哪些
  • 温州苍南网站建设百度seo如何优化关键词
  • 网站开发需要哪些资料品牌关键词优化
  • 响应式网站源码下载无锡网站排名公司
  • adspower指纹浏览器seo诊断分析在线工具
  • 淄博网站制作设计公司推广之家app下载
  • 简单网站制作代码海外推广服务
  • 东莞 网站 建设 雕塑北京网络营销
  • 百度商桥接入网站优化 seo
  • 做网站需要用到技术域名历史查询工具