Load balancing a Reverse Proxy with Nginx, HHVM and PHP FPM

Load balancing a Reverse Proxy with Nginx, HHVM and PHP-FPM HHVM gives superior performance while maintaining the development flexibility that PHP provides. But there are some noted inconsistencies .

It is better to workaround the inconsistencies and resolve however if you want to run your existing application in HHVM without breaking code in production it is better to use PHP-FPM alongside HHVM.

Proposed architecture: Keep two upstream server in Nginx , one running HHVM and other PHP-FPM. You can run HHVM and PHP-FRM either in local or remote. The PHP-FPM server will be in backup mode and all the request will by default proxied to HHVM server. For a particular request if there is any error or timeout due to inconsistencies related to HHVM in your code, the request will be handled by PHP-FPM. To store session you can use a common Redis server.

Here is the sample Nginx configuration: In this example both HHVM and PHP-FPM is running in the same local server. proxy_next_upstream parameter is used to forward request to backup node which is running PHP-FPM in case there is error in processing your code by HHVM. Otherwise it will through bad gateway to the browser.

To store session in Redis: Here the Redis server is installed in same server.

vim /etc/hhvm/php.ini
session.save_handler = redis
session.save_path = "tcp://localhost:6379"
vim /etc/php5/fpm/php.ini
session.save_handler = redis
session.save_path = "tcp://localhost:6379"