ChatGPT解决这个技术问题 Extra ChatGPT

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
I found the issue that I never had before. I had to delete /etc/nginx/sites-available/default. Then it worked. My conf was in /etc/nginx/default
I was running Nginx+Varnish and had this error. The solution was top stop both, then start nginx first and then varnish.
you may try stopping named service.
had you removed your default from sites-enabled?
i tried all the solutions here. Checked all the ports in use (it was being used by NGINX itself) so i killed the port and restarted it. Worked like a charm.

v
vinzee

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

Use sudo /etc/init.d/apache2 stop to stop apache on Ubuntu 14.04
The reason is because you cannot have two web servers listening to the same port. Choose one, and if required, proxy. E.g. use nginx, but for certain requests (such as for php files), proxy to Apache port.
Thanks. But any idea why did it suddenly start? I didn't touch the server for a week, but it had started last night.
maybe your server rebooted and you have apache configured to start on boot? and or another service requires apache as a dependency. it might also help to know more about your OS setup
Thank for your solution. It works for me, I use Google Cloud Platform
D
Dee

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

I was taking 404 Not Found, and I used your suggestion, now I did fix my problem. Thanks.
This worked for me. Nothing else did. Not sure what process starts when the system reboots, but I'll just add this in my restart nginx routine. Thanks!
This solution fixed my issue 👍
best to stick to operating on your nginx service in one way. I had it get to a bad state by using nginx -s reload instead of using systemctl
i love you; thank you. sudo pkill -f nginx & wait $! saved the day.
i
iSkore

[::]: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


You can also fix it by just removing the listen 80; because listen [::]:80 listents to both IPv4 and IPv6. Watch out though because some systems (like FreeBSD) separate the IPv4 and IPv6 sockets and then it won't work, but for Linux it should be fine. wiki.nginx.org/HttpCoreModule#listen
Thanks for digging into this and providing an explanation to why deleting /etc/nginx/sites-available/default helps with the above error.
Even when I comment out the ipv6 line, I still get the same error when I try sudo service nginx restart. When I do netstat -tulpn |grep 80, I get only once nginx process (0.0.0.0:80). Any ideas why it won't restart?
I second what @rednaw said, but instead of removing the listen, you can simply comment it out
Setting ipv6only=on fixed the problem, but nginx should really detect that it's trying to bind to the same interface:port twice.
s
shareef

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.


+1 This solution worked for me, but then I thought there must be something in default that is actually causing the problem, so I dug a bit deeper and provided an answer with more info.
I would argue that deleting the default site configuration template is not the best solution - commenting the line that contains listen 80; in that same template already solves the problem and correctly so. Your trick works but it's not what I would future readers of your question to do. That's why I suggest you select @Nathan's answer as the correct answer.
I had the same problem after running apt-get dist-upgrade, which upgraded the nginx package, which created a link in /etc/nginx/sites-enabled to /etc/nginx/sites-available/default. nginx was trying to load this default config, which listens to port 80 over IPv6, then it was also loading my read my real configs. Removing that symlink fixed the problem.
YOU DON'T NEED TO DELETE /etc/nginx/sites-available/default, simply remove the symbolic link to it - sudo rm /etc/nginx/sites-enabled/default
I experienced this issue when trying to run nginx on port 8080 and varnish on port 80 and similar to this answer, I found the problem to an nginx default config still listening on port 80 even though my sites-available configs all listened on port 8080. This was located at /etc/nginx/conf.d/default
E
Eje

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,

find the apache2 ports.conf file sudo /etc/apache2/ports.conf change the port other then 80 , i make it as 70 save the file restart your system

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


You may simply not want to run both Apache2 and nginx at the same time. I found the Apache installation starts the service. So, I issued "sudo /etc/init.d/apache2 stop", and then was able to start nginx normally. This also saves you from rebooting your system.
You are right that deleting the /etc/nginx/sites-enabled/default sym link does prevent it from listening on two ports. I find it actually a bit annoying that every tutorial on nginx starts by suggesting everyone delete the "default" link but that is a different topic I suppose.
@IgorGanapolsky switch Apache to a different port?
g
greybeard

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.


This worked perfectly :)
B
Black

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;

That's a very neat way to check things: grep -r listen /etc/nginx/* thanks for sharing!
Thank you sir! One question though, won't modifying the other listening to port 80 to let's say 70 cause any errors?
T
Trần Tuấn Anh

I solved by running

sudo killall apache2

sudo fuser -k 443/tcp

finally

sudo service nginx start

W
Wagner Pereira

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()

super random, but this was what my issue was i believe. thanks for posting this. my error seemed to manifest because of a outdated domain entry it was trying to renew but couldn't because i didn't control the domain anymore.
I guess this is probably my issue too. but I need to wait to have a look
Z
Zbigniew Malcherczyk

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.


K
Kamal Kumar

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


p
possum jones

I know this is old, but also make sure that none of your docker containers are already on port 80. That was my issue.


similar to this, in my case I have haproxy running
B
Big Dumb

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


o
oscarz

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


i did it to, i am removed from other config file [::]:80 and its work! BUT ITS SECURE ? when no default listener ?
M
Matthijs

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.


R
Rishikesh Chandra

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.


a
atazmin

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.


A
Allen

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.


L
Lance Cleveland

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.


D
Deejay

If problem persists after trying any of the above solutions, Restart your server once. It worked for me :)


Yes, it would. But killing the nginx process and then restarting just nginx also works. That's the answer by @datdinhquoc.
z
zombi_man

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.


B
Brian Nicholls

Quick solution that worked for me on Ubuntu. First find the currently running instance of nginx:

ps aux | grep nginx

Copy the pid (e.g. 123456) and run

kill -9 123456


R
Redjam

For me the problem was that I was running an app with Supervisor and another with Systemd. In the app running with Supervisor I had a config file to handle Nginx. I stoped this process and restarted Supervisor without it.

Now Nginx is launched by Systemd and I don't have the issue anymore.


D
Delowar Hossain

I was running Varnish server with nginx. My varnish server was running on port 80 and for this reason, my nginx server wasn't starting. I changed the port to 8088 in all nginx configuration files but my default.conf file was in /etc/nginx/conf.d directory in Ubuntu server.

Because of my conf.d directory was included in main /etc/nginx.conf file, nginx was not starting.

After renaming the /etc/nginx/conf.d/default.conf to /etc/nginx/conf.d/default.conf.backup, nginx started successfully or I could comment out or remove the include /etc/nginx/conf.d/*.conf; from my nginx.conf file.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now