Mac nginx: [emerg] bind() to 0.0.0.0:8000 failed (48: Address already in use)

Viewed 25

I am using Nginx to run both the frontend and backend from the same PC.

Frontend: React (localhost:3000)

Backend : GO-Lang (localhost:8080)

You can expect CORS error when you try to access BE APIs from your FE code.

To overcome this I used nginx to create proxies for FE and BE on Different routes but on the same port (localhost:8000)

FE: /

BE: /server

nginx.conf

upstream server {
server localhost:8080;
}

upstream client {
server localhost:3000;
}

server {
    listen 8000;
    server_name localhost;

    location / {
        proxy_pass http://client;
    }

    location ~ /server/(?<section>.*) {
        rewrite ^/server/(.*)$ /$1 break;

        proxy_pass http://server;
       
    }
}

I stopped the nginx with sudo nginx -s stop

Closed terminal

Quit terminal completely

But didn't shut down my PC

  1. Today I tried some other configs with the same port (8000) and got the error mentioned in the title.

  2. So used a different port everything worked as expected.

  3. But when I reverted my config to the above config again. It is showing the error mentioned in the title.

The problem

  1. What I observed is all the ports I used in step 2 are occupied now and I am not able to use them although I have stopped all the servers earlier.

  2. After reverting back to the initial config even though the port I am trying to use is 8000 it still says that the port is occupied.

How can I use my port 8000 again?

1 Answers

Kill the process in Activity Monitor

  1. sudo nginx starts a process
  2. Run ps -ef | grep nginx to know the running nginx processes.
  3. As you can see in the image attached when I stopped the service using sudo nginx -s stop 2 lines are missing in the search returned by ps -ef | grep nginx ( I am not sure about the last line that is common in both the results. I am guessing that it must be something related to the search )
  4. This probably means that I am able to stop the process successfully ✅

Solution here

  1. Run ps -ef | grep nginx. If you can see some processes running even after stopping the process it might be that the process didn't stop properly
  2. To terminate/ kill it properly
  3. Go to Activity Monitor ( search for activity monitor in Mac Spotlight search )
  4. Go to Network
  5. Search for nginx and quit them all.
  6. In some cases you won't find nginx in network
  7. Then go to CPU and search for nginx.
  8. You must find some nginx processes consuming CPU.
  9. Quit them all.
  10. Now try running sudo nginx. Everything should work as expected. ✅
Related