2011-05-21 131 views
13

我在Ubunto 10:04上使用nginx和Django。问题是,当我重新启动nginx我得到这个错误。无法重新启动nginx

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) 

此外,我试过停止然后开始,但仍然得到错误。

下面是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) 

如果我杀了PID为27141它的工作过程。不过,我想深究底线 为什么我不能只重启一次。

这里的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; 
     } 
    } 
} 

任何想法?

+0

这是重复的?你的日志文件说什么? – Mat 2011-05-21 16:50:59

+1

错误日志显示相同的事情。是的,这是可重复的。 – ErnieP 2011-05-21 16:54:09

+0

可能启动脚本已损坏。查看/etc/init.d/ngnix并查看它如何停止ngnix。它可能不像你已经指定了pid文件的位置(或者甚至用户/组运行)。初始化脚本通常需要自行完成,常常希望pid文件驻留在/ var/run /中的某处。 – nos 2011-05-21 17:04:15

回答

31

尝试:

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

定影器手册页面描述:“定影器显示使用指定文件或文件系统的进程的PID”(阅读:套接字) – kelorek 2013-03-31 00:17:17

+1

这适用,但是由于它没有解决根本原因而下调。 – 2013-06-02 06:21:56

2

的Daemontools开始nginx的成功,那么nginx的daemonizes,然后daemontools的尝试nginx的再次启动,失败,记录错误日志中。

解决这个问题是在nginx.conf的主要部分以禁用守护程序模式:

守护进程关闭;

网站:http://wiki.nginx.org/CoreModule

+0

非常感谢!我厌倦了绑定错误。 – user1071840 2013-04-08 03:15:12

1

厌倦了nginx的重启问题, “地址在使用” 故障。决定让它工作一劳永逸。

仅增加了一个在年底停止并重新启动行动/etc/init.d/nginx文件行

nginx -s quit 

所以现在看起来(并确保nginx的文件夹在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." 
    ;; 

希望这个解决方案能够为他人工作。

0

始终测试首先配置,它会显示语法错误并重复线条并指向那里。

nginx -t 

您将在那里看到日志,向您显示造成故障的原因。

0

这是因为您没有以root身份重新启动。

更改为root:

sudo -i 

重启:

service nginx restart 

或者:

/etc/init.d/nginx restart