1

Working setup, assume a request to http://127.0.0.1:8080/path/to/resource:

  • nginx/apache receives the request and passes it to index.php behind the scenes to hhvm vi FastCGI.
  • After the the handoff via FastCGI, the $_SERVER['SCRIPT_NAME'] is always index.php and the $_SERVER['REQUEST_URI'] is from the url /path/to/resource (notice, no index.php anywhere in it)
  • I've got a framework with an index.php file in /some/path/public/index.php
  • All requests go to index.php and have their URI's (in this case `/path/to/resource) parsed by a routing system.
  • The actual code handling the request have no structural relation in form to the URI (i.e. there's no /path/to/resource/index.php)

In order to take nginx or apache out of the picture when running hhvm for dev purposes or just fun, I run hhvm in server mode (NOT FastCGI mode!!!):

cd /some/path/public/
hhvm -m server -p 8080

However, the framework doesn't handle the index.php in the path gracefully. When served by hhvm running in server mode the only urls that work are:

 http://127.0.0.1:8080/index.php
 ...or...
 http://127.0.0.1:8080

Anything more complex fails like:

http://127.0.0.1:8080/path/to/resource  (HHVM fails, file not found)

Also, sticking in an explicit index.php fails as the framework doesn't handle the index.php in the REQUEST_URI gracefully.

http://127.0.0.1:8080/index.php/path/to/resource  (HHVM works, but framework fails, `index.php` in uri confuses it)

Does anyone know a way to get this to work where ALL request are sent to the root /some/path/public/index.php? Is there an option to set the SCRIPT_NAME explicitly via a option flag/setting?

Ideally the request http://127.0.0.1:8080/path/to/resource would have:

  • SCRIPT_NAME = index.php
  • REQUEST_URI = /path/to/resource
Ray
  • 40,256
  • 21
  • 101
  • 138

2 Answers2

1

Ok, I figured a few ways, running HHVM 3.18.1 with some help from another SO user.

The best way is to use virtual hosts rewite rules:

hhvm.server.default_document = index.php

hhvm.virtual_host[default][rewrite_rules][common][pattern] = "(.*)"
hhvm.virtual_host[default][rewrite_rules][common][to] = "index.php/$1"
hhvm.virtual_host[default][rewrite_rules][common][qsa] = true

Had a hard time finding documentation on this, but here's a link to the HHVM docs sort of explaining: https://docs.hhvm.com/hhvm/configuration/INI-settings#server-mode__virtual-host-format

Note that the documentation is the old format, not the new ini format, so option names should be converted from CamelCase to underscore seperated like camel_case.

Another, less elegant way: Without using rewrites, you can hijack 404's. You need to set BOTH the parameters

  • hhvm.server.error_document404
  • hhvm.server.default_document

Set both of these to whatever your default index/routing file is--let your framework handle 404 messages.

You can set these in an ini config file (I'll call it server.ini):

 hhvm.server.default_document = /path/to/index.php
 hhvm.server.error_document404 = /path/to/index.php

Then you can start the server with:

 hhvm -m server  -p 8080  -c /path/to/server.ini

You can also skip the INI file and pass the details in when starting HHVM:

 hhvm -m server -p 8080 -d  hhvm.server.default_document=/path/to/index.php -d hhvm.server.error_document404=/path/to/index.php

If you don't want to have to run hhvm from a specific folder, you can also set:

hhvm.server.source_root=/path/to/doc_root

Notes:

  • If this is set the default_document and error_document404 will be relative to this directory unless they have fully qualified paths. However, you must set the error_document404 to something explicitly. While default_document seems to default to index.php, the error_document404 seems not to have a default value.
  • Explicitly including index.php in the url (unless it's alone) fails. This means: http://127.0.0.1:8080/index.php/path/to/resource fails but http://127.0.0.1:8080/path/to/resource now works. Guess you can't have your cake and eat it too :)
Ray
  • 40,256
  • 21
  • 101
  • 138
  • @GuilhermeNascimento thanks, makes much more sense. Was finally able to locate some of the documentation for the hhvm virtual host options given text from your question. I'm updating my answer based on your post. – Ray Mar 15 '17 at 18:30
  • @GuilhermeNascimento also if you add your solution as an answer, I'll pick it. – Ray Mar 15 '17 at 18:31
0

As of HHVM 4.26, you can use the hhvm.server.global_document setting in server.ini.

An example would be:

hhvm.server.global_document = index.hh

All requests will now go through index.hh

mike2151
  • 1
  • 2