简体   繁体   English

NGINX + phpFPM负载均衡器和会话

[英]NGINX + phpFPM load balancer and sessions

i have one question, i am using nginx and PHPFPM . 我有一个问题,我正在使用nginxPHPFPM I am using loadbalancer for 2 phpfpm servers . 我正在使用loadbalancer 2个phpfpm服务器

To keep sessions from both phpfpm servers synced i used memcached. 为了保持两个phpfpm服务器的会话同步,我使用了memcached。 But when i use memcached i see that page is slowing down . 但是当我使用memcached时,我看到该页面正在变慢

When i use files as sessions save type web is runing faster, but sessions are not synced imediately (i guess files are owerwriting). 当我使用文件作为会话保存类型 web运行速度更快,但会话不会立即同步(我猜文件是owerwriting)。 I am using NFS to share sessions. 我正在使用NFS来共享会话。

Any ideas please how to sync sessions when using nginx loadbalancer for phpfpm servers? 有什么想法,请问如何在为phpfpm服务器使用nginx loadbalancer时同步会话?

The speed increase you're probably seeing here in PHP using NFS over memcached is a deceptive one by nature. 使用NFS over memcached在PHP中看到的速度增加本质上是一种欺骗性的。 PHP session storage defaults to lock acquisition on a first-come-first-served basis. PHP会话存储默认以先到先得的方式锁定获取。 Which means that two concurrent requests made to PHP for the same session will cause the first request to lock the session until either PHP is done or you explicitly call session_write_close() from your code, to release the lock. 这意味着对同一会话的两个并发请求将导致第一个请求锁定会话,直到PHP完成或您从代码中显式调用session_write_close() ,以释放锁定。

But in a file based session store, PHP relies on flock, which doesn't work in NFS. 但是在基于文件的会话存储中,PHP依赖于flock,这在NFS中不起作用。

The NFS (Versions 2 and 3) protocol does not support file locking NFS(版本2和3)协议支持文件锁定

See this answer on unix stackexchange 在unix stackexchange上查看此答案

So for a distributed session store, you rarely want slow file-system based lock. 因此,对于分布式会话存储,您很少需要基于文件系统的慢速锁定。 Most in-memory stores work faster anyway. 无论如何,大多数内存商店的工作速度更快。 And since NFS can't typically handle flock calls, your sessions will get corrupted if two concurrent requests try to write to the same session file. 由于NFS通常无法处理flock调用,因此如果两个并发请求尝试写入同一会话文件,则会话将被破坏 In other words, what you're seeing as faster is just basically your requests potentially corrupting their sessions faster , because there's no lock on the session for concurrency. 换句话说,您所看到的更快的是基本上您的请求可能会更快地破坏其会话,因为并发会话没有锁定。

If your requests take a really long time and don't require the session it's best to explicitly call session_write_close as early in the code as possible when you are done with the session so that any other concurrent requests coming in can get at the session. 如果您的请求需要很长时间并且不需要会话,则最好在完成会话后尽可能早地在代码中显式调用session_write_close ,以便任何其他并发请求可以进入会话。 This is typically a problem when you're doing a lot of long-polling requests to PHP (say over AJAX). 当您对PHP进行大量长轮询请求时(例如通过AJAX),这通常是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM