Nginx在CentOS中不起作用?

时间:2018-04-09 12:12:32

标签: nginx centos7

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost bin]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@localhost bin]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: **failed** (Result: exit-code) since Mon 2018-04-09 16:35:28 IST; 6min ago
  Process: 22654 ExecStart=/usr/sbin/nginx (**code=exited, status=1/FAILURE**)
  Process: 22597 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 22595 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

Apr 09 16:35:27 localhost.localdomain nginx[22654]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Apr 09 16:35:27 localhost.localdomain nginx[22654]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Apr 09 16:35:28 localhost.localdomain nginx[22654]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 09 16:35:28 localhost.localdomain nginx[22654]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Apr 09 16:35:28 localhost.localdomain nginx[22654]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Apr 09 16:35:28 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
Apr 09 16:35:28 localhost.localdomain nginx[22654]: nginx: [emerg] still could not bind()
Apr 09 16:35:28 localhost.localdomain systemd[1]: **Failed to start The nginx HTTP and reverse proxy server.**
Apr 09 16:35:28 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
Apr 09 16:35:28 localhost.localdomain systemd[1]: nginx.service failed.
  

错误:活动:失败(结果:退出代码)自I 2018-04-09 16:35:28 IST;           进程:22654 ExecStart = / usr / sbin / nginx(code = exited,status = 1 / FAILURE)           Apr 09 16:35:28 localhost.localdomain systemd [1]:无法启动nginx HTTP和反向代理服务器。

1 个答案:

答案 0 :(得分:1)

该错误基本上意味着某些其他应用程序正在使用那些默认端口。您可以使用:

sudo netstat -tulpn

获取已经使用该端口的进程的PID,并使用kill命令发送信号。

sudo kill -2 <PID>
sudo service nginx restart

在我的情况下,Apache httpd在同一端口上运行,我不想终止该进程,因此我将nginx的默认端口更改为其他端口。这就是我的nginx.conf现在的样子。

server {
    listen       3000 default_server;
    listen       [::]:3000 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

还要确保在此文件中,user设置为您登录的用户(例如root的登录用户)。

如果不是这种情况,您可以尝试提及here.

的其他选项

您可以使用nginx -t来测试文件nginx.conf的语法是否正确。