记一次nginx有时候404 有时候正常的问题

问题

再使用nginx的时候添加了一个配置,发现访问接口有时候正常,有时候异常排查了,被代理的接口正常的

复制代码
 server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


          location /abc/ {
                proxy_pass http://localhost:8080/abc/;
                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;
            }

            location /bbb/ {
                proxy_pass http://localhost:8999/bbb/;
                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_set_header Forwarded  host=$host:$server_port;
            }

            location /ccc/ {
                proxy_pass http://localhost:8888/ccc/;
                proxy_set_header Host $host:$server_port;
                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_set_header Forwarded  host=$host:$server_port;
        }

        location /v1/ {
                proxy_pass http://localhost:9999/v1/;
                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;
        }
}

每次修改完配置文件

bash 复制代码
nginx -t

nginx -s reload

都还检查配置文件是否正确。然后才重启的

ps aux | grep nginx

查看进程时间发现一个work 的启动时间是上个月

bash 复制代码
root      1234  0.0  0.1  43760  2004 ?        Ss   12月13   0:00 nginx: master process /usr/sbin/nginx
www-data  1235  0.0  0.1  44100  2104 ?        S    11月11   0:00 nginx: worker process
www-data  1236  0.0  0.1  44100  2104 ?        S    12月13   0:00 nginx: worker process

上个月的时候配置文件没有配置v1相关的

原因

终于问题找到原因了: 原来我每次修改配置文件之后,都是用命令 nginx -s reload 重启nginx的,这样重启可能有时候导致一个worker没有正常的启动,而另一个worker正常启动了,所以访问的时候,如果是第一个提供了服务,就是404 了,而那个正常的worker提供服务就正常了。

  • 在执行 nginx -s reload 时,Nginx 会启动新的 worker 进程来加载新的配置,并在新的 worker 进程准备好后平滑地停止旧的 worker 进程。
  • worker_processes 1; 设置的是1 没有平滑的停止掉旧的worker

解决

不要直接使用 nginx -s reload 这样的重启,至于为啥这样还不清楚

最好先关闭, 再查看, 再重启

bash 复制代码
# 关闭nginx
nginx -s stop

# 查看nginx 服务
ps aux | grep nginx

# 启动nginx
nginx 
相关推荐
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特3 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
张洛闻Eren3 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
布裘3 天前
【银河麒麟】V4桌面图标消失,右击鼠标没反应排查
运维·银河麒麟·桌面环境
懂软件的胡子个哥3 天前
微信机器人为什么需要“回复候选”而不是所有 AI 内容直接发送
运维·微信·自动化·wechatapi·个人微信号二次开发