Nginx

nginx bind() to 0.0.0.0:443 failed (48: Address already in use)

  • July 27, 2020

我在 Mac OS 10.14.2 上安裝了 Laravel Valet 2.1.6。

nginx 1.15.8 是使用 brew 安裝的。

我在沒有安裝任何更新或新軟體的情況下重新啟動了我的 Mac,現在所有example.test站點都出現 502 錯誤,/usr/local/var/log/nginx/error.log日誌中顯示以下內容:

2019/01/17 20:38:47 [warn] 31277#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:1
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: bind() to 0.0.0.0:443 failed (48: Address already in use)
2019/01/17 20:38:47 [emerg] 31277#0: still could not bind()

同時,我在/Users/Myself/.config/valet/Log/nginx-error.log日誌中得到以下資訊:

2019/01/17 20:41:34 [error] 32071#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: example.test, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/Users/Myself/.config/valet/valet.sock:", host: "example.test"

當我執行時,ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'我看到了這個列表:

 PID  PPID  %CPU      VSZ WCHAN  COMMAND
32064     1   0.0  4306660 -      nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
32065 32064   0.0  4333284 -      nginx: worker process
32066 32064   0.0  4332260 -      nginx: worker process
32067 32064   0.0  4333284 -      nginx: worker process
32068 32064   0.0  4333284 -      nginx: worker process
32069 32064   0.0  4326116 -      nginx: worker process
32070 32064   0.0  4316900 -      nginx: worker process
32071 32064   0.0  4368236 -      nginx: worker process
32072 32064   0.0  4331236 -      nginx: worker process
32073 32064   0.0  4326116 -      nginx: worker process
32074 32064   0.0  4340452 -      nginx: worker process
32075 32064   0.0  4333284 -      nginx: worker process
32076 32064   0.0  4334308 -      nginx: worker process
36815  1406   0.0  4268060 -      egrep (nginx|PID)

以下都不能解決問題:

  • sudo killall nginx
  • brew services restart nginx
  • brew services restart php
  • valet restart
  • 重新啟動我的 Mac
  • valet uninstall && valet install然後valet park在相關目錄

Apache 未作為衝突服務執行。

我嘗試這樣做sudo /usr/local/opt/nginx/bin/nginx -g 'daemon off;'並得到了這個:

nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:60 failed (48: Address already in use)
nginx: [emerg] still could not bind()

執行sudo lsof -i tcp:80產生:

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
nginx   42220 root    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42221 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42222 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42223 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42224 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42225 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42226 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42227 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42228 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42229 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42230 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42231 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)
nginx   42232 Myself    7u  IPv4 0x7ac8eae7874ccb11      0t0  TCP *:http (LISTEN)

當我為埠 443 執行該命令時,輸出基本相同。

This valet issue post建議valet domain test可以修復它,但這沒有幫助。

嘗試重新安裝 PHP 但沒有運氣:

  • 沖泡解除安裝–force php
  • 沖泡清理
  • 沖泡安裝php
  • 代客解除安裝 && 代客安裝

嘗試刪除所有代客配置/證書目錄,再次解除安裝代客,然後重新安裝並重新停放我的項目目錄。

嘗試以 root 身份執行 nginx:

  • sudo brew services stop nginx
  • 確認埠 80 上沒有執行任何 nginx 程序
  • sudo brew services start nginx

這是由 PHP 7.3 引起的。

我為 7.3 刪除了 Xdebug 的 7.3 和 2.7.0beta1。

然後我做了brew install php@7.2,然後使用pecl.

現在一切正常。

我猜真正的原因是您在之前的安裝中以 root 身份執行了 brew 服務。

sudo brew services restart nginx

最後,您在 /Library/LaunchAgents 中放置了一個 LaunchAgent,這就是在啟動時以 root 身份啟動 nginx,為您提供 2 個實例。您可以在活動監視器中檢查這一點。

進入/Library/LaunchAgents並刪除有問題的代理,然後重新啟動。

引用自:https://serverfault.com/questions/949657