nginx 1.12.2 not binding to ports on Centos 7.4

Robert Egginton asked:

I have installed nginx via yum on Centos 7.4. I’ve added some sites to /etc/nginx/conf.d that listen on port 80. Here is an example:

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 returns:

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

The service is started as follows:

systemctl start nginx

This returns no errors. And there is no firewall installed at present.

The problem is that even if I point /etc/hosts at api.somesite.info I am getting a refused connection.

To see the ports bound, I ran netstat -ltnep, giving:

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

So while php-fpm has bound to a port (I’m actually mostly using sockets, but that’s not relevant here I don’t think), nginx has not. Note that there is nothing listening to port 80 – apache is not installed by default in this version of centos.

I can see that nginx is running by running 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.

I can see a warning here where it fails to find the pid file, but as far as I understand that’s not a show stopper? Out of ideas as to where to look now. Why is nginx not binding port 80?

My answer:


Did you happen to see this in 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;

This means that the name of the file in /etc/nginx/conf.d must end with .conf. Check the file’s name and rename it so that it ends with .conf. With any other name it will be ignored.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.