Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Fri Dec 09, 2016 6:46 pm


This forum is dedicated to Apache, Nginx, Lighttpd, Squid and other HTTP transport protocol related software

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

Joined: Thu Aug 06, 2009 2:48 am
Posts: 105

Offline
 

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.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "Apache to Nginx Server parameters translation with php function"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Apache: how to list virtual hosts that are currently configured on my Apache server ?

LaR3

0

1155

Sun Feb 07, 2010 10:04 am

LaR3 View the latest post

There are no new unread posts for this topic. Attachment(s) Nginx + apache (for PHP) with real IP addresses in logs

designeru

0

571

Tue May 18, 2010 9:49 am

designeru View the latest post

There are no new unread posts for this topic. Converting PhpBB-SEO Apache RewriteRule to Nginx rewrite

debuser

0

1650

Tue Feb 23, 2010 5:00 am

debuser View the latest post

There are no new unread posts for this topic. How to test deflate/gzip compression on a HTTPS (HTTP over SSL) Apache server

debuser

0

1975

Tue Aug 03, 2010 3:30 am

debuser View the latest post

There are no new unread posts for this topic. Apache - Restricting "Server" information in HTTP response header with ServerTokens

debuser

0

414

Thu Nov 10, 2011 6:39 am

debuser View the latest post

There are no new unread posts for this topic. Apache 2.2 installation of mod_rewrite module without recompiling whole apache

debuser

0

2904

Tue Mar 23, 2010 11:22 am

debuser View the latest post

There are no new unread posts for this topic. Fatal error: Call to undefined function filter_var() in php script

LaR3

0

2202

Fri Oct 02, 2009 10:52 am

LaR3 View the latest post

There are no new unread posts for this topic. NGINX: how to set the default virtual host

mandrei99

0

385

Thu Dec 12, 2013 6:01 am

mandrei99 View the latest post

There are no new unread posts for this topic. Nginx: How to list virtual hosts

mandrei99

0

6746

Mon Feb 23, 2015 5:05 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Nginx + php-fpm increase upload_max_filesize and other php values per vhost

mandrei99

0

3572

Mon Jan 02, 2012 9:04 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO