Tampa
Tampa

Reputation: 78352

nginx - nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

All of a sudden I am getting the below nginx error

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

If I run

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

I get nothing. Nothing on port 80

Then I run the below:

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   - 

I am stumped. How do I debug this?

I am using uwsgi with a proxy pass on port 8070. uwsgi is running. Nginx is not. I am using ubuntu 12.4

Below are the relevant portions of my nginx conf file

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;
                 }
        }

Here is how I install nginx on ubuntu 12.04

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

Upvotes: 413

Views: 629118

Answers (28)

Jesse
Jesse

Reputation: 996

I've reviewed several answers, each addressing different culprits for the same symptoms. Here may be some of the deeper causes:

Conflicting configs in /etc/nginx/

Problem: listening for the same host on the same port

Without conflict, I have multiple nginx configs listening on the same port, but for different hosts via server_name:

  • 0.0.0.0:80
  • [::]:80
  • 0.0.0.0:443
  • [::]:443

The answer about multiple configs within /etc/nginx/ only had a problem to begin with because each config listed for the same host from the nginx server_name setting.

Apache-Nginx

Reverse proxy is okay; same host-port is not.

Many discuss Apache conflicts, like in this answer, but I successfully have an Nginx-Apache reverse proxy server that clips along valiantly.

In reverse-proxy, Apache doesn't listen on the public port for the host; it listens on an internal port albeit for the same host. That's no conflict.

Nginx, Apache, and combination thereof isn't the cause in this issue. The issue is more than one config listening for the same host/domain on the same port, regardless of combined web services.

Letsencrypt, et cetera: use fuser -k 443/tcp

My solution was the fuser -k 443/tcp answer, and everything continued working after reboot.

Remember, Letsencrypt heavily engages with whatever webserver it is told to. For me, it was Nginx, where my port errors were.

My likely culprit was attempting to get Letsencrypt certs while DNS wasn't quite ready, and it caused a log jam of port activity. Any other activity using web ports could do the same.

My solution was to use fuser -k 443/tcp after restarting for certbot in the future.


Concerning Letsencrypt-related causes, I wouldn't be surprised to find that the --nginx module for certbot gets an update that prevents this in the future.

Upvotes: 2

Couitchy
Couitchy

Reputation: 1109

I noticed this error occurred on my Ubuntu Server just after installing GitLab EE for the first time. The reason behind this is that another NGINX instance is bundled with GitLab EE and it was somehow conflicting with the main NGINX instance. So I had to follow these steps:

  1. Edit /etc/gitlab/gitlab.rb
  2. Set nginx['listen_addresses'] and nginx['listen_port'] to appropriate values that do not conflict with other sites enabled in the main NGINX instance
  3. Reconfigure GitLab EE with command sudo gitlab-ctl reconfigure
  4. Restart main NGINX instance using command sudo /etc/init.d/nginx restart

I hope maybe this will help someone!

Upvotes: 0

ABDULLOKH MUKHAMMADJONOV
ABDULLOKH MUKHAMMADJONOV

Reputation: 5234

For those who have tried every suggested answer from here and other posts with no success, make sure that an unremover docker container is not using port 80

In my case I first used Nginx inside docker (compose) with network_mode:host on default port 80. This makes Nginx container run just like Nginx on host (on port 80).

Then I installed Nginx on host server, but I could not run start it. It failed with [::]:80 failed (98: Address already in use) error since Docker Nginx container was bound to host's port 80.

If this is the case for you have 3 options:

  1. (if you want Nginx installed both on the host and inside docker) Stop Docker Nginx container. Then run Docker Nginx container without network_mode:host and install Nginx on the host. This way you can have multiple Nginx containers running inside docker without interferring host's Nginx.
  2. (if you want Nginx installed on the host) Simply stop Docker Nginx container and run Nginx on the host
  3. (if you want Nginx installed inside docker) Remove Nginx on the host and run Nginx inside Docker with network_mode:host. In this case you have to manage Nginx settings from Docker

Upvotes: 0

TheOnlyWayUp
TheOnlyWayUp

Reputation: 109

I solved this by killing the running tailscaled instance (I did tailscale down beforehand, didn't work, kill the daemon instead.)

sudo pkill -f tailscaled & wait $!

Upvotes: 0

Hassan Shamshir
Hassan Shamshir

Reputation: 121

In my case, I have checked the processes in linux ps -aux | grep www-data for www-data user and see apache is runing due to this my nginx can't start and show the following error but I have fixed the issue by stopping apache2 and after it start the nginx enter image description here

enter image description here

Upvotes: 5

Tyler
Tyler

Reputation: 510

With Nginx bind errors that happen while running certbot, disabling the perl module worked for me when every other solution failed.

sudo rm /etc/nginx/modules-enabled/50-mod-http-perl.conf
sudo systemctl stop nginx
sudo killall -9 nginx
sudo systemctl start nginx

Credit to _az.

To test:

sudo certbot renew --dry-run

Upvotes: 3

Parth Developer
Parth Developer

Reputation: 1887

For me the issue was that I have added below code for my backend server that was causing issue. I added this on my default config of nginx.

Removing below code worked :)

    location  {
           proxy_pass http://localhost:8080/;       //remove
           proxy_http_version 1.1;                  //remove
           proxy_set_header Upgrade $http_upgrade;  //remove
           proxy_set_header Connection 'upgrade';   //remove
           proxy_set_header Host $host;             //remove
           proxy_cache_bypass $http_upgrade;        //remove
    }

Upvotes: 1

Amit Dagar
Amit Dagar

Reputation: 197

PROBLEM - I had this issue. Basically, what I did is I started nginx with the normal nginx command and then tried to restart nginx with service service nginx reload / restart and then I get this error

SOLUTION - Just stopped nginx by using nginx -s stop and then restarted nginx server using service nginx restart issue resolved.

Upvotes: 1

Shafin Hasnat
Shafin Hasnat

Reputation: 649

Open /etc/nginx/nginx.conf with editor and comment out this line-

include /etc/nginx/sites-enabled/*;

Restart nginx and you are good to go!

Upvotes: 4

random-forest-cat
random-forest-cat

Reputation: 35984

I fixed this by running:

sudo apachectl stop

It turns out apache was running in the background and prevented nginx from starting on the desired port.

On Ubuntu, run:

sudo /etc/init.d/apache2 stop

Upvotes: 493

Dan Dinh
Dan Dinh

Reputation: 8629

My case is different, I had to kill the running Nginx process to restart it.

Instead of

sudo systemctl restart nginx

I had to use:

sudo pkill -f nginx & wait $!
sudo systemctl start nginx

Upvotes: 429

Sean
Sean

Reputation: 271

Multiple services can be listening on the same port. This issue typically comes from mixing Apache and NGINX on the same machine.

To check:

sudo netstat -plant | grep 80

Stop Apache & Restart NGINX:

sudo systemctl stop apache2 && sudo systemctl restart nginx && sudo systemctl status nginx

Upvotes: 10

Trần Tuấn Anh
Trần Tuấn Anh

Reputation: 333

I solved by running

sudo killall apache2

sudo fuser -k 443/tcp

finally

sudo service nginx start

Upvotes: 16

jack
jack

Reputation: 843

try to do this command

sudo fuser -k 443/tcp
service nginx restart

the fuser command will find the process id(PID) and the -k flag will kill the process enabling the nginx restart.

Upvotes: 58

Sebastien DA ROCHA
Sebastien DA ROCHA

Reputation: 472

I use supervisor to run Nginx and Gunicorn side by side on a Docker container.

This was the configuration used for supervisor :

[supervisord]
nodaemon=true

[program:gunicorn]
command = /project/start.sh
user = www-data

[program:nginx]
command=/usr/sbin/nginx

The problem was how I launched Ngnix by default it runs in the foreground. This makes supervisor retry to run another instance of Nginx.

You can fix issue by adding -g 'daemon off;' to the command line or daemon off; on top of the config file, Nginx stayed in the foreground, supervisor stopped trying to run another instance.

Upvotes: 6

atazmin
atazmin

Reputation: 5707

I had this error on AWS Lightsail, used the top answer above

from

listen [::]:80;

to

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

and then click on "reboot" button inside my AWS account, I have main server Apache and Nginx as proxy.

Upvotes: 1

Tampa
Tampa

Reputation: 78352

I found the issue that I never had before.

I just had to delete /etc/nginx/sites-available/default. Then it worked.

This deletion will delete the symlink only, As backup you can find default file in /etc/nginx/sites-enabled/default

My conf was in /etc/nginx/default.

Upvotes: 52

nmishin
nmishin

Reputation: 3064

I have the same issue, but I see that port 80 listened by Nginx:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9730/nginx 

But when I try to restart it, I have the error:

    service nginx restart
Stopping nginx:                                            [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()

My issue was in the config file, I am set PID file, and seems system cant catch it properly:

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

When I removed it, it worked.

Upvotes: 0

Rishikesh Chandra
Rishikesh Chandra

Reputation: 691

In my case, one of the services either Apache, Apache2 or Nginx was already running and due to that I was not able to start the other service.

Upvotes: 1

Matthijs
Matthijs

Reputation: 867

In my case the culprit turned out to be a server-block that contained:

        listen  127.0.0.1:80;
        listen  [::1]:80 ipv6only=on;
        server_name  localhost;

On Linux, a socket listening on a specific IP (e.g. [::1]:80) conflicts with a socket listening on the same port but any IP (i.e. [::]:80). Normally nginx will transparently deal with this problem by using a single socket behind this scenes. However, explicitly specifying ipv6only (or certain other options) on the listen directive forces nginx to (try to) create a separate socket for it, thus resulting in the Address already in use error.

Since ipv6only=on is the default anyway (since 1.3.4) the fix was simply to remove that option from this directive, and making sure ipv6only wasn't used anywhere else in my config.

Upvotes: 1

SAURABH
SAURABH

Reputation: 311

I was also getting the same error.

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

and when i typed the localhost in the browser, then i was getting

It works!

This is the default web page for this server.

The web server software is running but no content has been added, yet. instead of nginx welcome page, apache2 is running on the same port,

  1. find the apache2 ports.conf file

    sudo /etc/apache2/ports.conf
    
  2. change the port other then 80 , i make it as 70

  3. save the file

  4. restart your system

it will works for you also, if you type the localhost in the browser, you will get nginx welcome page

Upvotes: 31

Black
Black

Reputation: 10407

My issue was that I had overlapping listen directives. I have managed to figure out overlapping directives by running

grep -r listen /etc/nginx/*

Two files were listening at the same port:

/etc/nginx/conf.d/default.conf:           listen 80;  
/etc/nginx/sites-enabled/default.conf:    listen 80;

Upvotes: 22

Lance Cleveland
Lance Cleveland

Reputation: 3204

To follow on to @lfender6445 and @SAURABH answers --

My problem was also the fact that after upgrading to Vagrant 2.2.2 Apache2 was running as a web server when the guest booted. In the past I only had nginx as a web server.

vagrant ssh into the box and run the following command to disable Apache2 from starting up whenever the guest box boots:

sudo update-rc.d -f apache2 remove

Exit ssh, vagrant halt, vagrant up. Problem solved.

Upvotes: 0

oscarz
oscarz

Reputation: 1254

I met similar problem. the log is like below

2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: still could not bind()
2018/10/31 12:54:23 [alert] 127997#127997: unlink() "/run/nginx.pid" failed (2: No such file or directory)
2018/10/31 22:40:48 [info] 36948#36948: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:68
2018/10/31 22:50:40 [emerg] 37638#37638: duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/default:18
2018/10/31 22:51:33 [info] 37787#37787: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:68

The last [emerg] shows that duplicate listen options for [::]:80 which means that there are more than one nginx block file containing [::]:80.

My solution is to remove one of the [::]:80 setting

P.S. you probably have default block file. My advice is to keep this file as default server for port 80. and remove [::]:80 from other block files

Upvotes: 1

Wagner Pereira
Wagner Pereira

Reputation: 1078

I had the same problem in letsencrypt (certbot) and nginx,

ref: https://github.com/certbot/certbot/issues/5486

this error does not have a solution yet

so, a changed a cron for renew (putting a reload after renew) (using suggest from certbot)

-- in /etc/cron.d/certbot
from
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew 
to
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook "service nginx stop" --post-hook "service nginx start"

logs (short):

-- in /var/log/syslog
Jun 10 00:14:25 localhost systemd[1]: Starting Certbot...
Jun 10 00:14:38 localhost certbot[22222]: nginx: [error] open() "/run/nginx.pid$
Jun 10 00:14:41 localhost certbot[22222]: Hook command "nginx" returned error c$
Jun 10 00:14:41 localhost certbot[22222]: Error output from nginx:
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] still could not bind()
Jun 10 00:14:41 localhost systemd[1]: Started Certbot.


-- in /var/log/nginx/error.log
2018/06/10 00:14:27 [notice] 22233#22233: signal process started
2018/06/10 00:14:31 [notice] 22237#22237: signal process started
2018/06/10 00:14:33 [notice] 22240#22240: signal process started
2018/06/10 00:14:34 [notice] 22245#22245: signal process started
2018/06/10 00:14:38 [notice] 22255#22255: signal process started
2018/06/10 00:14:38 [error] 22255#22255: open() "/run/nginx.pid" failed (2: No $
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: still could not bind()

Upvotes: 9

Kamal Kumar
Kamal Kumar

Reputation: 3763

First change apache listen port 80 to 8080 apache in /etc/apache2/ports.conf include

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

then add nginx as reverse proxy server that will listen apache port

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
}

After changes restart nginx server

sudo service nginx restart

Now all traffic will be handled by nginx server and send all dynamic request to apache and static conten is served by nginx server.

For advance configuration like cache :

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

Upvotes: 2

Nathan
Nathan

Reputation: 8036

[::]:80 is a ipv6 address.

This error can be caused if you have a nginx configuration that is listening on port 80 and also on port [::]:80.

I had the following in my default sites-available file:

listen 80;
listen [::]:80 default_server;

You can fix this by adding ipv6only=on to the [::]:80 like this:

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

For more information, see:

http://forum.linode.com/viewtopic.php?t=8580

http://wiki.nginx.org/HttpCoreModule#listen

Upvotes: 251

Allen
Allen

Reputation: 434

I had several *.save files (emergency dumps from nano) from different NGINX config files in my sites-avilable dir. Once I deleted these .save files, NGINX restarted fine. I assumed these were harmless since there were no corresponding symlinks, but I guess I was wrong.

Upvotes: 0

Related Questions