使用 Nginx 和 PHP FPM 进行原子部署

使用 Nginx 和 PHP FPM 进行原子部署

我正在尝试获取原子部署,我不想丢失/丢弃任何请求。

我使用 Capistrano 部署代码,典型的情况是,您有一个“当前”目录,它是“发布”文件夹中最新版本的符号链接。我正在使用 Nginx、带有 Opcache 的 PHP 5.5 FPM。按照在 Nginx 中只使用 $realpath_root 的想法,我期望简单的 nginx reload 就足够了,但事实并非如此。使用 nginx reload 似乎我没有丢失任何请求,但 php-fpm 仍在执行来自先前旧目录的代码。如果我执行“php-fpm reload”,则 php fpm 正在从新目录读取,但我仍然可以看到我正在丢失一些请求:

'从上游读取响应头时 recv() 失败(104:对端重置连接)'

问题是:我是否应该重新加载 PHP FPM?

据我所知,nginx 正在将路径传递给 php-fpm,并且从 nginx 调试日志中我可以看到传递的路径是正确的,但 php-fpm 仍然执行旧的路径。

2014/09/23 17:13:22 [debug] 26234#0: *1742 http script var: "/shop/www/htdocs/current/web"
2014/09/23 17:13:22 [debug] 26234#0: *1742 posix_memalign: 00000000010517A0:4096 @16
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script copy: "SCRIPT_FILENAME"
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script var: "/shop/www/htdocs/ive/releases/20140923124417/web"
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script var: "/app.php"
2014/09/23 17:13:22 [debug] 26234#0: *1742 fastcgi param: "SCRIPT_FILENAME: /shop/www/htdocs/live/releases/20140923124417/web/app.php"
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script copy: "DOCUMENT_ROOT"
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script var: "/shop/www/htdocs/live/releases/20140923124417/web"
2014/09/23 17:13:22 [debug] 26234#0: *1742 fastcgi param: "DOCUMENT_ROOT: /shop/www/htdocs/live/releases/20140923124417/web"
2014/09/23 17:13:22 [debug] 26234#0: *1742 http script copy: "APPLICATION_ENV"

我遗漏了什么?谢谢

答案1

听起来你已经对默认的 OPcache 配置进行了更改。

默认值为:

opcache.validate_timestamps = 1
opcache.revalidate_freq = 2

这指定了 OPcache 在检查磁盘上的文件是否已更改之前是否应等待以及等待多长时间。在本例中,是的,两秒钟后。

首先,尝试重新启用此选项,因为听起来您已将其关闭。

答案2

我也遇到过这个问题,最后我找到了解决方案。

$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
$ chmod +x cachetool.phar

您可以连接到自动猜测的 fastcgi 服务器(如果 /var/run/php5-fpm.sock 是一个文件或 127.0.0.1:9000)

apc
  apc:bin:dump             Get a binary dump of files and user variables
  apc:bin:load             Load a binary dump into the APC file and user variables
  apc:cache:clear          Clears APC cache (user, system or all)
  apc:cache:info           Shows APC user & system cache information
  apc:cache:info:file      Shows APC file cache information
  apc:key:delete           Deletes an APC key
  apc:key:exists           Checks if an APC key exists
  apc:key:fetch            Shows the content of an APC key
  apc:key:store            Store an APC key with given value
  apc:sma:info             Show APC shared memory allocation information
opcache
  opcache:configuration    Get configuration information about the cache
  opcache:reset            Resets the contents of the opcode cache
  opcache:status           Show summary information about the opcode cache
  opcache:status:scripts   Show scripts in the opcode cache

例子 :

    [root@ip-172-31-5-244 ~]# php cachetool.phar opcache:status
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 42.71 MiB                       |
| Memory free          | 85.29 MiB                       |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 5.31 MiB                        |
| Strings memory free  | 2.69 MiB                        |
| Number of strings    | 103847                          |
+----------------------+---------------------------------+
| Cached scripts       | 1261                            |
| Cached keys          | 2748                            |
| Max cached keys      | 7963                            |
| Start time           | Thu, 08 Feb 2018 02:28:56 +0000 |
| Last restart time    | Thu, 08 Feb 2018 03:10:19 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 1                               |
| Hits                 | 47839                           |
| Misses               | 1269                            |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 97.415899649752                 |
+----------------------+---------------------------------+
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]# php cachetool.phar opcache:reset
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]# php cachetool.phar opcache:status
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 10.43 MiB                       |
| Memory free          | 117.57 MiB                      |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 545.69 KiB                      |
| Strings memory free  | 7.47 MiB                        |
| Number of strings    | 103847                          |
+----------------------+---------------------------------+
| Cached scripts       | 0                               |
| Cached keys          | 0                               |
| Max cached keys      | 7963                            |
| Start time           | Thu, 08 Feb 2018 02:28:56 +0000 |
| Last restart time    | Thu, 08 Feb 2018 03:19:00 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 2                               |
| Hits                 | 0                               |
| Misses               | 2                               |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 0                               |
+----------------------+---------------------------------+

您可以注意到内存、缓存键、命中数全部都变为 0 :-)。这非常有用。我也可以轻松地将其与 Ansible 关联。

它适用于 apcu 和其他东西:查看更多 http://gordalina.github.io/cachetool/

相关内容