突然我得到下面的nginx错误

 * Restarting nginx
 * Stopping nginx nginx
   ...done.
 * Starting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
   ...done.
   ...done.

如果我跑

lsof -i :80 or sudo fuser -k 80/tcp 

我什么也得不到。80端口上没有东西

然后我运行如下:

sudo netstat -pan | grep ":80"
tcp        0      0 127.0.0.1:8070          0.0.0.0:*               LISTEN      15056/uwsgi     
tcp        0      0 10.170.35.97:39567      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39564      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39584      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39566      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39571      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39580      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39562      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39582      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39586      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39575      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39579      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39560      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39587      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39591      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39589      10.158.58.13:8080       TIME_WAIT   - 

我被难住了。我如何调试这个?

我在端口8070上使用带有代理通道的uwsgi。Uwsgi正在运行。Nginx则不然。我使用的是ubuntu 12.4

下面是我的nginx conf文件的相关部分

upstream uwsgi_frontend {
          server 127.0.0.1:8070;
        }
server {
listen 80;
        server_name 127.0.0.1;
        location = /favicon.ico {
                  log_not_found off;
                }



                location / {
                       include uwsgi_params;
                       uwsgi_buffering off;

                       uwsgi_pass 127.0.0.1:8070;
                 }
        }

下面是我在ubuntu 12.04上安装nginx的方法

nginx=stable;add-apt-repository ppa:nginx/$nginx;
apt-get update
apt get install nginx-full

当前回答

首先修改apache监听端口80为8080 在/etc/apache2/ports.conf中包含Apache

Listen 1.2.3.4:80 to 1.2.3.4:8080
sudo service apache2 restart 

or

sudo service httpd restart    // in case of centos

然后添加nginx作为反向代理服务器,将监听apache端口

server {
 listen   1.2.3.4:80;
 server_name  some.com;

 access_log  /var/log/nginx/something-access.log;

 location / {
  proxy_pass http://localhost:8080;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }


location ~* ^.+\.(jpg|js|jpeg|png)$ {
   root /usr/share/nginx/html/;
}

location /404.html {
  root /usr/share/nginx/html/40x.html;
}

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

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

# put code for static content like js/css/images/fonts
}

修改后重启nginx服务器

sudo service nginx restart

现在所有流量都将由nginx服务器处理,并将所有动态请求发送到apache,静态内容由nginx服务器提供。

对于像缓存这样的高级配置:

https://www.linode.com/docs/web-servers/nginx/slightly-more-advanced-configurations-for-nginx/#basic-nginx-caching

其他回答

多个服务可以在同一个端口上侦听。 这个问题通常来自于在同一台机器上混合使用Apache和NGINX。

检查:

Sudo netstat -plant | grep 80

停止Apache并重启NGINX:

Sudo systemctl stop apache2 && Sudo systemctl restart nginx && Sudo systemctl status nginx

继续关注@lfender6445和@SAURABH回答——

我的问题还在于,在升级到Vagrant 2.2.2之后,当客户机启动时,Apache2作为web服务器运行。在过去,我只使用nginx作为web服务器。

vagrant ssh进入机箱,并运行以下命令使Apache2在guest机箱引导时不启动:

sudo update-rc.d -f apache2 remove

退出ssh,流浪汉停下来,流浪汉起来。问题解决了。

在Ubuntu上对我有效的快速解决方案。首先找到当前运行的nginx实例:

PS 辅助 |格雷普·恩金克斯

复制pid(例如123456)并运行

杀死-9 123456

我通过运行:

sudo apachectl stop

结果apache在后台运行,阻止nginx在指定的端口上启动。

在Ubuntu上运行:

sudo /etc/init.d/apache2 stop

如果在尝试上述任何解决方案后问题仍然存在,请重新启动服务器一次。这对我很有用:)