Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Last active December 29, 2015 05:02
Show Gist options
  • Save tegansnyder/3107686ddcd40f322678 to your computer and use it in GitHub Desktop.
Save tegansnyder/3107686ddcd40f322678 to your computer and use it in GitHub Desktop.
Exclude a directory from being processed by HHVM (HHVM Bypass for PHP-FPM)

I was looking for a way to load certain files using regular PHP and mix and match HHVM with PHP-FPM. Here is an example of how to use PHP-FPM to process files in the /exclude/ directory and HHVM for all other files.

## Use regular ol' PHP for files in this directory
location ^~ /exclude/ {
  include fastcgi_params;
  fastcgi_pass  127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  break;
}

## HHVM
location ~ \.php$ {
  if (!-e $request_filename) { rewrite / /index.php last; }

  expires off;
  fastcgi_pass 127.0.0.1:8080;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;

  fastcgi_keep_conn on;
}

Here is a another example. Lets say you are using rewrites and you can't target a specific location directive due to it so you want to use and if statement and key off the request_uri paramater. Here is a way to do that:

location ~ \.php$ {

  add_header X-request_uri "$request_uri";

  if (!-e $request_filename) { rewrite / /index.php last; }

  ## set a variable to use hhvm
  set $use_hhvm 1;

  ## if request_uri is /checkout or /cybersource dont use hhvm
  if ($request_uri ~ ^/(checkout|cybersourcesop)) {
    fastcgi_pass 127.0.0.1:9000;
    set $use_hhvm 0;
  }

  expires off; ## Do not cache dynamic content

  ## otherwise use hhvm
  if ($use_hhvm = 1) {
    fastcgi_pass 127.0.0.1:8080;
  }

  fastcgi_index index.php;
  #fastcgi_param HTTPS $fastcgi_https;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;

  fastcgi_keep_conn on; #hhvm param
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment