Question

Service nginx restart fail

Posted September 18, 2014 230.3k views

Hi all!

When i do “ sudo service nginx restart ” , terminal says me: * Restarting nginx nginx [fail]

What can i do?

10 comments
  • Solved:

    This error occurs when editing the nginx.conf something goes wrong and nginx can’t restart. The “nginx -t” It is very useful to resolve these errors when you edit nginx.conf,

    Thanks to all!!

  • Thank you very much. It helped me finding out error in configuration file.

  • Thanks! it was so helpful to me too.

  • Show 7 more comments

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
11 answers

After each modification in your nginx config file do : sudo nginx -t

It will tell you if there are any error in config file.
If everything is ok, do : sudo service nginx reload

reload = nginx will reload your configuration, without restart.
restart = stop and start again nginx. For some seconds, your website will be offline.

You can check your error log: cat /var/log/nginx/error.log (I think error.log is the default name)

Excuse me my bad english.

Your logs should indicate why that command failed. You can also use “nginx -t” to test your config for errors.

The logs are generally in /var/log/nginx

Hi, thanks! But when i try “nginx -t” i think i have an error:

nginx: [emerg] “location” directive is not allowed here in /etc/nginx/nginx.conf:11
nginx: configuration file /etc/nginx/nginx.conf test failed

it does not make any sense when I am trying to sudo service nginx restart and it said nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

its keeping failing.

what I am having the code here…

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root  /var/www/html/test2/public;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name 222.231.22.22;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }


        location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }

the document root default is var/www/html

Is possible conflict with XAMPP or apache2.

I have the same problem! Did you find a fix per chance?

I’m having the same issue. sudo nginx -t says everything is good. And the reload seems to go just fine. But the restart fails and I am not sure where to go from here.

EDIT: I think my issue may be due to something else already listening on port 80 on my server. I’ll start there.

  • Just to make sure my comments didn’t lead anyone astray. My issue was due to an malformed key file. Once I had that re issued all was well.

  • Could you explain the malformed key issue. I’m having difficulty starting my nginx service and nothing I’ve found so far has worked.

  • Hi @dfansteel, are there any errors in nginx’s error log?

    sudo tail /var/log/nginx/error.log
    
  • @kamaln7 Yes:

    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: still could not bind()
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: still could not bind()
    

    I’m also asking this question on stack overflow someone thinks I might have two installs of nginx…
    http://stackoverflow.com/questions/31576178/nginx-on-digital-ocean-restart-fails-and-show-not-running-but-site-is-being-ser

  • @kamaln7 Yes:

    root@git ~# sudo tail /var/log/nginx/error.log
    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:30:28 [emerg] 3840#0: still could not bind()
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/07/22 20:32:58 [emerg] 4198#0: still could not bind()
    

    I’m also asking the question on stack overflow, but the comment gets rejected as spam when I attempt to paste it here. Someone thinks I might have 2 installs of nginx.

I have the same problem!
When i try
“ /usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf”
I get everything is ok
“nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok”
“nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful”
What can i do?

Hey all, I was getting this issue as well. Running sudo tail /var/log/nginx/error.log (thanks @kamaln7!) show me the same errors as @dfansteel is getting. It looks like another task was running on port 80. This might not be the most elegant solution (someone pipe up if this is a bad idea), but you can kill the process by running sudo fuser -k 80/tcp. This allowed me to restart nginx without issue.

Thank you!!!!! This was so helpful!!!

This post was very helpful in identifying the error with nginx. Thanks!

Submit an Answer