Nginx 搭配 PHP-FPM 時轉用 Unix/TCP socket 時出現 502 Bad Gateway 錯誤

自從把筆記改在粉絲頁PO再透過機器人轉發過來Blog後,就好久沒寫文,實在有點墮落XD

不過也不是就這樣擺爛沒進度就是,好多等著消化來寫 Orz

這篇文章是簡單而且自己犯的蠢毛病,把它記錄一下(因為很簡單寫 哈哈)

情境

在伺服器上安裝 Nginx + PHP5 的架構時 Unix socket 跟 TCP sockek 這兩種建立連線方式有著「速度」、「穩定度」的兩大考量,然後在設定上沒設定好就會噴 502 Bad Gateway 的錯誤。

解決辦法

  1. 同步檢查 Nginx 與 PHP-FPM 的對應設定是否一致(不論 PHP-FPM中 listen = 127.0.0.1:9000 or listen = /var/run/php5-fpm.sock是設定哪個,Nginx中也要確定有對到)
  2. PHP-FPM 的 www.conf 中將 listen.mode = 0660 解註(listen.ownerlisten.group 亦同)
  3. 最後確定有重啟 PHP-FPM 和 Nginx

後記才是核心

最近都在tune伺服器的架構,所以細節不能放過。前面提到這兩種連線方式的選擇有兩大考量

  1. 速度

如果拿這篇文章中的TCP Socket, Unix domain socket 跟 performance 這幾個關鍵字去找資料會看到不少討論跟資料。其中介紹的比較詳細是這篇mail list的討論串

而我自己拿 Pi 2 測試的結果也是差異不小

  • TCP Socket

    This is ApacheBench, Version 2.3
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/

    Benchmarking 123.204.247.250 (be patient)
    Completed 500 requests
    Completed 1000 requests
    Completed 1500 requests
    Completed 2000 requests
    Completed 2500 requests
    Completed 3000 requests
    Completed 3500 requests
    Completed 4000 requests
    Completed 4500 requests
    Completed 5000 requests
    Finished 5000 requests

    Server Software: nginx/1.8.0
    Server Hostname: 123.204.247.250
    Server Port: 80

    Document Path: /phpinfo.php
    Document Length: 192 bytes

    Concurrency Level: 1000
    Time taken for tests: 32.081 seconds
    Complete requests: 5000
    Failed requests: 3970
    (Connect: 0, Receive: 0, Length: 3970, Exceptions: 0)
    Non-2xx responses: 1289
    Keep-Alive requests: 259
    Total transferred: 250200723 bytes
    HTML transferred: 249442595 bytes
    Requests per second: 155.86 [#/sec] (mean)
    Time per request: 6416.142 [ms] (mean)
    Time per request: 6.416 [ms] (mean, across all concurrent requests)
    Transfer rate: 7616.31 [Kbytes/sec] received

    Connection Times (ms)
    min mean[+/-sd] median max
    Connect: 0 47 93.8 0 1001
    Processing: 2 3446 6801.1 472 31673
    Waiting: 1 3636 7065.2 471 31671
    Total: 2 3493 6796.0 486 31776

    Percentage of the requests served within a certain time (ms)
    50% 486
    66% 867
    75% 1518
    80% 3656
    90% 11597
    95% 17873
    98% 31225
    99% 31371
    100% 31776 (longest request)

  • Unix domain socket

    This is ApacheBench, Version 2.3
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/

    Benchmarking 123.204.247.250 (be patient)
    Completed 500 requests
    Completed 1000 requests
    Completed 1500 requests
    Completed 2000 requests
    Completed 2500 requests
    Completed 3000 requests
    Completed 3500 requests
    Completed 4000 requests
    Completed 4500 requests
    Completed 5000 requests
    Finished 5000 requests

    Server Software: nginx/1.8.0
    Server Hostname: 123.204.247.250
    Server Port: 80

    Document Path: /phpinfo.php
    Document Length: 192 bytes

    Concurrency Level: 1000
    Time taken for tests: 1.961 seconds
    Complete requests: 5000
    Failed requests: 4115
    (Connect: 0, Receive: 0, Length: 4115, Exceptions: 0)
    Non-2xx responses: 4782
    Keep-Alive requests: 3873
    Total transferred: 17177367 bytes
    HTML transferred: 16392626 bytes
    Requests per second: 2549.96 [#/sec] (mean)
    Time per request: 392.163 [ms] (mean)
    Time per request: 0.392 [ms] (mean, across all concurrent requests)
    Transfer rate: 8555.00 [Kbytes/sec] received

    Connection Times (ms)
    min mean[+/-sd] median max
    Connect: 0 42 119.9 0 1003
    Processing: 0 100 246.6 2 1502
    Waiting: 0 98 246.3 2 1499
    Total: 0 142 303.3 2 1596

    Percentage of the requests served within a certain time (ms)
    50% 2
    66% 3
    75% 314
    80% 321
    90% 393
    95% 897
    98% 1376
    99% 1445
    100% 1596 (longest request)

測試參數都是: ab -k -c 1000 -n 5000 http://123.204.247.250/phpinfo.php,環境也是預設。

結果如上,差了不小。

  1. 穩定度

雖然速度快,但因為是對檔案的交換控制,會有錯誤掉包情況發生,而且發生率很高。

兩種設定考量都有優劣,在什麼服務上設定什麼也要了解到這細節才有概念!

Facebook 外掛功能


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[//////////____30%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *