Topic review - Apache to Nginx Server parameters translation with php function
Author
Message
debuser
Post subject: Apache to Nginx Server parameters translation with php function | Posted: Thu Aug 26, 2010 7:47 am
When ran as Apache module, PHP will get $_SERVER variables in the form that they come in the request: Content-Type, Content-Length, X-File-Size, X-File-Name and so on.
When ran as PHP-FPM with NGINX, php will see the $_SERVER variables in a totally different way (this is because of the default passing of server parameters to fcgi scripts in nginx).
Here's how $_SERVER variables will be seen by php when ran as php-fpm:
Note the HTTP_CONTENT_TYPE, HTTP_X_FILE_SIZE, HTTP_X_FILE_NAME and other HTTP_* variables.
Below is a simple php function that will regulate these header values into apache like ones. I'm figuring that it's easier to implement it rather than changing a whole application.
The $headers variable will be an asociative array containing the header attribute name as key and the header attribute value as array value. The $replace_array array inside the function can be changed to contain more translations. I just needed these four.
When ran as Apache module, PHP will get $_SERVER variables in the form that they come in the request: [b]Content-Type, Content-Length, X-File-Size, X-File-Name[/b] and so on.
When ran as PHP-FPM with NGINX, php will see the $_SERVER variables in a totally different way (this is because of the default passing of server parameters to fcgi scripts in nginx).
Note the HTTP_CONTENT_TYPE, HTTP_X_FILE_SIZE, HTTP_X_FILE_NAME and other HTTP_* variables.
Below is a simple php function that will regulate these header values into apache like ones. I'm figuring that it's easier to implement it rather than changing a whole application. [code] function emu_getallheaders() { $replace_array = array('CONTENT_TYPE' => 'Content-Type', 'CONTENT_LENGTH' => 'Content-Length', 'X_FILE_SIZE' => 'X-File-Size', 'X_FILE_NAME' => 'X-File-Name'); foreach($_SERVER as $h=>$v) { $h = str_replace("HTTP_", "", $h); $h = $replace_array[$h]; $headers[$h] = $v; } return $headers; } [/code] The [i]$headers[/i] variable will be an asociative array containing the header attribute name as key and the header attribute value as array value. The [i]$replace_array[/i] array inside the function can be changed to contain more translations. I just needed these four.