【问题标题】:Nginx(proxy) + Apache: two process listening same portNginx(proxy) + Apache:两个进程监听同一个端口
【发布时间】:2023-03-09 20:55:02
【问题描述】:

我在 CentOS 上有两台服务器:Nginx(proxy) + Apache。 我需要重启 Nginx,但是如果我在重启前尝试测试配置,我会遇到下一个错误:

[root@vm5808 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use)
nginx: configuration file /etc/nginx/nginx.conf test failed

我该如何解决这个问题?谢谢!

附: Nginx 有监听 80 端口,apache 监听 81。

【问题讨论】:

    标签: linux apache nginx centos


    【解决方案1】:

    不知道为什么会出现这个错误,但是我做了之后

    [root@vm5808 ~]# /etc/init.d/nginx stop  
    Stopping nginx:                                            [  OK  ]
    [root@vm5808 ~]# /etc/init.d/nginx start
    Starting nginx:                                            [  OK  ]
    

    错误消失了:

    [root@vm5808 ~]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    我在stopstart之前尝试过restart,但是Nginx不会重启:

    [root@vm5808 ~]# /etc/init.d/nginx restart
    nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use)
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    【讨论】:

    • 也许 nginx 没有检测到两个实例。无论如何,下次尝试使用 netstat --program --listening --numeric (或简称 -npl)来发现该端口中进程的 pid
    • @PerroVerd 我试过netstat -tanup | grep 80,我有tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8400/nginx
    【解决方案2】:

    你的问题很简单,好像nginx和apache都在监听80端口,所以nginx发现这个端口已经在使用,你需要把apache移到另一个端口,比如81,让nginx监听80 ,因为它将接收 http 请求并代理它。

    【讨论】:

    • 感谢您的回答。但是看看 P.S.在我的问题中;)
    • 请确保停止 apache 服务并启动 nginx,如果它启动则 apache 持有端口 80 的错误
    【解决方案3】:

    检查 apache 中的所有虚拟主机,可能其中一个正在监听 80 端口

    【讨论】:

    • 感谢提示,但他们监听 81 端口。我已经检查过了。
    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多