Service nginx restart fail

September 18, 2014 115.6k views

Hi all!

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

What can i do?

8 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.

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

    This was my response and yet it still won't start

  • @bgarrison25 I had the same problem. Check nginx's error logs (/var/log/nginx/error.log, or find the location in nginx.conf error_log).

    In my case I had:
    2015/12/29 12:33:37 [emerg] 24489#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2015/12/29 12:33:37 [emerg] 24489#0: still could not bind()

    There was a port conflict in my conf. I was transitioning the server from apache > nginx, setting up nginx to run on a different port while apache still served live sites. By default, sites listen on 80 (which was in use by apache). Adding a "listen [port#];" with my temporary non-80 port to the site's server block fixed it.

  • This was in fact my issue @palpants . I had my apache server still listening on port 80...durrr....figured it out a long time ago but forgot i even had this question here. Thanks!

  • Even after all this time, I found this post and the -t command is very useful in resolving simple nginx startup issues. Thanks

  • very helpful, thanks!

10 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.

  • Thanks, this helped :)

  • Hey, you are mad.........half brain
    I am use virtual host on nginx ubuntu
    I am used this sudo fuser -k 80/tcp on nginx
    I dont know why I used this command... by mistake
    Going all mysite down...because of you
    What I do. Plz reply

  • zafarmd, try being nicer next time. If you used this command by mistake, it's hardly my fault for copying and pasting it here from another forum thread.

    Restart nginx if you haven't already. Beyond that I can't help you.

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

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

Have another answer? Share your knowledge.