英文:
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。
关闭终端。
完全退出终端。
但是我没有关闭电脑。
-
今天我尝试了一些其他的配置,使用了相同的端口(8000),并得到了标题中提到的错误。
-
所以我使用了不同的端口,一切都按预期工作。
-
但是当我再次恢复到上述配置时,它显示了标题中提到的错误。
问题
-
我观察到在步骤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
-
Today I tried some other configs with the same port (8000) and got the error mentioned in the title.
-
So used a different port everything worked as expected.
-
But when I reverted my config to the above config again. It is showing the error mentioned in the title.
The problem
-
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.
-
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
在活动监视器中终止进程
sudo nginx
启动一个进程- 运行
ps -ef | grep nginx
来查看正在运行的 nginx 进程。 - 如附图所示,当我使用
sudo nginx -s stop
停止服务时,ps -ef | grep nginx
返回的搜索结果中缺少2行(我不确定最后一行是否在两个结果中都存在。我猜想它可能与搜索有关) - 这可能意味着我成功地停止了进程 ✅
解决方案如下
- 运行
ps -ef | grep nginx
。如果在停止进程后仍然看到一些正在运行的进程,可能是进程没有正确停止 - 要正确终止/杀死它
- 打开活动监视器(在 Mac 的 Spotlight 搜索中搜索活动监视器)
- 转到网络
- 搜索
nginx
并全部退出。 - 在某些情况下,您在网络中找不到
nginx
- 然后转到 CPU 并搜索
nginx
。 - 您应该找到一些消耗 CPU 的
nginx
进程。 - 全部退出。
- 现在尝试运行
sudo nginx
。一切应该正常工作。✅
> <img src="https://i.stack.imgur.com/6W2sj.jpg" width="700"/>
英文:
Kill the process in Activity Monitor
sudo nginx
starts a process- Run
ps -ef | grep nginx
to know the running nginx processes. - 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 byps -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 ) - 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
- 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 - To terminate/ kill it properly
- Go to Activity Monitor ( search for activity monitor in Mac Spotlight search )
- Go to Network
- Search for
nginx
and quit them all. - In some cases you won't find
nginx
in network - Then go to CPU and search for
nginx
. - You must find some
nginx
processes consuming CPU. - Quit them all.
- Now try running
sudo nginx
. Everything should work as expected. ✅
> <img src="https://i.stack.imgur.com/6W2sj.jpg" width="700"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论