分类 Nginx 下的文章
Nginx 502 no live upstreams while connecting to upstream 问题
Nginx转发两个后端服务,请求返回502,没有进入到后端服务,排查Nginx日志发现:
no live upstreams while connecting to upstream造成该问题的原因:
转发到upstream两个节点的请求都出现了超时(upstream timed out (110: Connection timed out) while reading response header from upstream),Nginx认为upstream中的两个节点都挂掉了,无法使用,所以拒绝新请求的连接,返回502。
需要等待fail_timeout时间后,才能继续尝试转发,这是属于Nginx的被动健康检查机制。
Nginx中 $host $http_host $proxy_host的区别
| 变量 | 值来源 | 是否显示端口 | 说明 |
|---|---|---|---|
| $host | 按照 请求行中的Host > 请求头中的Host > server_name 的值 优先级获取 | 否 | "Host:port"显示值为a:b的时候,只显示a |
| $http_host | 请求头中的Host | 是 | "Host:port",port存在就显示,请求头中Host是多少 $http_host就是多少 |
| $proxy_host | proxy_pass 指定的地址 | 是(默认80不显示) | "Host:port" proxy_pass指定的 "Host:port",当port为80的时候只有“Host”,其它端口,正常显示为 “Host:port” |
Nginx 报错 413 client intended to send too large body 处理
promtail通过nginx向loki发送数据,收到以下报错
level=error ts=2025-05-08T01:21:05.955472787Z caller=client.go:380 component=client host=local-loki:3100 msg="final error sending batch" status=413 error="server returned HTTP status 413 Request Entity Too Large (413): <html>"排查nginx日志,发现
2025/05/08 09:21:06 [error] 21#21: *62769236 client intended to send too large body: 1312028 bytes, client: 10.40.30.155, server: , request: "POST /loki/api/v1/push HTTP/1.1", host: "local-loki:3100"
- | 10.40.30.155 | - | 08/May/2025:09:21:06 +0800 | POST /loki/api/v1/push HTTP/1.1 | 413 | 183 | - | promtail/2.6.1 | - | 0.032 | - | - | - | local-loki:3100触发了Nginx的最大请求体限制,收到了 413 (Request Entity Too Large) error
从源代码构建定制Nginx
官方文档: 从源代码构建Nginx
基于源码,定制编译Nginx,提供了关于源码编译Nginx的示例
- 不使用外部模块编译Nginx
- 使用外部模块编译Nginx
编译环境
Nginx版本: 1.27.5
操作系统: Ubuntu 18.04
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
Release: 18.04
Codename: bionic