记一次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 
相关推荐
企鹅侠客21 小时前
Ubuntu本地部署AnythingLLM实现本地文档RAG
linux·运维·ubuntu·llm
醇氧21 小时前
Git 合并冲突提示 Local Changes Prevent from Merge
运维·git
被AI抢饭碗的人21 小时前
linux:进程间通信
linux·运维·服务器
chao1031 天前
ubuntu下业务运行环境搭建
linux·运维·ubuntu
海阔的天空1 天前
VSCode通过continue插件免费安装AI模型实现自动编程
运维·ide·人工智能·vscode·编辑器·ai编程
weixin_46681 天前
Docker Dockerfile文件
linux·运维·服务器
保持低旋律节奏1 天前
linux——软件包、yum 安装和卸载
linux·运维·服务器
深圳市恒讯科技1 天前
美国服务器备份策略:快照、异地备份与恢复演练指南
运维·服务器·github
LSL666_1 天前
docker概述
运维·docker·容器
翼龙云_cloud1 天前
阿里云渠道商:无影云电脑怎么使用?
运维·服务器·阿里云·云计算·电脑