nginx日志的一点理解

1、access.log什么时候打印?

经过测试,观察到access.log是在后端返回响应结果之后才打印的,不过也很好理解,nginx要等后端返回才知道是否处理成功。

2、nginx什么时候出现upstream timed out?

nginx超时时间<接口响应时间,就会出现upstream timed out.

3、nginx什么时候出现no live upstreams?

测试过程日下

后端接口:

java 复制代码
@RestController
public class BasicController {

    private static final Logger logger = LoggerFactory.getLogger(BasicController.class);
    @GetMapping("/xxx/test")
    public String hello() throws InterruptedException {
        logger.info("xxxtest");
        Thread.sleep(50000);
        return "success";
    }
}

开启了两个端口8080和8081,启动项目

nginx配置如下:

bash 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/json;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        # 指定前端项目所在的位置
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        location / {  
            # 设置超时时间  
            proxy_read_timeout 20s;  
            proxy_connect_timeout 20s;  
            proxy_send_timeout 20s;
            proxy_next_upstream error timeout;
            # 其他代理设置(可选)
            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_set_header X-Forwarded-Proto $scheme;

            proxy_pass http://backend;
        }
    }

    upstream backend {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }
}

日志如下:

access.log

127.0.0.1 - - 07/Jun/2025:22:53:32 +0800 "GET /xxx/test HTTP/1.1" 502 497 "-" "PostmanRuntime/7.44.0"

127.0.0.1 - - 07/Jun/2025:22:53:50 +0800 "GET /xxx/test HTTP/1.1" 504 497 "-" "PostmanRuntime/7.44.0"

error.log

2025/06/07 22:53:30 error 44212#13196: *25 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /xxx/test HTTP/1.1", upstream: "http://127.0.0.1:8081/xxx/test", host: "localhost"

2025/06/07 22:53:32 error 44212#13196: *27 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /xxx/test HTTP/1.1", upstream: "http://127.0.0.1:8080/xxx/test", host: "localhost"

2025/06/07 22:53:32 error 44212#13196: *27 no live upstreams while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /xxx/test HTTP/1.1", upstream: "http://backend/xxx/test", host: "localhost"

2025/06/07 22:53:50 error 44212#13196: *25 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /xxx/test HTTP/1.1", upstream: "http://127.0.0.1:8080/xxx/test", host: "localhost"

相关推荐
天天喝旺仔23 分钟前
Docker 镜像瘦身实战:多阶段构建把体积缩小 90%
运维·后端·ci/cd·docker·云原生·容器·性能优化
NJCloud38 分钟前
Docker 私有仓库部署:Registry、加密传输与认证鉴权
运维·网络·docker·云原生·容器
玖石书1 小时前
Ubuntu 更换 USTC 镜像源完整指南(AMD64 / AARCH64)
linux·运维·ubuntu
zbyyd1 小时前
Linux 进程间通信(IPC):信号、消息队列、共享内存与信号量详解
linux·运维·c语言
智脑API1 小时前
CCSwitch Claude Code 如何配置 MCP?服务器启动、工具权限与安全测试
运维·服务器·claude·codex·ccswitch
闲云野鹤在人间2 小时前
KVM虚拟化实战|CentOS‑Stream8 两种安装方式+模板制作+基础使用
linux·运维·服务器·centos
小张同学a.2 小时前
Docker 容器实战 2—— docker 仓库
linux·运维·服务器·docker·容器
Android系统攻城狮2 小时前
Linux PipeWire深度解析之pw_core_get_registry调用流程与实战(九十一)
linux·运维·服务器·音频进阶·pipewire音频实战进阶
其实防守也摸鱼2 小时前
智能体推荐:精选 AI Agent 工具与实战指南
运维·开发语言·人工智能·学习·web安全·自动化
邪修king2 小时前
Re:Linux 系统编程篇 (十二):进程篇(一)基础与 fork 系统调用全解
linux·运维·服务器