Can't restart nginx

2019-01-30 20:57发布

问题:

I'm using nginx with Django on Ubunto 10:04. The problem is when I restart nginx I get this error.

sudo /etc/init.d/nginx restart
Restarting nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

Also, I have tried stop and then start but still get the error.

Here's the output from lsof:

sudo lsof -i tcp:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   27141   root    6u  IPv4 245906      0t0  TCP *:www (LISTEN)
nginx   27142 nobody    6u  IPv4 245906      0t0  TCP *:www (LISTEN)

If I kill the process with PID 27141 it works. However, I would like to get to the bottom of why I can't just do a restart.

Here's the nginx.conf:

worker_processes 1;

user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
    worker_connections 1024;
    accept_mutex off;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /tmp/nginx.access.log combined;
    sendfile on;

    upstream app_server {
        # server unix:/tmp/gunicorn.sock fail_timeout=0;
        # For a TCP configuration:
        server 127.0.0.1:8000 fail_timeout=0;
    }

    server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # path for static files
        root /home/apps/venvs/app1/app1;

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app_server;
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /path/to/app/current/public;
        }
    }
}

Any ideas?

回答1:

Try:

$ sudo fuser -k 80/tcp ; sudo /etc/init.d/nginx restart 


回答2:

This worked for me

sudo fuser -k 80/tcp

And then

service nginx start

Source: https://rtcamp.com/tutorials/nginx/troubleshooting/emerg-bind-failed-98-address-already-in-use/



回答3:

Daemontools starting nginx successfully, then nginx daemonizes, and then daemontools tries to start nginx again, unsuccessfully, logging an error to the log.

The solution to this problem is to disable daemon mode in the main section of the nginx.conf:

daemon off;

Site: http://wiki.nginx.org/CoreModule



回答4:

Tired with nginx restart issues and "address in use" faults. Decided to make it work once and for all.

Added just one line at the end stop and restart action in /etc/init.d/nginx file

nginx -s quit

so it looks now like (and ensure that nginx folder is in PATH variable, otherwise specify the full path)

stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    nginx -s quit
    ;;

restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON || true
    nginx -s quit
    sleep 1
    test_nginx_config
    start-stop-daemon --start --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;

Hope that this solution will work for others.



回答5:

Always test your config first, it will show syntax errors and duplicate lines and point you there.

nginx -t

You will see logs there showing you what is causing the failure.



回答6:

It's because you aren't restarting as root.

Change to root:

sudo -i

Restart:

service nginx restart

Or:

/etc/init.d/nginx restart


标签: django nginx