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"

相关推荐
U-Mail邮件系统3 小时前
自建邮箱服务器有哪些软件?三款主流邮件服务器软件推荐
运维·服务器
天远数科5 小时前
零信任架构实战:基于天远贷前风险报告构建自动化消金信贷风控与多头共债排查网关
运维·人工智能·架构·自动化
别动我齐刘海6 小时前
Linux 系统调试学习2
linux·运维·c++·人工智能·学习·机器学习·机器人
honsor6 小时前
机房IP化改造首选:以太网温湿度传感器,PoE供电+Modbus TCP一网搞定
linux·运维·服务器
星创易联6 小时前
PLC数据采集与远程运维平台:从边缘计算到集中监控
大数据·运维·边缘计算
郭老二7 小时前
【docker】docker compose使用,已nginx为例
nginx·docker·容器
寻道模式8 小时前
【运维心得】完整教程:绕过Win11限制让老机焕发新生
运维·操作系统·win11
zzu123zsw8 小时前
从 proxy_pass 末尾斜杠到 WebAssembly 预压缩:深挖 Nginx 生产环境四大高频踩坑点
运维·nginx·wasm·webassembly·devops·运维排错
张洛闻Eren8 小时前
Ansible【第一课】:Ansible介绍
运维·数据库·云原生·kubernetes·github·ansible
郝亚军9 小时前
RK3562 中有 mosquitto、nginx 了,还需要 http,ftp,tftp 等协议吗?
运维·nginx·http