Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

Apache to Nginx Server parameters translation with php function
https://forum.ivorde.com/apache-to-nginx-server-parameters-translation-with-php-function-t127.html
Page 1 of 1

Author:  debuser [ Thu Aug 26, 2010 7:47 am ]
Post subject:  Apache to Nginx Server parameters translation with php function

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:
Code:
Array ( [USER] => nobody [HOME] => / [FCGI_ROLE] => RESPONDER [SCRIPT_FILENAME] => /usr/local/www/www.site.com/public_html/site/index.php [Content-Type] => multipart/form-data [QUERY_STRING] => upload=true [REQUEST_METHOD] => POST [CONTENT_LENGTH] => 40403 [SCRIPT_NAME] => /site/index.php [REQUEST_URI] => /site/index.php?upload=true [DOCUMENT_URI] => /site/index.php [DOCUMENT_ROOT] => /usr/local/www/www.site.com/public_html [SERVER_PROTOCOL] => HTTP/1.1 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_SOFTWARE] => nginx/0.7.65 [REMOTE_ADDR] => 193.110.48.4 [REMOTE_PORT] => 9971 [SERVER_ADDR] => 192.168.1.10 [SERVER_PORT] => 80 [SERVER_NAME] => www.site.com [REDIRECT_STATUS] => 200 [HTTP_HOST] => www.site.com [HTTP_ORIGIN] => http://www.site.com [HTTP_X_FILE_SIZE] => 40403 [HTTP_X_REQUESTED_WITH] => XMLHttpRequest [HTTP_CACHE_CONTROL] => max-age=0 [HTTP_IF_MODIFIED_SINCE] => Mon, 26 Jul 1997 05:00:00 GMT [HTTP_CONTENT_LENGTH] => 40403 [HTTP_X_FILE_NAME] => nokia-6300-yahoo.jpg [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8 [HTTP_CONTENT_TYPE] => multipart/form-data [HTTP_REFERER] => http://www.site.com/site/index.php [HTTP_ACCEPT] => */* [HTTP_ACCEPT_LANGUAGE] => en-us [HTTP_ACCEPT_ENCODING] => gzip, deflate [HTTP_CONNECTION] => keep-alive [PHP_SELF] => /site/index.php [REQUEST_TIME] => 1282822862 )


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;
}

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.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/