$_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).
<?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)&&in_array($c=$_SERVER['APP_ENV'],array_keys($a))){return $c;}foreach($a as $c=>$d){$e=$_SERVER['SERVER_NAME'];if((is_array($d)&&in_array($e,$d))||$e==$d){return $c;}}return $b;}, array(
// Environment Definitions
array(
'local' => array('myproject.craft.dev'),
'dev' => array('myproject.mydevsite.com'),
'stage' => array('stage.myproject.com'),
'production' => 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 <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}
require_once $path;
And then in your config.php file (adapted from this stack exchange answer by Ben Parizek):
<?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
'*' => array(
// environment variables
'environmentVariables' => array(
'baseUrl' => SITE_URL,
'basePath' => BASE_PATH . 'html/'
),
// localization settings
'siteUrl' => array(
'en-us' => SITE_URL,
),
),
// specific environment config
'local' => array(
'devMode' => true,
),
'dev' => array(
'devMode' => true,
),
'stage' => array(
// environment variables
'environmentVariables' => array(
'baseUrl' => SITE_URL,
'basePath' => BASE_PATH . 'public/'
),
),
'production' => array(
// environment variables
'environmentVariables' => array(
'baseUrl' => SITE_URL,
'basePath' => BASE_PATH . 'public/'
),
),
);