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

huangapple go评论95阅读模式
英文:

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

问题

我正在使用Nginx在同一台计算机上运行前端和后端。

前端:React(localhost:3000)

后端:GO-Lang(localhost:8080)

当您尝试从前端代码访问后端API时,可能会遇到CORS错误。

为了解决这个问题,我使用nginx在不同的路由上为前端和后端创建了代理,但端口相同(localhost:8000)。

前端:/

后端:/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;
       
    }
}

我使用 sudo nginx -s stop 停止了nginx。

关闭终端。

完全退出终端。

但是我没有关闭电脑。

  1. 今天我尝试了一些其他的配置,使用了相同的端口(8000),并得到了标题中提到的错误。

  2. 所以我使用了不同的端口,一切都按预期工作。

  3. 但是当我再次恢复到上述配置时,它显示了标题中提到的错误。

问题

  1. 我观察到在步骤2中使用的所有端口现在都被占用了,尽管我之前停止了所有的服务器,但我现在无法使用它们。

  2. 在恢复到初始配置后,尽管我尝试使用的端口是8000,但它仍然显示该端口被占用。

如何再次使用端口8000?

英文:

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

得分: 2

在活动监视器中终止进程

  1. sudo nginx 启动一个进程
  2. 运行 ps -ef | grep nginx 来查看正在运行的 nginx 进程。
  3. 如附图所示,当我使用 sudo nginx -s stop 停止服务时,ps -ef | grep nginx 返回的搜索结果中缺少2行(我不确定最后一行是否在两个结果中都存在。我猜想它可能与搜索有关)
  4. 这可能意味着我成功地停止了进程 ✅

解决方案如下

  1. 运行 ps -ef | grep nginx。如果在停止进程后仍然看到一些正在运行的进程,可能是进程没有正确停止
  2. 要正确终止/杀死它
  3. 打开活动监视器(在 Mac 的 Spotlight 搜索中搜索活动监视器)
  4. 转到网络
  5. 搜索 nginx 并全部退出。
  6. 在某些情况下,您在网络中找不到 nginx
  7. 然后转到 CPU 并搜索 nginx
  8. 您应该找到一些消耗 CPU 的 nginx 进程。
  9. 全部退出。
  10. 现在尝试运行 sudo nginx。一切应该正常工作。✅

> <img src="https://i.stack.imgur.com/6W2sj.jpg" width="700"/>

英文:

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 ✅

> <img src="https://i.stack.imgur.com/z7W8p.png" width="700"/>

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

> <img src="https://i.stack.imgur.com/6W2sj.jpg" width="700"/>

huangapple
  • 本文由 发表于 2022年9月15日 17:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/73729080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定