Nginx 502 bad gateway

把 wordpress 裝好後(nginx + php),發現在瀏覽頁面時,偶發出現 502 bad gateway。查了 nginx 的 error log,發現有:

connect() failed (111: Connection refused) while connecting to upstream

爬了幾篇 Stack Overflow,其中一篇提到用 netstat 檢查。突然靈光一閃,該不會是 listening port 有衝突。果然 php-fpm 的 default port 是 9000,而主機上剛好有一個 node process 也是 listen 在同一個 port,改掉後就正常了。

但後來仔細一想如果 port 相衝,一開始啟動 php-fpm service 時就應該會有錯誤。再去檢查 nginx 的設定:

upstream php {
  server unix:/var/run/php/php7.3-fpm.sock;
  server 127.0.0.1:9000;
}

才發現 root cause 是這行 server 127.0.0.1:9000,把這行註解掉就正常了。

難怪改掉 node process 的 port 後,用 netstat 再檢查一遍,根本也沒其它的 process listen 在 9000,純粹是 nginx 的設定錯誤造成的。

Leave a comment

Your email address will not be published. Required fields are marked *