nginx 1.12.2 未绑定到 Centos 7.4 上的端口

nginx 1.12.2 未绑定到 Centos 7.4 上的端口

我在 Centos 7.4 上通过 yum 安装了 nginx。我添加了一些/etc/nginx/conf.d监听 80 端口的站点。下面是示例:

server {
    listen   80;

    root /var/www/vhosts/somesite;
    index index.php index.html index.htm;

    server_name api.somesite.info;

    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/impro.somesite.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

nginx -t 返回:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

服务启动如下:

systemctl start nginx

这不会返回任何错误。并且目前没有安装防火墙。

问题是,即使我指向/etc/hostsapi.somesite.info,也会被拒绝连接。

为了查看绑定的端口,我运行了netstat -ltnep,结果如下:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       
User       Inode      PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      0          14585      1/systemd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          18224      1105/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          16689      979/master
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      0          68092      4930/php-fpm: maste
tcp6       0      0 :::3306                 :::*                    LISTEN      27         51441      26589/mysqld
tcp6       0      0 :::111                  :::*                    LISTEN      0          14584      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      0          18226      1105/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      0          16690      979/master

因此,虽然 php-fpm 已绑定到端口(我实际上主要使用套接字,但我认为这与此无关),但 nginx 尚未绑定。请注意,没有任何东西监听端口 80 - 此版本的 centos 中默认未安装 apache。

我可以通过运行以下命令看到 nginx 正在运行systemctl status nginx

nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2018-04-04 15:24:04 BST; 4min 8s ago
Process: 6091 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 6088 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 6086 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 6094 (nginx)
 CGroup: /system.slice/nginx.service
       ├─6094 nginx: master process /usr/sbin/nginx
       └─6095 nginx: worker process

Apr 04 15:24:04 smaractus systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 04 15:24:04 smaractus nginx[6088]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 04 15:24:04 smaractus nginx[6088]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 15:24:04 smaractus systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Apr 04 15:24:04 smaractus systemd[1]: Started The nginx HTTP and reverse proxy server.

我在这里看到一个警告,它无法找到 pid 文件,但据我所知,这不是什么大问题?现在不知道该去哪里找。为什么 nginx 没有绑定端口 80?

答案1

您是否碰巧在 看到过这个nginx.conf

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

这意味着 中的文件名称/etc/nginx/conf.d必须以 结尾.conf。请检查文件的名称并将其重命名,使其以 结尾.conf。其他任何名称都将被忽略。

相关内容