multi environment nginx, how to check $_SERVER['SERVER_NAME'] I'm trying to have Craft switch baseUrl in general.php based on the environment. The Craft docs tells me to specify the environment in index.php by using logic based on the $_SERVER['SERVER_NAME'] variable. 
But this variable doesn't seem to exist in my server environment.

In Craft, neither craft/config/general.php and craft/public/index.php seem to have access to this variable. Where does the $_SERVER['SERVER_NAME'] for each project live and how do I see what it is? Is it supposed to be the same as the server_name block in my nginx config?
$_SERVER['SERVER_NAME'] is a global server variable (not a craft variable) and should be available. I don't use nginx, so I can't comment on where the $_SERVER['SERVER_NAME'] comes from exactly — but a quick google search seems to explain things:


  Blockquote
  nginx – $_SERVER['SERVER_NAME'] is the first entry in the server_name directive.


As far as multi-environment config — this is the method which I have adopted, which has proven to be reliable and flexible. (*The only issue that I've encountered is that it doesn't seem to like non-standard ports — if your using port :8888 in a virtual dev environment for example. Perhaps there is a way to add support for non-standard ports as well).

In your main index.php page (adapted from this craft cookbook recipe).



&lt;?php

// Path to your craft/ folder
$craftPath = '../craft';

// Multi-environment Configuration
define('CRAFT_ENVIRONMENT', call_user_func_array(function($a,$b){if(array_key_exists('APP_ENV',$_SERVER)&amp;&amp;in_array($c=$_SERVER['APP_ENV'],array_keys($a))){return $c;}foreach($a as $c=&gt;$d){$e=$_SERVER['SERVER_NAME'];if((is_array($d)&amp;&amp;in_array($e,$d))||$e==$d){return $c;}}return $b;}, array(

    // Environment Definitions
    array(
        'local' =&gt; array('myproject.craft.dev'),
        'dev' =&gt; array('myproject.mydevsite.com'),
        'stage' =&gt; array('stage.myproject.com'),
        'production' =&gt; array('myproject.com'),
    ),

    // Default Environment
    'production',

)));

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    exit('Could not find your craft/ folder. Please ensure that &lt;strong&gt;&lt;code&gt;$craftPath&lt;/code&gt;&lt;/strong&gt; is set correctly in '.__FILE__);
}

require_once $path;


And then in your config.php file (adapted from this stack exchange answer by Ben Parizek):

&lt;?php

/**
 * General Configuration
 */

// Ensure our urls have the right scheme
define('URI_SCHEME', ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );

// The site url (doesn't work well when port numbers are required, i.e. http://site.com:8888)
define('SITE_URL', URI_SCHEME . $_SERVER['SERVER_NAME'] . '/');

// The site basepath
define('BASE_PATH', realpath(CRAFT_BASE_PATH . '/../') . '/');

return array(

    // default config for all environments
    '*' =&gt; array(

        // environment variables
        'environmentVariables' =&gt; array(
            'baseUrl'  =&gt; SITE_URL,
            'basePath' =&gt; BASE_PATH . 'html/'
        ),

        // localization settings
        'siteUrl' =&gt; array(
            'en-us' =&gt; SITE_URL,
        ),

    ),

    // specific environment config

    'local' =&gt; array(

        'devMode'    =&gt; true,

    ),

    'dev' =&gt; array(

        'devMode'    =&gt; true,

    ),

    'stage' =&gt; array(

        // environment variables
        'environmentVariables' =&gt; array(
            'baseUrl'  =&gt; SITE_URL,
            'basePath' =&gt; BASE_PATH . 'public/'
        ),

    ),

    'production' =&gt; array(

        // environment variables
        'environmentVariables' =&gt; array(
            'baseUrl'  =&gt; SITE_URL,
            'basePath' =&gt; BASE_PATH . 'public/'
        ),

    ),

);