<p><code>$_SERVER['SERVER_NAME']</code> 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 <code>$_SERVER['SERVER_NAME']</code> comes from exactly — but a quick google search seems to <a href="https://fisharebest.stonystratford.org/132/where-does-php-get-_serverserver_name-apache-versus-nginx/" rel="nofollow noreferrer">explain things</a>:</p>

<blockquote>
  <p>Blockquote
  nginx – $_SERVER['SERVER_NAME'] is the first entry in the server_name directive.</p>
</blockquote>

<p>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).</p>

<p>In your main <code>index.php</code> page (adapted from this <a href="http://craftcookbook.net/recipes/273" rel="nofollow noreferrer">craft cookbook recipe</a>).</p>



<pre class="lang-php prettyprint-override"><code>&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;
</code></pre>

<p>And then in your <code>config.php</code> file (adapted from this <a href="https://craftcms.stackexchange.com/a/4734/784">stack exchange answer</a> by Ben Parizek):</p>

<pre class="lang-php prettyprint-override"><code>&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/'
        ),

    ),

);
</code></pre>

CC BY-SA 3.0
2016-06-16T01:36:33.850Z
2016-06-16T01:43:09.863Z
2017-04-13T12:51:00.160Z
Douglas McDonald
<p>I'm trying to have Craft switch baseUrl in <code>general.ph</code>p based on the environment. The Craft docs tells me to specify the environment in <code>index.php</code> by using logic based on the <code>$_SERVER['SERVER_NAME']</code> variable. 
But this variable doesn't seem to exist in my server environment.</p>

<p>In Craft, neither <code>craft/config/general.php</code> and <code>craft/public/index.php</code> seem to have access to this variable. Where does the <code>$_SERVER['SERVER_NAME']</code> for each project live and how do I see what it is? Is it supposed to be the same as the <code>server_name</code> block in my nginx config?</p>

<php><multi-environment><nginx>
multi environment nginx, how to check $_SERVER['SERVER_NAME']
# multi environment nginx, how to check $_SERVER['SERVER_NAME']

(Asked by: username_0 on 2016-06-15)

I'm trying to have Craft switch baseUrl in `general.ph`p 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?

**Tags:** php, multi-environment, nginx

---

## 1 answers

### Answer by username_1 on 2016-06-16. Score: 2

$_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).
$d){$e=$_SERVER['SERVER_NAME'];if((is_array($d)&∈_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 $craftPath is set correctly in '.__FILE__);
}

require_once $path;

And then in your config.php file (adapted from this stack exchange answer by Ben Parizek):
 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/'
        ),

    ),

);


---

