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

四层Nginx代理日志配置

NGINX 4层代理(stream模块)的日志配置需要在stream块中进行设置。以下是详细的配置方法:

基本日志配置

stream {# 定义日志格式log_format proxy '$remote_addr [$time_local] ''$protocol $status $bytes_sent $bytes_received ''$session_time "$upstream_addr" ''"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';# 设置访问日志access_log /var/log/nginx/stream.log proxy;# 设置错误日志error_log /var/log/nginx/stream_error.log;# 代理配置示例upstream backend {server 192.168.1.10:3306;server 192.168.1.11:3306;}server {listen 3306;proxy_pass backend;proxy_timeout 1s;proxy_responses 1;proxy_bind $remote_addr transparent;}
}

常用日志变量

在stream模块中可以使用的日志变量包括:

  • $remote_addr - 客户端IP地址
  • $time_local - 本地时间
  • $protocol - 协议类型(TCP/UDP)
  • $status - 连接状态
  • $bytes_sent - 发送字节数
  • $bytes_received - 接收字节数
  • $session_time - 会话持续时间
  • $upstream_addr - 上游服务器地址
  • $upstream_bytes_sent - 向上游发送的字节数
  • $upstream_bytes_received - 从上游接收的字节数
  • $upstream_connect_time - 连接上游服务器的时间

高级配置示例

stream {# 详细的日志格式log_format detailed '$remote_addr - [$time_local] $protocol $status ''$bytes_sent $bytes_received $session_time ''upstream: $upstream_addr ''up_sent: $upstream_bytes_sent ''up_received: $upstream_bytes_received ''up_connect_time: $upstream_connect_time';# 不同服务使用不同日志文件map $server_port $log_file {3306 /var/log/nginx/mysql_proxy.log;5432 /var/log/nginx/postgres_proxy.log;6379 /var/log/nginx/redis_proxy.log;default /var/log/nginx/stream_default.log;}# MySQL代理server {listen 3306;proxy_pass mysql_backend;access_log /var/log/nginx/mysql_proxy.log detailed;error_log /var/log/nginx/mysql_error.log;}# Redis代理server {listen 6379;proxy_pass redis_backend;access_log /var/log/nginx/redis_proxy.log detailed;error_log /var/log/nginx/redis_error.log;}
}

注意事项

  1. stream模块启用:确保NGINX编译时包含了--with-stream模块
  2. 日志目录权限:确保NGINX进程有写入日志目录的权限
  3. 日志轮转:配置logrotate来管理日志文件大小
  4. 性能考虑:频繁的日志写入可能影响性能,可以考虑使用buffer参数

检查配置

验证配置是否正确:

nginx -t

重新加载配置:

nginx -s reload

这样配置后,NGINX就能记录4层代理的详细访问和错误日志了。

http://www.dtcms.com/a/349790.html

相关文章:

  • 2024年Engineering SCI2区,面向工程管理的无人机巡检路径与调度,深度解析+性能实测
  • Vue 2 中的 v-model和Vue3中的v-model
  • 设计模式:原型模式(Prototype Pattern)
  • 使用 HandlerMethodReturnValueHandler 在SpringBoot项目 实现 RESTful API 返回值自动封装,简化开发
  • 数据结构青铜到王者第三话---ArrayList与顺序表(2)
  • 零知开源——基于STM32F103RBT6和ADXL335实现SG90舵机姿态控制系统
  • three.js+WebGL踩坑经验合集(9.1):polygonOffsetUnits工作原理大揭秘
  • 【数据结构】LeetCode160.相交链表 138.随即链表复制 牛客——链表回文问题
  • [SC]SystemC动态进程概述及案例
  • LinkedIn 自动消息发送工具
  • 网络编程——TCP、UDP
  • 人工智能(AI)与网络安全
  • 【Linux】协议的本质
  • 一键脚本:自动安装 Nginx + Certbot + HTTPS(Let‘s Encrypt)
  • QT-QSS样式表
  • 面试:计算机网络
  • 《输赢》电视剧总结学习
  • 数据结构:红黑树(Red-Black Tree)
  • 电商秒杀场景下,深挖JVM内存泄漏与多线程死锁的解决方案
  • Python3.14安装包下载与保姆级图文安装教程!!
  • PyTorch实战(1)——深度学习概述
  • 【动态规划】309. 买卖股票的最佳时机含冷冻期及动态规划模板
  • webpack文件指纹:hash、chunkhash与contenthash详解
  • 基于 OpenCV 与 Mediapipe 的二头肌弯举追踪器构建指南:从环境搭建到实时计数的完整实现
  • 【CV】图像基本操作——①图像的IO操作
  • 系统架构设计师-计算机系统存储管理-页式、段氏、段页式模拟题
  • [系统架构设计师]专业英语(二十二)
  • Python爬虫第四课:selenium自动化
  • Qwt7.0-打造更美观高效的Qt开源绘图控件库
  • macbook国内源安装rust