NGINX 启动失败

大家好!

我正在尝试将 Discourse 安装到一个子域。
我的主域运行的是 WordPress,它是使用 docker-compose 构建的。
子域和主域使用同一个 VPS 服务器。
安装过程中,我遵循了以下指南:https://meta.discourse.org/t/run-other-websites-on-the-same-machine-as-discourse/17247

不幸的是,NGINX 无法启动。当我运行 systemctl status nginx 命令时,出现以下消息:

× nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Fri 2024-03-08 07:06:36 CST; 1h 14min ago
       Docs: man:nginx(8)
    Process: 27329 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 27330 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
        CPU: 42ms

Mar 08 07:06:33 vmi1695712.contaboserver.net systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 08 07:06:33 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
Mar 08 07:06:34 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
Mar 08 07:06:34 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
Mar 08 07:06:35 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
Mar 08 07:06:35 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
Mar 08 07:06:36 vmi1695712.contaboserver.net nginx[27330]: nginx: [emerg] still could not bind()
Mar 08 07:06:36 vmi1695712.contaboserver.net systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Mar 08 07:06:36 vmi1695712.contaboserver.net systemd[1]: nginx.service: Failed with result 'exit-code'.
Mar 08 07:06:36 vmi1695712.contaboserver.net systemd[1]: Failed to start A high performance web server and a reverse proxy server.

我的 Discourse 站点配置文件如下:

# Default server configuration
#
server {
        # listen 80 default_server;
        # listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name forum.ictsharks.com;

        location / {
                proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
                proxy_set_header Host $http_host;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Real-IP $remote_addr;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

我检查了好几次,但还是找不到错误所在。

抱歉,我是 Linux / NGINX / Docker 新手。任何建议都将不胜感激。谢谢。

该机器上是否有其他网络服务器占用了端口 80?

您是否已更改 app.yml 中的端口,以便 Discourse 不会尝试使用 NGINX 想要的端口?

这是一个高级设置,需要了解 DNS、https 和证书、nginx、反向代理、docker 和路由。

正如我所提到的,wordpress 运行在主域名上。

如果我理解正确的话,另一个 NGIX 与 wordpress 一起在容器中运行。

我遵循了本教程:How To Install WordPress With Docker Compose | DigitalOcean

如果我运行 netstat -tulnp 命令,我会得到以下结果:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      419/systemd-resolve
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1192/docker-proxy
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      463/sshd: /usr/sbin
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1179/docker-proxy
tcp6       0      0 :::22                   :::*                    LISTEN      463/sshd: /usr/sbin
udp        0      0 127.0.0.53:53           0.0.0.0:*                           419/systemd-resolve

说明建议注释掉它们,所以我这样做了:

## 这个容器应该暴露哪些 TCP/IP 端口?
## 如果您想让 Discourse 与 Apache 或 nginx 等其他 Web 服务器共享端口,
## 请参阅 https://meta.discourse.org/t/17247 获取详细信息
expose:
  # - "80:80"   # http
  # - "443:443" # https

我了解 DNS 记录、https 和证书。只有其余部分对我来说是新的。

别担心,我知道如何使用谷歌搜索,而且我学得很快 :wink:

在你开始之前,它是否位于一个正在工作的 NGINX 反向代理后面,你正在向其添加 Discourse?

或者,如果之前运行的是 Apache/WordPress,那么它是否占用了 80 端口?

哦,看起来 docker-proxy 占用了 80 端口,所以也许你的 WordPress 位于 docker-proxy 后面?

也许 docker ps 可以让你看到 docker 是什么

正如我所说,我认为另一个 NGIX(用于 wordpress 的那个)与 MySQL 和 Wordpress 一起运行在一个容器中。

这是我通过运行 docker ps 命令得到的结果:

CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS          PORTS                                      NAMES
6ca4cbec2c88   local_discourse/app          “/sbin/boot”             51 minutes ago   Up 15 minutes                                              app
4a3ebdd9f5b5   nginx:1.15.12-alpine         “nginx -g 'daemon of…”   20 hours ago     Up 15 minutes   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   webserver
42c4211a2fb6   wordpress:5.1.1-fpm-alpine   “docker-entrypoint.s…”   2 days ago       Up 15 minutes   9000/tcp                                   wordpress
5b588a7435ad   mysql:8.0                    “docker-entrypoint.s…”   2 days ago       Up 15 minutes   3306/tcp, 33060/tcp                        db

这就是为什么外部 nginx 无法启动的原因,因为端口已被内部的占用。你可能需要关闭该容器,并将 WordPress 的配置转移到外部 nginx 层。

另外,这些是独立的容器,nginx 与 MySQL 和 WordPress 是分开的。

我关闭了 Web 服务器(NGINX 容器)并尝试启动外部 NGINX。仍然无法正常工作。

你说得对。是我的错。我以为提到的所有应用程序都在一个容器中。

一个容器 = 一个应用程序/程序 :wink:

现在的 docker psnetstat -tulnp 是什么样子?

容器ID   镜像                        命令                  创建于         状态         端口                 名称
6ca4cbec2c88   local_discourse/app      “/sbin/boot”       3小时前      已启动2小时                         app
42c4211a2fb6   wordpress:5.1.1-fpm-alpine   “docker-entrypoint.s…”   2天前       已启动2小时   9000/tcp              wordpress
5b588a7435ad   mysql:8.0                “docker-entrypoint.s…”   2天前       已启动2小时   3306/tcp, 33060/tcp   db

活动Internet连接(仅服务器)
协议 接收队列 发送队列 本地地址           外部地址         状态       PID/程序名称
tcp        0      0 127.0.0.53:53           0.0.0.0:*               监听      418/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               监听      462/sshd: /usr/sbin
tcp6       0      0 :::22                   :::*                    监听      462/sshd: /usr/sbin
udp        0      0 127.0.0.53:53           0.0.0.0:*                           418/systemd-resolve

我在这里能做的也 Měi yǒu duō shénme le, nín kàn nánjīng (nginx) hái zài zuò zhe xiāngtóng de shìqíng ma, háishì shuō cuòwù yǒu shénme biànhuà ma?

这应该结合添加 web.socketed.template.yml 模板来完成,你做了吗?

是的,我试过了。

我尝试卸载并重新安装 NGIX,并删除了配置文件。但是,问题仍然存在。

我将尝试查找列出的 NGIX 错误并查看我能找到什么。

我解决了 NGIX 问题!:grinning:

在 /etc/nginx/sites-enabled/ 文件夹中有一个名为“ysystemctl status nginx”的无效文件。我已将其删除。

现在 NGIX 可以正常工作了 :muscle:

我将遵循 riking 的指示,并在完成后告知您结果。

好的,我的 discourse 论坛已成功运行!

但不幸的是,这些说明没有提到如何修改运行网站/WordPress 的 NGINX 的设置。

它只说了这个:创建其他的 NGINX “站点”,然后像上面最后一步那样链接并启用它们。

如果您能提供一些关于如何实现这一点的技巧,我将不胜感激。

我是按照本指南安装的 WordPress:如何使用 Docker Compose 安装 WordPress

除此之外,您能否确认或否定我的结论?

  1. NGINX(容器外的那个)用作反向代理。
  2. NGINX(容器外的那个)被称为“外部 NGIX”。
  3. NGINX(容器外的那个)在当前设置中使用 unix 套接字而不是 TCP 端口进行通信。
  4. Discourse 是一个独立的应用程序,因此不需要安装自己的 NGIX Web 服务器。

如果我试图验证显而易见的事情,我深表歉意。我只是想百分之百清楚。

差不多就是这样。

discourse 容器包括 nginx、rails、postgres 和 redis。